7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2023 The Matrix.org Foundation C.I.C. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | // you may not use this file except in compliance with the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | // You may obtain a copy of the License at |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | // Unless required by applicable law or agreed to in writing, software |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | // See the License for the specific language governing permissions and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | // limitations under the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | //! Helpers for creating `std::fmt::Debug` implementations. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | use std::fmt; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | use ruma::serde::Raw; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | pub trait DebugStructExt<'a, 'b> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | fn maybe_field<T: fmt::Debug>( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | &mut self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | name: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | value: &Option<T>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | ) -> &mut fmt::DebugStruct<'a, 'b>; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | impl<'a, 'b> DebugStructExt<'a, 'b> for fmt::DebugStruct<'a, 'b> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | fn maybe_field<T: fmt::Debug>( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | &mut self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | name: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | value: &Option<T>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | ) -> &mut fmt::DebugStruct<'a, 'b> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | if let Some(value) = value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | self.field(name, value); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | /// A wrapper around `Raw` that implements `Debug` in a way that only prints the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | /// event ID and event type. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | pub struct DebugRawEvent<'a, T>(pub &'a Raw<T>); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | #[cfg(not(tarpaulin_include))] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | impl<T> fmt::Debug for DebugRawEvent<'_, T> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | f.debug_struct("RawEvent") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | .field("event_id", &DebugStringField(self.0.get_field("event_id"))) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | .field("event_type", &DebugStringField(self.0.get_field("type"))) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | .finish_non_exhaustive() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | /// A wrapper around `Raw` that implements `Debug` in a way that only prints the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | /// event type. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | pub struct DebugRawEventNoId<'a, T>(pub &'a Raw<T>); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | #[cfg(not(tarpaulin_include))] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | impl<T> fmt::Debug for DebugRawEventNoId<'_, T> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | f.debug_struct("RawEvent") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | .field("event_type", &DebugStringField(self.0.get_field("type"))) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | .finish_non_exhaustive() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | struct DebugStringField(serde_json::Result<Option<String>>); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | #[cfg(not(tarpaulin_include))] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | impl fmt::Debug for DebugStringField { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | match &self.0 { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | Ok(Some(id)) => id.fmt(f), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | Ok(None) => f.write_str("Missing"), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | Err(e) => f.debug_tuple("Invalid").field(&e).finish(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | } |