Add DebugStructExt helper for writing less verbose Debug impls

This commit is contained in:
Jonas Platte
2023-05-11 13:03:04 +02:00
committed by Jonas Platte
parent 149950cc29
commit 30eee70e9d
2 changed files with 30 additions and 14 deletions

View File

@@ -18,6 +18,28 @@ use std::fmt;
use ruma::serde::Raw;
pub trait DebugStructExt<'a, 'b> {
fn maybe_field<T: fmt::Debug>(
&mut self,
name: &str,
value: &Option<T>,
) -> &mut fmt::DebugStruct<'a, 'b>;
}
impl<'a, 'b> DebugStructExt<'a, 'b> for fmt::DebugStruct<'a, 'b> {
fn maybe_field<T: fmt::Debug>(
&mut self,
name: &str,
value: &Option<T>,
) -> &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<T>);

View File

@@ -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()
}
}