defmt_or_log/
macros.rs

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