matrix_sdk_common/
debug.rs1use std::fmt;
18
19use ruma::serde::Raw;
20
21pub trait DebugStructExt<'a, 'b> {
22 fn maybe_field<T: fmt::Debug>(
23 &mut self,
24 name: &str,
25 value: &Option<T>,
26 ) -> &mut fmt::DebugStruct<'a, 'b>;
27}
28
29impl<'a, 'b> DebugStructExt<'a, 'b> for fmt::DebugStruct<'a, 'b> {
30 fn maybe_field<T: fmt::Debug>(
31 &mut self,
32 name: &str,
33 value: &Option<T>,
34 ) -> &mut fmt::DebugStruct<'a, 'b> {
35 if let Some(value) = value {
36 self.field(name, value);
37 }
38
39 self
40 }
41}
42
43pub struct DebugRawEvent<'a, T>(pub &'a Raw<T>);
46
47#[cfg(not(tarpaulin_include))]
48impl<T> fmt::Debug for DebugRawEvent<'_, T> {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 f.debug_struct("RawEvent")
51 .field("event_id", &DebugStringField(self.0.get_field("event_id")))
52 .field("event_type", &DebugStringField(self.0.get_field("type")))
53 .finish_non_exhaustive()
54 }
55}
56
57pub struct DebugRawEventNoId<'a, T>(pub &'a Raw<T>);
60
61#[cfg(not(tarpaulin_include))]
62impl<T> fmt::Debug for DebugRawEventNoId<'_, T> {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 f.debug_struct("RawEvent")
65 .field("event_type", &DebugStringField(self.0.get_field("type")))
66 .finish_non_exhaustive()
67 }
68}
69
70struct DebugStringField(serde_json::Result<Option<String>>);
71
72#[cfg(not(tarpaulin_include))]
73impl fmt::Debug for DebugStringField {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 match &self.0 {
76 Ok(Some(id)) => id.fmt(f),
77 Ok(None) => f.write_str("Missing"),
78 Err(e) => f.debug_tuple("Invalid").field(&e).finish(),
79 }
80 }
81}