matrix_sdk_base/
debug.rs

1// Copyright 2023 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Helpers for creating `std::fmt::Debug` implementations.
16
17use std::fmt;
18
19pub use matrix_sdk_common::debug::*;
20use ruma::{
21    api::client::sync::sync_events::v3::{InvitedRoom, KnockedRoom},
22    serde::Raw,
23};
24
25/// A wrapper around a slice of `Raw` events that implements `Debug` in a way
26/// that only prints the event type of each item.
27pub struct DebugListOfRawEventsNoId<'a, T>(pub &'a [Raw<T>]);
28
29#[cfg(not(tarpaulin_include))]
30impl<T> fmt::Debug for DebugListOfRawEventsNoId<'_, T> {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        let mut list = f.debug_list();
33        list.entries(self.0.iter().map(DebugRawEventNoId));
34        list.finish()
35    }
36}
37
38/// A wrapper around an invited room as found in `/sync` responses that
39/// implements `Debug` in a way that only prints the event ID and event type for
40/// the raw events contained in `invite_state`.
41pub struct DebugInvitedRoom<'a>(pub &'a InvitedRoom);
42
43#[cfg(not(tarpaulin_include))]
44impl fmt::Debug for DebugInvitedRoom<'_> {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        f.debug_struct("InvitedRoom")
47            .field("invite_state", &DebugListOfRawEvents(&self.0.invite_state.events))
48            .finish()
49    }
50}
51
52/// A wrapper around a knocked on room as found in `/sync` responses that
53/// implements `Debug` in a way that only prints the event ID and event type for
54/// the raw events contained in `knock_state`.
55pub struct DebugKnockedRoom<'a>(pub &'a KnockedRoom);
56
57#[cfg(not(tarpaulin_include))]
58impl fmt::Debug for DebugKnockedRoom<'_> {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        f.debug_struct("KnockedRoom")
61            .field("knock_state", &DebugListOfRawEvents(&self.0.knock_state.events))
62            .finish()
63    }
64}
65
66pub(crate) struct DebugListOfRawEvents<'a, T>(pub &'a [Raw<T>]);
67
68#[cfg(not(tarpaulin_include))]
69impl<T> fmt::Debug for DebugListOfRawEvents<'_, T> {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        let mut list = f.debug_list();
72        list.entries(self.0.iter().map(DebugRawEvent));
73        list.finish()
74    }
75}