use dynamic dispatch to simplify file io a bit

This commit is contained in:
Connor Skees 2020-07-16 00:07:59 -04:00
parent d5d527ab70
commit fb724b8bee

View File

@ -192,20 +192,17 @@ fn main() -> std::io::Result<()> {
.unicode_error_messages(!matches.is_present("NO_UNICODE")) .unicode_error_messages(!matches.is_present("NO_UNICODE"))
.allows_charset(!matches.is_present("NO_CHARSET")); .allows_charset(!matches.is_present("NO_CHARSET"));
if let Some(name) = matches.value_of("INPUT") { let (mut stdout_write, mut file_write);
if let Some(path) = matches.value_of("OUTPUT") { let buf_out: &mut dyn Write = if let Some(path) = matches.value_of("OUTPUT") {
let mut buf = BufWriter::new(File::open(path).unwrap_or(File::create(path)?)); file_write = BufWriter::new(File::open(path).unwrap_or(File::create(path)?));
buf.write_all( &mut file_write
from_path(name, &options)
.unwrap_or_else(|e| {
eprintln!("{}", e);
std::process::exit(1)
})
.as_bytes(),
)?;
} else { } else {
let mut stdout = BufWriter::new(stdout()); stdout_write = BufWriter::new(stdout());
stdout.write_all( &mut stdout_write
};
if let Some(name) = matches.value_of("INPUT") {
buf_out.write_all(
from_path(name, &options) from_path(name, &options)
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
eprintln!("{}", e); eprintln!("{}", e);
@ -214,6 +211,5 @@ fn main() -> std::io::Result<()> {
.as_bytes(), .as_bytes(),
)?; )?;
} }
}
Ok(()) Ok(())
} }