diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-04-11 13:33:35 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-04-12 12:41:51 +0200 |
commit | 24c637f1d665c1dfeafc05f214ab33c9a7f26e8a (patch) | |
tree | 613884870be126eab818884f311f20574c073aaa /tool | |
parent | ecfae7e26d9b1bef7a35d671bce93a469ad28ac3 (diff) |
tool: Turn HexDumper into an io::Write filter.
Diffstat (limited to 'tool')
-rw-r--r-- | tool/src/commands/dump.rs | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/tool/src/commands/dump.rs b/tool/src/commands/dump.rs index 2858b332..d5601dd8 100644 --- a/tool/src/commands/dump.rs +++ b/tool/src/commands/dump.rs @@ -412,10 +412,11 @@ impl PacketDumper { if let Some(map) = map { writeln!(output)?; - let mut hd = HexDumper::new(""); + let mut hd = HexDumper::new(output, ""); for field in map.iter() { - hd.write(output, field.data, field.name)?; + hd.write(field.data, field.name)?; } + let output = hd.into_inner(); writeln!(output)?; } else { writeln!(output, "{}", i)?; @@ -560,60 +561,67 @@ impl PacketDumper { } } -pub struct HexDumper { +pub struct HexDumper<W: io::Write> { + inner: W, indent: String, offset: usize, } -impl HexDumper { - pub fn new<I: AsRef<str>>(indent: I) -> Self { +impl<W: io::Write> HexDumper<W> { + pub fn new<I: AsRef<str>>(inner: W, indent: I) -> Self { HexDumper { + inner: inner, indent: indent.as_ref().into(), offset: 0, } } - pub fn write(&mut self, sink: &mut io::Write, buf: &[u8], msg: &str) - -> Result<()> { + /// Returns the inner writer. + pub fn into_inner(self) -> W { + self.inner + } + + pub fn write(&mut self, buf: &[u8], msg: &str) -> Result<()> { let mut msg_printed = false; - write!(sink, "{}{:08x} ", self.indent, self.offset)?; + write!(self.inner, "{}{:08x} ", self.indent, self.offset)?; for i in 0 .. self.offset % 16 { if i != 7 { - write!(sink, " ")?; + write!(self.inner, " ")?; } else { - write!(sink, " ")?; + write!(self.inner, " ")?; } } for c in buf { - write!(sink, "{:02x} ", c)?; + write!(self.inner, "{:02x} ", c)?; self.offset += 1; match self.offset % 16 { 0 => { if ! msg_printed { - write!(sink, " {}", msg)?; + write!(self.inner, " {}", msg)?; msg_printed = true; } - write!(sink, "\n{}{:08x} ", self.indent, self.offset)?; + write!(self.inner, + "\n{}{:08x} ", self.indent, self.offset)?; }, - 8 => write!(sink, " ")?, + 8 => write!(self.inner, " ")?, _ => (), } } for i in self.offset % 16 .. 16 { if i != 7 { - write!(sink, " ")?; + write!(self.inner, " ")?; } else { - write!(sink, " ")?; + write!(self.inner, " ")?; } } if ! msg_printed { - write!(sink, " {}", msg)?; + write!(self.inner, " {}", msg)?; } - writeln!(sink)?; + writeln!(self.inner)?; Ok(()) } } |