From 229948ae45110faca23b293e293dab0aab836f62 Mon Sep 17 00:00:00 2001 From: Andreas Hartmann Date: Mon, 23 Aug 2021 11:58:38 +0200 Subject: [PATCH] uu: Replace `safe_unwrap` with `crash_if_err` Unify the usage of macros `safe_unwrap` and `crash_if_err` that are identical to each other except for the assumption of a default error code. Use the more generic `crash_if_err` so that `safe_unwrap` is now obsolete and can be removed. --- src/uu/base32/src/base_common.rs | 2 +- src/uu/expand/src/expand.rs | 13 ++++--- src/uu/fold/src/fold.rs | 2 +- src/uu/hashsum/src/hashsum.rs | 60 +++++++++++++++++++------------- src/uu/ls/src/ls.rs | 4 +-- src/uu/pinky/src/pinky.rs | 2 +- src/uu/unexpand/src/unexpand.rs | 12 +++---- src/uu/who/src/who.rs | 2 +- 8 files changed, 56 insertions(+), 41 deletions(-) diff --git a/src/uu/base32/src/base_common.rs b/src/uu/base32/src/base_common.rs index 615093311..78962d9db 100644 --- a/src/uu/base32/src/base_common.rs +++ b/src/uu/base32/src/base_common.rs @@ -122,7 +122,7 @@ pub fn base_app<'a>(name: &str, version: &'a str, about: &'a str) -> App<'static pub fn get_input<'a>(config: &Config, stdin_ref: &'a Stdin) -> Box { match &config.to_read { Some(name) => { - let file_buf = safe_unwrap!(File::open(Path::new(name))); + let file_buf = crash_if_err!(1, File::open(Path::new(name))); Box::new(BufReader::new(file_buf)) // as Box } None => { diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index 04cb42649..f5a5686f6 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -329,12 +329,15 @@ fn expand(options: Options) { // now dump out either spaces if we're expanding, or a literal tab if we're not if init || !options.iflag { if nts <= options.tspaces.len() { - safe_unwrap!(output.write_all(options.tspaces[..nts].as_bytes())); + crash_if_err!( + 1, + output.write_all(options.tspaces[..nts].as_bytes()) + ); } else { - safe_unwrap!(output.write_all(" ".repeat(nts).as_bytes())); + crash_if_err!(1, output.write_all(" ".repeat(nts).as_bytes())); }; } else { - safe_unwrap!(output.write_all(&buf[byte..byte + nbytes])); + crash_if_err!(1, output.write_all(&buf[byte..byte + nbytes])); } } _ => { @@ -352,14 +355,14 @@ fn expand(options: Options) { init = false; } - safe_unwrap!(output.write_all(&buf[byte..byte + nbytes])); + crash_if_err!(1, output.write_all(&buf[byte..byte + nbytes])); } } byte += nbytes; // advance the pointer } - safe_unwrap!(output.flush()); + crash_if_err!(1, output.flush()); buf.truncate(0); // clear the buffer } } diff --git a/src/uu/fold/src/fold.rs b/src/uu/fold/src/fold.rs index c5628125d..c4cc16469 100644 --- a/src/uu/fold/src/fold.rs +++ b/src/uu/fold/src/fold.rs @@ -119,7 +119,7 @@ fn fold(filenames: Vec, bytes: bool, spaces: bool, width: usize) { stdin_buf = stdin(); &mut stdin_buf as &mut dyn Read } else { - file_buf = safe_unwrap!(File::open(Path::new(filename))); + file_buf = crash_if_err!(1, File::open(Path::new(filename))); &mut file_buf as &mut dyn Read }); diff --git a/src/uu/hashsum/src/hashsum.rs b/src/uu/hashsum/src/hashsum.rs index 384ef1388..f308da300 100644 --- a/src/uu/hashsum/src/hashsum.rs +++ b/src/uu/hashsum/src/hashsum.rs @@ -469,7 +469,7 @@ where stdin_buf = stdin(); Box::new(stdin_buf) as Box } else { - file_buf = safe_unwrap!(File::open(filename)); + file_buf = crash_if_err!(1, File::open(filename)); Box::new(file_buf) as Box }); if options.check { @@ -486,19 +486,25 @@ where } else { "+".to_string() }; - let gnu_re = safe_unwrap!(Regex::new(&format!( - r"^(?P[a-fA-F0-9]{}) (?P[ \*])(?P.*)", - modifier, - ))); - let bsd_re = safe_unwrap!(Regex::new(&format!( - r"^{algorithm} \((?P.*)\) = (?P[a-fA-F0-9]{digest_size})", - algorithm = options.algoname, - digest_size = modifier, - ))); + let gnu_re = crash_if_err!( + 1, + Regex::new(&format!( + r"^(?P[a-fA-F0-9]{}) (?P[ \*])(?P.*)", + modifier, + )) + ); + let bsd_re = crash_if_err!( + 1, + Regex::new(&format!( + r"^{algorithm} \((?P.*)\) = (?P[a-fA-F0-9]{digest_size})", + algorithm = options.algoname, + digest_size = modifier, + )) + ); let buffer = file; for (i, line) in buffer.lines().enumerate() { - let line = safe_unwrap!(line); + let line = crash_if_err!(1, line); let (ck_filename, sum, binary_check) = match gnu_re.captures(&line) { Some(caps) => ( caps.name("fileName").unwrap().as_str(), @@ -528,14 +534,17 @@ where } }, }; - let f = safe_unwrap!(File::open(ck_filename)); + let f = crash_if_err!(1, File::open(ck_filename)); let mut ckf = BufReader::new(Box::new(f) as Box); - let real_sum = safe_unwrap!(digest_reader( - &mut *options.digest, - &mut ckf, - binary_check, - options.output_bits - )) + let real_sum = crash_if_err!( + 1, + digest_reader( + &mut *options.digest, + &mut ckf, + binary_check, + options.output_bits + ) + ) .to_ascii_lowercase(); if sum == real_sum { if !options.quiet { @@ -549,12 +558,15 @@ where } } } else { - let sum = safe_unwrap!(digest_reader( - &mut *options.digest, - &mut file, - options.binary, - options.output_bits - )); + let sum = crash_if_err!( + 1, + digest_reader( + &mut *options.digest, + &mut file, + options.binary, + options.output_bits + ) + ); if options.tag { println!("{} ({}) = {}", options.algoname, filename.display(), sum); } else { diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index da8955152..3d957474c 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -1362,8 +1362,8 @@ fn enter_directory(dir: &PathData, config: &Config, out: &mut BufWriter) vec![] }; - let mut temp: Vec<_> = safe_unwrap!(fs::read_dir(&dir.p_buf)) - .map(|res| safe_unwrap!(res)) + let mut temp: Vec<_> = crash_if_err!(1, fs::read_dir(&dir.p_buf)) + .map(|res| crash_if_err!(1, res)) .filter(|e| should_display(e, config)) .map(|e| PathData::new(DirEntry::path(&e), Some(e.file_type()), None, config, false)) .collect(); diff --git a/src/uu/pinky/src/pinky.rs b/src/uu/pinky/src/pinky.rs index 02573c994..4aa27affa 100644 --- a/src/uu/pinky/src/pinky.rs +++ b/src/uu/pinky/src/pinky.rs @@ -291,7 +291,7 @@ impl Pinky { let mut s = ut.host(); if self.include_where && !s.is_empty() { - s = safe_unwrap!(ut.canon_host()); + s = crash_if_err!(1, ut.canon_host()); print!(" {}", s); } diff --git a/src/uu/unexpand/src/unexpand.rs b/src/uu/unexpand/src/unexpand.rs index cb8541048..7fb9b2590 100644 --- a/src/uu/unexpand/src/unexpand.rs +++ b/src/uu/unexpand/src/unexpand.rs @@ -178,13 +178,13 @@ fn write_tabs( break; } - safe_unwrap!(output.write_all(b"\t")); + crash_if_err!(1, output.write_all(b"\t")); scol += nts; } } while col > scol { - safe_unwrap!(output.write_all(b" ")); + crash_if_err!(1, output.write_all(b" ")); scol += 1; } } @@ -272,7 +272,7 @@ fn unexpand(options: Options) { init, true, ); - safe_unwrap!(output.write_all(&buf[byte..])); + crash_if_err!(1, output.write_all(&buf[byte..])); scol = col; break; } @@ -292,7 +292,7 @@ fn unexpand(options: Options) { }; if !tabs_buffered { - safe_unwrap!(output.write_all(&buf[byte..byte + nbytes])); + crash_if_err!(1, output.write_all(&buf[byte..byte + nbytes])); scol = col; // now printed up to this column } } @@ -317,7 +317,7 @@ fn unexpand(options: Options) { } else { 0 }; - safe_unwrap!(output.write_all(&buf[byte..byte + nbytes])); + crash_if_err!(1, output.write_all(&buf[byte..byte + nbytes])); scol = col; // we've now printed up to this column } } @@ -336,7 +336,7 @@ fn unexpand(options: Options) { init, true, ); - safe_unwrap!(output.flush()); + crash_if_err!(1, output.flush()); buf.truncate(0); // clear out the buffer } } diff --git a/src/uu/who/src/who.rs b/src/uu/who/src/who.rs index b0ef7b3fa..d61747127 100644 --- a/src/uu/who/src/who.rs +++ b/src/uu/who/src/who.rs @@ -492,7 +492,7 @@ impl Who { }; let s = if self.do_lookup { - safe_unwrap!(ut.canon_host()) + crash_if_err!(1, ut.canon_host()) } else { ut.host() };