From bd08ef479ce8732fc7dbdaedf026e3331016bb00 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 23 Feb 2024 21:58:23 -0700 Subject: [PATCH] hru(1): error handling tweaks --- src/hru.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/hru.rs b/src/hru.rs index 26f3343..2540fb8 100644 --- a/src/hru.rs +++ b/src/hru.rs @@ -19,13 +19,13 @@ use std::{ cmp::Ordering, env::args, - io::stdin, - process::ExitCode, + io::{ stdin, stdout, Write }, + process::{ ExitCode, exit }, }; extern crate sysexits; -use sysexits::{ EX_DATAERR, EX_SOFTWARE }; +use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE }; const LIST: [(u32, &str); 10] = [ (3, "k"), @@ -79,7 +79,7 @@ fn main() -> ExitCode { f }, Err(err) => { - eprintln!("{}: {}", argv[0], err); + eprintln!("{}: {}.", argv[0], err); return ExitCode::from(EX_DATAERR as u8); }, }; @@ -87,7 +87,7 @@ fn main() -> ExitCode { let (number, prefix) = match convert(n) { Ok(x) => x, Err(err) => { - eprintln!("{}: {}", argv[0], err); + eprintln!("{}: {}.", argv[0], err); return ExitCode::from(EX_SOFTWARE as u8); }, }; @@ -96,7 +96,11 @@ fn main() -> ExitCode { let out = ((number * 10.0).round() / 10.0).to_string(); - println!("{} {}", out, si_prefix); + stdout().write_all(format!("{} {}", out, si_prefix).as_bytes()) + .unwrap_or_else(|e| { + eprintln!("{}: {}.", argv[0], e); + exit(EX_IOERR); + }); } ExitCode::SUCCESS