1
0
Fork 0

hru(1): error handling tweaks

This commit is contained in:
Emma Tebibyte 2024-02-23 21:58:23 -07:00
parent ee3877b607
commit bd08ef479c
Signed by untrusted user: emma
GPG Key ID: 06FA419A1698C270
1 changed files with 10 additions and 6 deletions

View File

@ -19,13 +19,13 @@
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
env::args, env::args,
io::stdin, io::{ stdin, stdout, Write },
process::ExitCode, process::{ ExitCode, exit },
}; };
extern crate sysexits; extern crate sysexits;
use sysexits::{ EX_DATAERR, EX_SOFTWARE }; use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE };
const LIST: [(u32, &str); 10] = [ const LIST: [(u32, &str); 10] = [
(3, "k"), (3, "k"),
@ -79,7 +79,7 @@ fn main() -> ExitCode {
f f
}, },
Err(err) => { Err(err) => {
eprintln!("{}: {}", argv[0], err); eprintln!("{}: {}.", argv[0], err);
return ExitCode::from(EX_DATAERR as u8); return ExitCode::from(EX_DATAERR as u8);
}, },
}; };
@ -87,7 +87,7 @@ fn main() -> ExitCode {
let (number, prefix) = match convert(n) { let (number, prefix) = match convert(n) {
Ok(x) => x, Ok(x) => x,
Err(err) => { Err(err) => {
eprintln!("{}: {}", argv[0], err); eprintln!("{}: {}.", argv[0], err);
return ExitCode::from(EX_SOFTWARE as u8); return ExitCode::from(EX_SOFTWARE as u8);
}, },
}; };
@ -96,7 +96,11 @@ fn main() -> ExitCode {
let out = ((number * 10.0).round() / 10.0).to_string(); 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 ExitCode::SUCCESS