custom_debug/
lib.rs

1#![no_std]
2use core::fmt;
3
4/// Alias of [Debug]
5pub use custom_debug_derive::Debug as CustomDebug;
6pub use custom_debug_derive::*;
7
8/// Formats a buffer as hex using \xNN notation.
9pub fn hexbuf(v: &impl AsRef<[u8]>, f: &mut fmt::Formatter) -> fmt::Result {
10    write!(f, "b\"")?;
11
12    for x in v.as_ref() {
13        write!(f, "\\x{:02x}", x)?;
14    }
15
16    write!(f, "\"")?;
17
18    Ok(())
19}
20
21/// Formats a buffer as hex using \xNN notation,
22/// except for printable ascii characters.
23pub fn hexbuf_str(v: &impl AsRef<[u8]>, f: &mut fmt::Formatter) -> fmt::Result {
24    write!(f, "b\"")?;
25
26    for x in v.as_ref() {
27        match x {
28            b'\\' => write!(f, "\\\\")?,
29            b'"' => write!(f, "\\\"")?,
30            b if b.is_ascii_graphic() => write!(f, "{}", *x as char)?,
31            _ => write!(f, "\\x{:02x}", x)?,
32        }
33    }
34
35    write!(f, "\"")?;
36
37    Ok(())
38}