unwind_context/
debug_with.rs

1use core::fmt::{Debug, Display, Formatter, Result as FmtResult};
2
3/// An utility wrapper type which is used to forward both [`core::fmt::Debug`]
4/// and [`core::fmt::Display`] value implementations to its
5/// [`core::fmt::Display`] implementation.
6///
7/// # Examples
8///
9/// ```rust
10/// use unwind_context::{unwind_context, WithDisplay};
11///
12/// fn func(value: impl core::fmt::Display) {
13///     let _ctx = unwind_context!(fn(WithDisplay(value)));
14///     // ...
15/// }
16/// ```
17#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
18pub struct WithDisplay<T>(
19    /// The wrapped value to be formatted with [`core::fmt::Display`] regardless
20    /// of whether formatting is invoked with [`core::fmt::Debug`] or
21    /// [`core::fmt::Display`] formatter.
22    pub T,
23);
24
25/// An utility wrapper type which is used to forward both [`core::fmt::Debug`]
26/// and [`core::fmt::Display`] value implementations to its [`core::fmt::Debug`]
27/// implementation with pretty flag.
28///
29/// # Examples
30///
31/// ```rust
32/// use unwind_context::{unwind_context, WithPrettyDebug};
33///
34/// fn func(long_json: impl core::fmt::Debug) {
35///     let _ctx = unwind_context!(fn(WithPrettyDebug(long_json)));
36///     // ...
37/// }
38/// ```
39#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
40pub struct WithPrettyDebug<T>(
41    /// The wrapped value to be formatted with pretty
42    /// [`core::fmt::Debug`] variant regardless of whether formatting is
43    /// invoked with [`core::fmt::Debug`] or [`core::fmt::Display`]
44    /// formatter.
45    pub T,
46);
47
48impl<T> Display for WithDisplay<T>
49where
50    T: Display,
51{
52    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
53        Display::fmt(&self.0, f)
54    }
55}
56
57impl<T> Debug for WithDisplay<T>
58where
59    T: Display,
60{
61    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
62        Display::fmt(&self.0, f)
63    }
64}
65
66impl<T> Display for WithPrettyDebug<T>
67where
68    T: Debug,
69{
70    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
71        write!(f, "{:#?}", self.0)
72    }
73}
74
75impl<T> Debug for WithPrettyDebug<T>
76where
77    T: Debug,
78{
79    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
80        write!(f, "{:#?}", self.0)
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use crate::test_util::buf_fmt;
87    use crate::{WithDisplay, WithPrettyDebug};
88
89    #[derive(Clone, Debug)]
90    struct Struct {
91        _first: u32,
92        _second: &'static str,
93    }
94
95    #[test]
96    fn test_debug_with_display() {
97        let mut buffer = [0; 16];
98        assert_eq!(
99            buf_fmt(&mut buffer, format_args!("{}", "foo\nbar")),
100            Ok("foo\nbar")
101        );
102        assert_eq!(
103            buf_fmt(&mut buffer, format_args!("{:?}", "foo\nbar")),
104            Ok("\"foo\\nbar\"")
105        );
106        assert_eq!(
107            buf_fmt(&mut buffer, format_args!("{}", WithDisplay("foo\nbar"))),
108            Ok("foo\nbar")
109        );
110        assert_eq!(
111            buf_fmt(&mut buffer, format_args!("{:?}", WithDisplay("foo\nbar"))),
112            Ok("foo\nbar")
113        );
114        assert_eq!(
115            buf_fmt(&mut buffer, format_args!("{:#?}", WithDisplay("foo\nbar"))),
116            Ok("foo\nbar")
117        );
118    }
119
120    #[test]
121    fn test_debug_with_pretty_debug() {
122        let value = Struct {
123            _first: 1,
124            _second: "foo\nbar",
125        };
126        let mut buffer = [0; 64];
127        assert_eq!(
128            buf_fmt(&mut buffer, format_args!("{value:?}")),
129            Ok("Struct { _first: 1, _second: \"foo\\nbar\" }")
130        );
131        assert_eq!(
132            buf_fmt(&mut buffer, format_args!("{value:#?}")),
133            Ok("Struct {\n    _first: 1,\n    _second: \"foo\\nbar\",\n}")
134        );
135        assert_eq!(
136            buf_fmt(&mut buffer, format_args!("{}", WithPrettyDebug(&value))),
137            Ok("Struct {\n    _first: 1,\n    _second: \"foo\\nbar\",\n}")
138        );
139        assert_eq!(
140            buf_fmt(&mut buffer, format_args!("{:?}", WithPrettyDebug(&value))),
141            Ok("Struct {\n    _first: 1,\n    _second: \"foo\\nbar\",\n}")
142        );
143        assert_eq!(
144            buf_fmt(&mut buffer, format_args!("{:#?}", WithPrettyDebug(&value))),
145            Ok("Struct {\n    _first: 1,\n    _second: \"foo\\nbar\",\n}")
146        );
147    }
148}