diff --git a/main.rs b/main.rs index 5ed41fc..ebce1ae 100644 --- a/main.rs +++ b/main.rs @@ -22,6 +22,18 @@ fn print_error(error: Box) { writeln!(io::stderr(), "hexcat: {}", error.description()).expect("could not write to stderr"); } +fn print_file(file: fs::File) { + let buf_reader = io::BufReader::new(file); + for byte in buf_reader.bytes() { + write!(io::stdout(), "\\x").expect("could not write to stdout"); + let mut byte_hex = format!("{:x}", byte.expect("could not read byte")); + if byte_hex.len() == 1 { + byte_hex.insert(0, '0'); + } + write!(io::stdout(), "{}", byte_hex).expect("could not write to stdout"); + } +} + fn main() { let matches = clap::App::new("hexcat") .version(crate_version!()) @@ -42,15 +54,7 @@ fn main() { continue; } }; - let buf_reader = io::BufReader::new(file); - for byte in buf_reader.bytes() { - write!(io::stdout(), "\\x").expect("could not write to stdout"); - let mut byte_hex = format!("{:x}", byte.expect("could not read byte")); - if byte_hex.len() == 1 { - byte_hex.insert(0, '0'); - } - write!(io::stdout(), "{}", byte_hex).expect("could not write to stdout"); - } + print_file(file); } // TODO: don't print newline if nothing was written write!(io::stdout(), "\n").expect("could not write to stdout");