diff --git a/crates/matrix-sdk-common/src/debug.rs b/crates/matrix-sdk-common/src/debug.rs index 8d8f7195f..5b2ad37ad 100644 --- a/crates/matrix-sdk-common/src/debug.rs +++ b/crates/matrix-sdk-common/src/debug.rs @@ -18,6 +18,28 @@ use std::fmt; use ruma::serde::Raw; +pub trait DebugStructExt<'a, 'b> { + fn maybe_field( + &mut self, + name: &str, + value: &Option, + ) -> &mut fmt::DebugStruct<'a, 'b>; +} + +impl<'a, 'b> DebugStructExt<'a, 'b> for fmt::DebugStruct<'a, 'b> { + fn maybe_field( + &mut self, + name: &str, + value: &Option, + ) -> &mut fmt::DebugStruct<'a, 'b> { + if let Some(value) = value { + self.field(name, value); + } + + self + } +} + /// A wrapper around `Raw` that implements `Debug` in a way that only prints the /// event ID and event type. pub struct DebugRawEvent<'a, T>(pub &'a Raw); diff --git a/crates/matrix-sdk/src/config/sync.rs b/crates/matrix-sdk/src/config/sync.rs index 797ec96f3..fd3627db6 100644 --- a/crates/matrix-sdk/src/config/sync.rs +++ b/crates/matrix-sdk/src/config/sync.rs @@ -14,6 +14,7 @@ use std::{fmt, time::Duration}; +use matrix_sdk_common::debug::DebugStructExt; use ruma::{api::client::sync::sync_events, presence::PresenceState}; const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30); @@ -38,20 +39,13 @@ impl Default for SyncSettings { #[cfg(not(tarpaulin_include))] impl fmt::Debug for SyncSettings { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut s = f.debug_struct("SyncSettings"); - - macro_rules! opt_field { - ($field:ident) => { - if let Some(value) = &self.$field { - s.field(stringify!($field), value); - } - }; - } - - opt_field!(filter); - opt_field!(timeout); - - s.field("full_state", &self.full_state).finish() + let Self { filter, timeout, token: _, full_state, set_presence } = self; + f.debug_struct("SyncSettings") + .maybe_field("filter", filter) + .maybe_field("timeout", timeout) + .field("full_state", full_state) + .field("set_presence", set_presence) + .finish() } }