From 3cee3de9005fe071530629c8f56dad69182143d1 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 23 Feb 2024 21:46:50 -0700 Subject: [PATCH] fop(1): committing to Rust error messages --- src/fop.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 09cac94..c9a767e 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -18,7 +18,7 @@ use std::{ env::args, - io::{ Read, stdin, Write }, + io::{ Read, stdin, stdout, Write }, process::{ Command, exit, Stdio }, }; @@ -54,8 +54,8 @@ fn main() { exit(EX_USAGE); }); - let index = argv[index_arg].parse::().unwrap_or_else(|_| { - eprintln!("{}: {}: Not an integer.", argv[0], argv[1]); + let index = argv[index_arg].parse::().unwrap_or_else(|e| { + eprintln!("{}: {}: {}.", argv[0], argv[1], e); exit(EX_DATAERR); }); @@ -74,8 +74,8 @@ fn main() { .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() - .unwrap_or_else( |_| { - eprintln!("{}: {}: Command not found.", argv[0], argv[command_arg]); + .unwrap_or_else( |e| { + eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e); exit(EX_UNAVAILABLE); }); @@ -83,7 +83,7 @@ fn main() { eprintln!( "{}: {}: No such index in input.", argv[0], - index.to_string() + index.to_string(), ); exit(EX_DATAERR); }); @@ -93,11 +93,8 @@ fn main() { drop(child_stdin); } - let output = spawned.wait_with_output().unwrap_or_else(|_| { - eprintln!("{}: {}: Command has no output.", - argv[0], - argv[command_arg] - ); + let output = spawned.wait_with_output().unwrap_or_else(|e| { + eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e); exit(EX_IOERR); }); @@ -105,16 +102,17 @@ fn main() { if replace.pop() != Some(b'\n') { replace = output.stdout; } - let new_field = String::from_utf8(replace).unwrap_or_else(|_| { - eprintln!( - "{}: {}: Command output is invalid UTF-8.", - argv[0], - argv[command_arg] - ); + let new_field = String::from_utf8(replace).unwrap_or_else(|e| { + eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e); exit(EX_IOERR); }); fields[index] = &new_field; - print!("{}", fields.join(&d.to_string())); + stdout().write_all( + fields.join(&d.to_string()).as_bytes() + ).unwrap_or_else(|e|{ + eprintln!("{}: {}.", argv[0], e); + exit(EX_IOERR); + }); }