pub enum ByteFormat<'a> {
Concise(&'a [u8]),
Nicely(&'a [u8]),
Hexdump(&'a [u8]),
}
Expand description
Display a byte string in a more humane manner.
The intended use for this enumeration is wrapping byte strings before
handing them off to one of Rust’s formatting macros. However, the low-level
ByteFormat::render
method, especially when combined with a Rewriter
instance, enables other use cases, too.
§Example
assert_eq!(
format!("{}", ByteFormat::Concise(b"\x1b[1m\x90@\xfe\x07")),
"␛[1m.@.␇"
);
assert_eq!(
format!("{}", ByteFormat::Nicely(b"\x1b[1m\x90@\xfe\x07")),
"‹ESC›[1m‹DCS›@「FE」‹BEL›"
);
assert_eq!(
format!("{}", ByteFormat::Hexdump(b"\x1b[1m\x90@\xfe\x07")),
"0000: 1b5b 316d 9040 fe07 ␛[1m.@.␇"
);
Variants§
Concise(&'a [u8])
The concise format uses one character per byte. It displays C0 control
codes with Unicode control pictures (which may be hard to read) and
replaces bytes larger than 0x7F with a period .
Nicely(&'a [u8])
The elaborate format uses more than one character per byte where
necessary. It displays C0 control codes as well as select C1 control
codes as mnemonics between guillemets, e.g., ‹ESC›
for 0x1B. It
displays bytes larger than 0x7F as hexadecimal numbers between corner
brackets, e.g., 「A0」
for 0xA0.
Hexdump(&'a [u8])
The hexdump format combines hexadecimal and concise formatting. Unlike the other formats, it is line-oriented, displaying up to 16 bytes per line.
Implementations§
Source§impl ByteFormat<'_>
impl ByteFormat<'_>
Sourcepub fn render<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, Error>
pub fn render<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, Error>
Render the bytes with the given writer.
This method largely is an implementation detail. It differs from the display trait by accepting arbitrary writers and by returning the number of characters (not bytes) written. It is public to support applications that require either of these features.
Since the hexdump format is line-oriented, it emits newlines for all but the last line. The number of characters written only covers that last line.