atagen

matrix-rust-sdk

public
Star0
Watch0
ssh://brewery@pls.lobotomise.me:33/atagen/matrix-rust-sdk.git

Blame: crates/matrix-sdk-common/src/debug.rs

7ff8adafe9e4atagen2026-10-22 12:30 UTC1
// Copyright 2023 The Matrix.org Foundation C.I.C.
7ff8adafe9e4atagen2026-10-22 12:30 UTC2
//
7ff8adafe9e4atagen2026-10-22 12:30 UTC3
// Licensed under the Apache License, Version 2.0 (the "License");
7ff8adafe9e4atagen2026-10-22 12:30 UTC4
// you may not use this file except in compliance with the License.
7ff8adafe9e4atagen2026-10-22 12:30 UTC5
// You may obtain a copy of the License at
7ff8adafe9e4atagen2026-10-22 12:30 UTC6
//
7ff8adafe9e4atagen2026-10-22 12:30 UTC7
//     http://www.apache.org/licenses/LICENSE-2.0
7ff8adafe9e4atagen2026-10-22 12:30 UTC8
//
7ff8adafe9e4atagen2026-10-22 12:30 UTC9
// Unless required by applicable law or agreed to in writing, software
7ff8adafe9e4atagen2026-10-22 12:30 UTC10
// distributed under the License is distributed on an "AS IS" BASIS,
7ff8adafe9e4atagen2026-10-22 12:30 UTC11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7ff8adafe9e4atagen2026-10-22 12:30 UTC12
// See the License for the specific language governing permissions and
7ff8adafe9e4atagen2026-10-22 12:30 UTC13
// limitations under the License.
7ff8adafe9e4atagen2026-10-22 12:30 UTC14
7ff8adafe9e4atagen2026-10-22 12:30 UTC15
//! Helpers for creating `std::fmt::Debug` implementations.
7ff8adafe9e4atagen2026-10-22 12:30 UTC16
7ff8adafe9e4atagen2026-10-22 12:30 UTC17
use std::fmt;
7ff8adafe9e4atagen2026-10-22 12:30 UTC18
7ff8adafe9e4atagen2026-10-22 12:30 UTC19
use ruma::serde::Raw;
7ff8adafe9e4atagen2026-10-22 12:30 UTC20
7ff8adafe9e4atagen2026-10-22 12:30 UTC21
pub trait DebugStructExt<'a, 'b> {
7ff8adafe9e4atagen2026-10-22 12:30 UTC22
    fn maybe_field<T: fmt::Debug>(
7ff8adafe9e4atagen2026-10-22 12:30 UTC23
        &mut self,
7ff8adafe9e4atagen2026-10-22 12:30 UTC24
        name: &str,
7ff8adafe9e4atagen2026-10-22 12:30 UTC25
        value: &Option<T>,
7ff8adafe9e4atagen2026-10-22 12:30 UTC26
    ) -> &mut fmt::DebugStruct<'a, 'b>;
7ff8adafe9e4atagen2026-10-22 12:30 UTC27
}
7ff8adafe9e4atagen2026-10-22 12:30 UTC28
7ff8adafe9e4atagen2026-10-22 12:30 UTC29
impl<'a, 'b> DebugStructExt<'a, 'b> for fmt::DebugStruct<'a, 'b> {
7ff8adafe9e4atagen2026-10-22 12:30 UTC30
    fn maybe_field<T: fmt::Debug>(
7ff8adafe9e4atagen2026-10-22 12:30 UTC31
        &mut self,
7ff8adafe9e4atagen2026-10-22 12:30 UTC32
        name: &str,
7ff8adafe9e4atagen2026-10-22 12:30 UTC33
        value: &Option<T>,
7ff8adafe9e4atagen2026-10-22 12:30 UTC34
    ) -> &mut fmt::DebugStruct<'a, 'b> {
7ff8adafe9e4atagen2026-10-22 12:30 UTC35
        if let Some(value) = value {
7ff8adafe9e4atagen2026-10-22 12:30 UTC36
            self.field(name, value);
7ff8adafe9e4atagen2026-10-22 12:30 UTC37
        }
7ff8adafe9e4atagen2026-10-22 12:30 UTC38
7ff8adafe9e4atagen2026-10-22 12:30 UTC39
        self
7ff8adafe9e4atagen2026-10-22 12:30 UTC40
    }
7ff8adafe9e4atagen2026-10-22 12:30 UTC41
}
7ff8adafe9e4atagen2026-10-22 12:30 UTC42
7ff8adafe9e4atagen2026-10-22 12:30 UTC43
/// A wrapper around `Raw` that implements `Debug` in a way that only prints the
7ff8adafe9e4atagen2026-10-22 12:30 UTC44
/// event ID and event type.
7ff8adafe9e4atagen2026-10-22 12:30 UTC45
pub struct DebugRawEvent<'a, T>(pub &'a Raw<T>);
7ff8adafe9e4atagen2026-10-22 12:30 UTC46
7ff8adafe9e4atagen2026-10-22 12:30 UTC47
#[cfg(not(tarpaulin_include))]
7ff8adafe9e4atagen2026-10-22 12:30 UTC48
impl<T> fmt::Debug for DebugRawEvent<'_, T> {
7ff8adafe9e4atagen2026-10-22 12:30 UTC49
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7ff8adafe9e4atagen2026-10-22 12:30 UTC50
        f.debug_struct("RawEvent")
