dezoomify_rs/
binary_display.rs1use std::fmt;
2
3pub struct BinaryDisplay<'a>(pub &'a [u8]);
8
9impl fmt::Display for BinaryDisplay<'_> {
10 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11 write!(f, "b'")?;
12
13 for &byte in self.0 {
14 match byte {
15 0x20..=0x7e => write!(f, "{}", byte as char)?,
17 _ => write!(f, "\\x{byte:02x}")?,
19 }
20 }
21
22 write!(f, "'")
23 }
24}
25
26impl fmt::Debug for BinaryDisplay<'_> {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 fmt::Display::fmt(self, f)
29 }
30}
31
32pub fn display_bytes<T: AsRef<[u8]> + ?Sized>(bytes: &T) -> BinaryDisplay<'_> {
34 BinaryDisplay(bytes.as_ref())
35}