1
#![macro_use]
2
#![allow(unused_macros)]
3

            
4
use core::fmt::{Debug, Display, LowerHex};
5

            
6
#[collapse_debuginfo(yes)]
7
macro_rules! assert {
8
    ($($x:tt)*) => {
9
        {
10
            cfg_select! {
11
                feature = "defmt" => {
12
                    ::defmt::assert!($($x)*);
13
                }
14
                _ => {
15
                    ::core::assert!($($x)*);
16
                }
17
            }
18
        }
19
    };
20
}
21

            
22
#[collapse_debuginfo(yes)]
23
macro_rules! assert_eq {
24
    ($($x:tt)*) => {
25
        {
26
            cfg_select! {
27
                feature = "defmt" => {
28
                    ::defmt::assert_eq!($($x)*);
29
                }
30
                _ => {
31
                    ::core::assert_eq!($($x)*);
32
                }
33
            }
34
        }
35
    };
36
}
37

            
38
#[collapse_debuginfo(yes)]
39
macro_rules! assert_ne {
40
    ($($x:tt)*) => {
41
        {
42
            cfg_select! {
43
                feature = "defmt" => {
44
                    ::defmt::assert_ne!($($x)*);
45
                }
46
                _ => {
47
                    ::core::assert_ne!($($x)*);
48
                }
49
            }
50
        }
51
    };
52
}
53

            
54
#[collapse_debuginfo(yes)]
55
macro_rules! debug_assert {
56
    ($($x:tt)*) => {
57
        {
58
            cfg_select! {
59
                feature = "defmt" => {
60
                    ::defmt::debug_assert!($($x)*);
61
                }
62
                _ => {
63
                    ::core::debug_assert!($($x)*);
64
                }
65
            }
66
        }
67
    };
68
}
69

            
70
#[collapse_debuginfo(yes)]
71
macro_rules! debug_assert_eq {
72
    ($($x:tt)*) => {
73
        {
74
            cfg_select! {
75
                feature = "defmt" => {
76
                    ::defmt::debug_assert_eq!($($x)*);
77
                }
78
                _ => {
79
                    ::core::debug_assert_eq!($($x)*);
80
                }
81
            }
82
        }
83
    };
84
}
85

            
86
#[collapse_debuginfo(yes)]
87
macro_rules! debug_assert_ne {
88
    ($($x:tt)*) => {
89
        {
90
            cfg_select! {
91
                feature = "defmt" => {
92
                    ::defmt::debug_assert_ne!($($x)*);
93
                }
94
                _ => {
95
                    ::core::debug_assert_ne!($($x)*);
96
                }
97
            }
98
        }
99
    };
100
}
101

            
102
#[collapse_debuginfo(yes)]
103
macro_rules! todo {
104
    ($($x:tt)*) => {
105
        {
106
            cfg_select! {
107
                feature = "defmt" => {
108
                    ::defmt::todo!($($x)*);
109
                }
110
                _ => {
111
                    ::core::todo!($($x)*);
112
                }
113
            }
114
        }
115
    };
116
}
117

            
118
#[collapse_debuginfo(yes)]
119
macro_rules! unreachable {
120
    ($($x:tt)*) => {
121
        {
122
            cfg_select! {
123
                feature = "defmt" => {
124
                    ::defmt::unreachable!($($x)*);
125
                }
126
                _ => {
127
                    ::core::unreachable!($($x)*);
128
                }
129
            }
130
        }
131
    };
132
}
133

            
134
#[collapse_debuginfo(yes)]
135
macro_rules! panic {
136
    ($($x:tt)*) => {
137
        {
138
            cfg_select! {
139
                feature = "defmt" => {
140
                    ::defmt::panic!($($x)*);
141
                }
142
                _ => {
143
                    ::core::panic!($($x)*);
144
                }
145
            }
146
        }
147
    };
148
}
149

            
150
#[collapse_debuginfo(yes)]
151
macro_rules! trace {
152
    ($s:literal $(, $x:expr)* $(,)?) => {
153
        {
154
            cfg_select! {
155
                feature = "defmt" => {
156
                    ::defmt::trace!($s $(, $x)*);
157
                }
158
                feature = "log-04" => {
159
                    ::log_04::trace!($s $(, $x)*);
160
                }
161
                _ => {
162
                    let _ = ($( & $x ),*);
163
                }
164
            }
165
        }
166
    };
167
}
168

            
169
#[collapse_debuginfo(yes)]
170
macro_rules! debug {
171
    ($s:literal $(, $x:expr)* $(,)?) => {
172
        {
173
            cfg_select! {
174
                feature = "defmt" => {
175
                    ::defmt::debug!($s $(, $x)*);
176
                }
177
                feature = "log-04" => {
178
                    ::log_04::debug!($s $(, $x)*);
179
                }
180
                _ => {
181
                    let _ = ($( & $x ),*);
182
                }
183
            }
184
        }
185
    };
186
}
187

            
188
#[collapse_debuginfo(yes)]
189
macro_rules! info {
190
    ($s:literal $(, $x:expr)* $(,)?) => {
191
        {
192
            cfg_select! {
193
                feature = "defmt" => {
194
                    ::defmt::info!($s $(, $x)*);
195
                }
196
                feature = "log-04" => {
197
                    ::log_04::info!($s $(, $x)*);
198
                }
199
                _ => {
200
                    let _ = ($( & $x ),*);
201
                }
202
            }
203
        }
204
    };
205
}
206

            
207
#[collapse_debuginfo(yes)]
208
macro_rules! warn {
209
    ($s:literal $(, $x:expr)* $(,)?) => {
210
        {
211
            cfg_select! {
212
                feature = "defmt" => {
213
                    ::defmt::warn!($s $(, $x)*);
214
                }
215
                feature = "log-04" => {
216
                    ::log_04::warn!($s $(, $x)*);
217
                }
218
                _ => {
219
                    let _ = ($( & $x ),*);
220
                }
221
            }
222
        }
223
    };
224
}
225

            
226
#[collapse_debuginfo(yes)]
227
macro_rules! error {
228
    ($s:literal $(, $x:expr)* $(,)?) => {
229
        {
230
            cfg_select! {
231
                feature = "defmt" => {
232
                    ::defmt::error!($s $(, $x)*);
233
                }
234
                feature = "log-04" => {
235
                    ::log_04::error!($s $(, $x)*);
236
                }
237
                _ => {
238
                    let _ = ($( & $x ),*);
239
                }
240
            }
241
        }
242
    };
243
}
244

            
245
#[cfg(feature = "defmt")]
246
#[collapse_debuginfo(yes)]
247
macro_rules! unwrap {
248
    ($($x:tt)*) => {
249
        ::defmt::unwrap!($($x)*)
250
    };
251
}
252

            
253
#[cold]
254
#[inline(never)]
255
#[cfg(not(feature = "defmt"))]
256
pub(crate) fn __unwrap_failed(arg: &str, e: impl ::core::fmt::Debug) -> ! {
257
    ::core::panic!("unwrap of `{}` failed: {:?}", arg, e);
258
}
259

            
260
#[cold]
261
#[inline(never)]
262
#[cfg(not(feature = "defmt"))]
263
pub(crate) fn __unwrap_failed_with_message(
264
    arg: &str,
265
    e: impl core::fmt::Debug,
266
    msg: impl core::fmt::Display,
267
) -> ! {
268
    ::core::panic!("unwrap of `{}` failed: {}: {:?}", arg, msg, e);
269
}
270

            
271
#[cfg(not(feature = "defmt"))]
272
#[collapse_debuginfo(yes)]
273
macro_rules! unwrap {
274
    ($arg:expr) => {
275
        match $crate::fmt::Try::into_result($arg) {
276
            ::core::result::Result::Ok(t) => t,
277
            ::core::result::Result::Err(e) => { $crate::fmt::__unwrap_failed(::core::stringify!($arg), e) }
278
        }
279
    };
280
    ($arg:expr, $msg:expr) => {
281
        match $crate::fmt::Try::into_result($arg) {
282
            ::core::result::Result::Ok(t) => t,
283
            ::core::result::Result::Err(e) => { $crate::fmt::__unwrap_failed_with_message(::core::stringify!($arg), e, $msg) }
284
        }
285
    };
286
    ($arg:expr, $($msg:expr),+ $(,)? ) => {
287
        match $crate::fmt::Try::into_result($arg) {
288
            ::core::result::Result::Ok(t) => t,
289
            ::core::result::Result::Err(e) => { $crate::fmt::__unwrap_failed_with_message(::core::stringify!($arg), e, ::core::format_args!($($msg,)*)) }
290
        }
291
    }
292
}
293

            
294
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
295
pub struct NoneError;
296

            
297
pub trait Try {
298
    type Ok;
299
    type Error;
300
    #[allow(unused)]
301
    fn into_result(self) -> Result<Self::Ok, Self::Error>;
302
}
303

            
304
impl<T> Try for Option<T> {
305
    type Ok = T;
306
    type Error = NoneError;
307

            
308
    #[inline]
309
    fn into_result(self) -> Result<T, NoneError> {
310
        self.ok_or(NoneError)
311
    }
312
}
313

            
314
impl<T, E> Try for Result<T, E> {
315
    type Ok = T;
316
    type Error = E;
317

            
318
    #[inline]
319
    fn into_result(self) -> Self {
320
270
        self
321
    }
322
}
323

            
324
/// A way to `{:x?}` format a byte slice which is compatible with `defmt`
325
#[allow(unused)]
326
pub(crate) struct Bytes<'a>(pub &'a [u8]);
327

            
328
impl Debug for Bytes<'_> {
329
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
330
        write!(f, "{:#02x?}", self.0)
331
    }
332
}
333

            
334
impl Display for Bytes<'_> {
335
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
336
        write!(f, "{:#02x?}", self.0)
337
    }
338
}
339

            
340
impl LowerHex for Bytes<'_> {
341
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
342
        write!(f, "{:#02x?}", self.0)
343
    }
344
}
345

            
346
#[cfg(feature = "defmt")]
347
impl defmt::Format for Bytes<'_> {
348
    fn format(&self, fmt: defmt::Formatter<'_>) {
349
        defmt::write!(fmt, "{:02x}", self.0)
350
    }
351
}