7ff8adafe9e4atagen2026-10-22 12:30 UTC51
            .field("event_id", &DebugStringField(self.0.get_field("event_id")))
7ff8adafe9e4atagen2026-10-22 12:30 UTC52
            .field("event_type", &DebugStringField(self.0.get_field("type")))
7ff8adafe9e4atagen2026-10-22 12:30 UTC53
            .finish_non_exhaustive()
7ff8adafe9e4atagen2026-10-22 12:30 UTC54
    }
7ff8adafe9e4atagen2026-10-22 12:30 UTC55
}
7ff8adafe9e4atagen2026-10-22 12:30 UTC56
7ff8adafe9e4atagen2026-10-22 12:30 UTC57
/// A wrapper around `Raw` that implements `Debug` in a way that only prints the
7ff8adafe9e4atagen2026-10-22 12:30 UTC58
/// event type.
7ff8adafe9e4atagen2026-10-22 12:30 UTC59
pub struct DebugRawEventNoId<'a, T>(pub &'a Raw<T>);
7ff8adafe9e4atagen2026-10-22 12:30 UTC60
7ff8adafe9e4atagen2026-10-22 12:30 UTC61
#[cfg(not(tarpaulin_include))]
7ff8adafe9e4atagen2026-10-22 12:30 UTC62
impl<T> fmt::Debug for DebugRawEventNoId<'_, T> {
7ff8adafe9e4atagen2026-10-22 12:30 UTC63
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7ff8adafe9e4atagen2026-10-22 12:30 UTC64
        f.debug_struct("RawEvent")
7ff8adafe9e4atagen2026-10-22 12:30 UTC65
            .field("event_type", &DebugStringField(self.0.get_field("type")))
7ff8adafe9e4atagen2026-10-22 12:30 UTC66
            .finish_non_exhaustive()
7ff8adafe9e4atagen2026-10-22 12:30 UTC67
    }
7ff8adafe9e4atagen2026-10-22 12:30 UTC68
}
7ff8adafe9e4atagen2026-10-22 12:30 UTC69
7ff8adafe9e4atagen2026-10-22 12:30 UTC70
struct DebugStringField(serde_json::Result<Option<String>>);
7ff8adafe9e4atagen2026-10-22 12:30 UTC71
7ff8adafe9e4atagen2026-10-22 12:30 UTC72
#[cfg(not(tarpaulin_include))]
7ff8adafe9e4atagen2026-10-22 12:30 UTC73
impl fmt::Debug for DebugStringField {
7ff8adafe9e4atagen2026-10-22 12:30 UTC74
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7ff8adafe9e4atagen2026-10-22 12:30 UTC75
        match &self.0 {
7ff8adafe9e4atagen2026-10-22 12:30 UTC76
            Ok(Some(id)) => id.fmt(f),
7ff8adafe9e4atagen2026-10-22 12:30 UTC77
            Ok(None) => f.write_str("Missing"),
7ff8adafe9e4atagen2026-10-22 12:30 UTC78
            Err(e) => f.debug_tuple("Invalid").field(&e).finish(),
7ff8adafe9e4atagen2026-10-22 12:30 UTC79
        }
7ff8adafe9e4atagen2026-10-22 12:30 UTC80
    }
7ff8adafe9e4atagen2026-10-22 12:30 UTC81
}