From eda45e445c58151bafd01f130fd919ccdbd3af74 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 2 Oct 2025 04:38:20 +0000 Subject: [PATCH] Implement better fmt::Debug for pdu::Builder. Signed-off-by: Jason Volk --- src/core/matrix/pdu/builder.rs | 50 ++++++++++++++++++++++------- src/service/rooms/timeline/build.rs | 7 +++- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/core/matrix/pdu/builder.rs b/src/core/matrix/pdu/builder.rs index 38ba3f44..8957a8a5 100644 --- a/src/core/matrix/pdu/builder.rs +++ b/src/core/matrix/pdu/builder.rs @@ -1,4 +1,4 @@ -use std::collections::BTreeMap; +use std::{collections::BTreeMap, fmt}; use ruma::{ MilliSecondsSinceUnixEpoch, OwnedEventId, @@ -10,7 +10,7 @@ use serde_json::value::{RawValue as RawJsonValue, to_raw_value}; use super::StateKey; /// Build the start of a PDU in order to add it to the Database. -#[derive(Debug, Deserialize)] +#[derive(Deserialize)] pub struct Builder { #[serde(rename = "type")] pub event_type: TimelineEventType, @@ -30,6 +30,19 @@ pub struct Builder { type Unsigned = BTreeMap; +impl Default for Builder { + fn default() -> Self { + Self { + event_type: "m.room.message".into(), + content: Box::::default(), + unsigned: None, + state_key: None, + redacts: None, + timestamp: None, + } + } +} + impl Builder { pub fn state(state_key: S, content: &T) -> Self where @@ -58,15 +71,30 @@ impl Builder { } } -impl Default for Builder { - fn default() -> Self { - Self { - event_type: "m.room.message".into(), - content: Box::::default(), - unsigned: None, - state_key: None, - redacts: None, - timestamp: None, +impl fmt::Debug for Builder { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut d = f.debug_struct("Builder"); + + d.field("type", &self.event_type); + + if let Some(state_key) = self.state_key.as_ref() { + d.field("state_key", state_key); } + + if let Some(redacts) = self.redacts.as_ref() { + d.field("redacts", redacts); + } + + if let Some(timestamp) = self.timestamp.as_ref() { + d.field("ts", timestamp); + } + + if let Some(unsigned) = self.unsigned.as_ref() { + d.field("unsigned", unsigned); + } + + d.field("content", &self.content); + + d.finish() } } diff --git a/src/service/rooms/timeline/build.rs b/src/service/rooms/timeline/build.rs index 62226afc..c5a45d13 100644 --- a/src/service/rooms/timeline/build.rs +++ b/src/service/rooms/timeline/build.rs @@ -23,7 +23,12 @@ use super::RoomMutexGuard; /// takes a roomid_mutex_state, meaning that only this function is able to /// mutate the room state. #[implement(super::Service)] -#[tracing::instrument(skip(self, state_lock), level = "debug", ret)] +#[tracing::instrument( + name = "build_and_append" + level = "debug", + skip(self, state_lock), + ret, +)] pub async fn build_and_append_pdu( &self, pdu_builder: PduBuilder,