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 matrix_sdk_common::deserialized_responses::ProcessedToDeviceEvent;
21use ruma::{
22    api::client::sync::sync_events::v3::{InvitedRoom, KnockedRoom},
23    serde::Raw,
24};
25
26/// A wrapper around a slice of `Raw` events that implements `Debug` in a way
27/// that only prints the event type of each item.
28pub struct DebugListOfRawEventsNoId<'a, T>(pub &'a [Raw<T>]);
29
30#[cfg(not(tarpaulin_include))]
31impl<T> fmt::Debug for DebugListOfRawEventsNoId<'_, T> {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        let mut list = f.debug_list();
34        list.entries(self.0.iter().map(DebugRawEventNoId));
35        list.finish()
36    }
37}
38
39/// A wrapper around a slice of `ProcessedToDeviceEvent` events that implements
40/// `Debug` in a way that only prints the event type of each item.
41pub struct DebugListOfProcessedToDeviceEvents<'a>(pub &'a [ProcessedToDeviceEvent]);
42
43#[cfg(not(tarpaulin_include))]
44impl fmt::Debug for DebugListOfProcessedToDeviceEvents<'_> {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        let mut list = f.debug_list();
47        list.entries(self.0.iter().map(|e| DebugRawEventNoId(e.as_raw())));
48        list.finish()
49    }
50}
51
52/// A wrapper around an invited 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 `invite_state`.
55pub struct DebugInvitedRoom<'a>(pub &'a InvitedRoom);
56
57#[cfg(not(tarpaulin_include))]
58impl fmt::Debug for DebugInvitedRoom<'_> {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        f.debug_struct("InvitedRoom")
61            .field("invite_state", &DebugListOfRawEvents(&self.0.invite_state.events))
62            .finish()
63    }
64}
65
66/// A wrapper around a knocked on room as found in `/sync` responses that
67/// implements `Debug` in a way that only prints the event ID and event type for
68/// the raw events contained in `knock_state`.
69pub struct DebugKnockedRoom<'a>(pub &'a KnockedRoom);
70
71#[cfg(not(tarpaulin_include))]
72impl fmt::Debug for DebugKnockedRoom<'_> {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        f.debug_struct("KnockedRoom")
75            .field("knock_state", &DebugListOfRawEvents(&self.0.knock_state.events))
76            .finish()
77    }
78}
79
80pub(crate) struct DebugListOfRawEvents<'a, T>(pub &'a [Raw<T>]);
81
82#[cfg(not(tarpaulin_include))]
83impl<T> fmt::Debug for DebugListOfRawEvents<'_, T> {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        let mut list = f.debug_list();
86        list.entries(self.0.iter().map(DebugRawEvent));
87        list.finish()
88    }
89}