Skip to main content

embassy_stm32/
fmt.rs

1#![macro_use]
2#![allow(unused)]
3
4use core::fmt::{Debug, Display, LowerHex};
5
6#[cfg(all(feature = "defmt", feature = "log"))]
7compile_error!("You may not enable both `defmt` and `log` features.");
8
9#[collapse_debuginfo(yes)]
10macro_rules! rcc_assert {
11    ($($x:tt)*) => {
12        {
13            #[cfg(not(feature = "unchecked-overclocking"))]
14            {
15                #[cfg(not(feature = "defmt"))]
16                ::core::assert!($($x)*);
17                #[cfg(feature = "defmt")]
18                ::defmt::assert!($($x)*);
19            }
20            #[cfg(feature = "unchecked-overclocking")]
21            {
22                #[cfg(feature = "log")]
23                ::log::warn!("`rcc_assert!` skipped: `unchecked-overclocking` feature is enabled.");
24                #[cfg(feature = "defmt")]
25                ::defmt::warn!("`rcc_assert!` skipped: `unchecked-overclocking` feature is enabled.");
26            }
27        }
28    };
29}
30
31#[collapse_debuginfo(yes)]
32macro_rules! assert {
33    ($($x:tt)*) => {
34        {
35            #[cfg(not(feature = "defmt"))]
36            ::core::assert!($($x)*);
37            #[cfg(feature = "defmt")]
38            ::defmt::assert!($($x)*);
39        }
40    };
41}
42
43#[collapse_debuginfo(yes)]
44macro_rules! assert_eq {
45    ($($x:tt)*) => {
46        {
47            #[cfg(not(feature = "defmt"))]
48            ::core::assert_eq!($($x)*);
49            #[cfg(feature = "defmt")]
50            ::defmt::assert_eq!($($x)*);
51        }
52    };
53}
54
55#[collapse_debuginfo(yes)]
56macro_rules! assert_ne {
57    ($($x:tt)*) => {
58        {
59            #[cfg(not(feature = "defmt"))]
60            ::core::assert_ne!($($x)*);
61            #[cfg(feature = "defmt")]
62            ::defmt::assert_ne!($($x)*);
63        }
64    };
65}
66
67#[collapse_debuginfo(yes)]
68macro_rules! debug_assert {
69    ($($x:tt)*) => {
70        {
71            #[cfg(not(feature = "defmt"))]
72            ::core::debug_assert!($($x)*);
73            #[cfg(feature = "defmt")]
74            ::defmt::debug_assert!($($x)*);
75        }
76    };
77}
78
79#[collapse_debuginfo(yes)]
80macro_rules! debug_assert_eq {
81    ($($x:tt)*) => {
82        {
83            #[cfg(not(feature = "defmt"))]
84            ::core::debug_assert_eq!($($x)*);
85            #[cfg(feature = "defmt")]
86            ::defmt::debug_assert_eq!($($x)*);
87        }
88    };
89}
90
91#[collapse_debuginfo(yes)]
92macro_rules! debug_assert_ne {
93    ($($x:tt)*) => {
94        {
95            #[cfg(not(feature = "defmt"))]
96            ::core::debug_assert_ne!($($x)*);
97            #[cfg(feature = "defmt")]
98            ::defmt::debug_assert_ne!($($x)*);
99        }
100    };
101}
102
103#[collapse_debuginfo(yes)]
104macro_rules! todo {
105    ($($x:tt)*) => {
106        {
107            #[cfg(not(feature = "defmt"))]
108            ::core::todo!($($x)*);
109            #[cfg(feature = "defmt")]
110            ::defmt::todo!($($x)*);
111        }
112    };
113}
114
115#[collapse_debuginfo(yes)]
116macro_rules! unreachable {
117    ($($x:tt)*) => {
118        {
119            #[cfg(not(feature = "defmt"))]
120            ::core::unreachable!($($x)*);
121            #[cfg(feature = "defmt")]
122            ::defmt::unreachable!($($x)*);
123        }
124    };
125}
126
127#[collapse_debuginfo(yes)]
128macro_rules! panic {
129    ($($x:tt)*) => {
130        {
131            #[cfg(not(feature = "defmt"))]
132            ::core::panic!($($x)*);
133            #[cfg(feature = "defmt")]
134            ::defmt::panic!($($x)*);
135        }
136    };
137}
138
139#[collapse_debuginfo(yes)]
140macro_rules! trace {
141    ($s:literal $(, $x:expr)* $(,)?) => {
142        {
143            #[cfg(feature = "log")]
144            ::log::trace!($s $(, $x)*);
145            #[cfg(feature = "defmt")]
146            ::defmt::trace!($s $(, $x)*);
147            #[cfg(not(any(feature = "log", feature="defmt")))]
148            let _ = ($( & $x ),*);
149        }
150    };
151}
152
153#[collapse_debuginfo(yes)]
154macro_rules! debug {
155    ($s:literal $(, $x:expr)* $(,)?) => {
156        {
157            #[cfg(feature = "log")]
158            ::log::debug!($s $(, $x)*);
159            #[cfg(feature = "defmt")]
160            ::defmt::debug!($s $(, $x)*);
161            #[cfg(not(any(feature = "log", feature="defmt")))]
162            let _ = ($( & $x ),*);
163        }
164    };
165}
166
167#[collapse_debuginfo(yes)]
168macro_rules! info {
169    ($s:literal $(, $x:expr)* $(,)?) => {
170        {
171            #[cfg(feature = "log")]
172            ::log::info!($s $(, $x)*);
173            #[cfg(feature = "defmt")]
174            ::defmt::info!($s $(, $x)*);
175            #[cfg(not(any(feature = "log", feature="defmt")))]
176            let _ = ($( & $x ),*);
177        }
178    };
179}
180
181#[collapse_debuginfo(yes)]
182macro_rules! warn {
183    ($s:literal $(, $x:expr)* $(,)?) => {
184        {
185            #[cfg(feature = "log")]
186            ::log::warn!($s $(, $x)*);
187            #[cfg(feature = "defmt")]
188            ::defmt::warn!($s $(, $x)*);
189            #[cfg(not(any(feature = "log", feature="defmt")))]
190            let _ = ($( & $x ),*);
191        }
192    };
193}
194
195#[collapse_debuginfo(yes)]
196macro_rules! error {
197    ($s:literal $(, $x:expr)* $(,)?) => {
198        {
199            #[cfg(feature = "log")]
200            ::log::error!($s $(, $x)*);
201            #[cfg(feature = "defmt")]
202            ::defmt::error!($s $(, $x)*);
203            #[cfg(not(any(feature = "log", feature="defmt")))]
204            let _ = ($( & $x ),*);
205        }
206    };
207}
208
209#[cfg(feature = "defmt")]
210trait_set::trait_set! {
211    pub trait Debuggable = Debug + defmt::Format;
212}
213
214#[cfg(not(feature = "defmt"))]
215trait_set::trait_set! {
216    pub trait Debuggable = Debug;
217}
218
219#[cfg(feature = "defmt")]
220#[collapse_debuginfo(yes)]
221macro_rules! unwrap {
222    ($($x:tt)*) => {
223        ::defmt::unwrap!($($x)*)
224    };
225}
226
227#[cfg(not(feature = "defmt"))]
228#[collapse_debuginfo(yes)]
229macro_rules! unwrap {
230    ($arg:expr) => {
231        match $crate::fmt::Try::into_result($arg) {
232            ::core::result::Result::Ok(t) => t,
233            ::core::result::Result::Err(e) => {
234                ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e);
235            }
236        }
237    };
238    ($arg:expr, $($msg:expr),+ $(,)? ) => {
239        match $crate::fmt::Try::into_result($arg) {
240            ::core::result::Result::Ok(t) => t,
241            ::core::result::Result::Err(e) => {
242                ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
243            }
244        }
245    }
246}
247
248#[derive(Debug, Copy, Clone, Eq, PartialEq)]
249pub struct NoneError;
250
251pub trait Try {
252    type Ok;
253    type Error;
254    fn into_result(self) -> Result<Self::Ok, Self::Error>;
255}
256
257impl<T> Try for Option<T> {
258    type Ok = T;
259    type Error = NoneError;
260
261    #[inline]
262    fn into_result(self) -> Result<T, NoneError> {
263        self.ok_or(NoneError)
264    }
265}
266
267impl<T, E> Try for Result<T, E> {
268    type Ok = T;
269    type Error = E;
270
271    #[inline]
272    fn into_result(self) -> Self {
273        self
274    }
275}
276
277pub(crate) struct Bytes<'a>(pub &'a [u8]);
278
279impl<'a> Debug for Bytes<'a> {
280    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
281        write!(f, "{:#02x?}", self.0)
282    }
283}
284
285impl<'a> Display for Bytes<'a> {
286    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
287        write!(f, "{:#02x?}", self.0)
288    }
289}
290
291impl<'a> LowerHex for Bytes<'a> {
292    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
293        write!(f, "{:#02x?}", self.0)
294    }
295}
296
297#[cfg(feature = "defmt")]
298impl<'a> defmt::Format for Bytes<'a> {
299    fn format(&self, fmt: defmt::Formatter) {
300        defmt::write!(fmt, "{:02x}", self.0)
301    }
302}