mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-05 07:27:46 +00:00
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.
This commit is contained in:
parent
52cfd4c6cb
commit
229948ae45
8 changed files with 56 additions and 41 deletions
|
@ -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<dyn Read + 'a> {
|
||||
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<dyn Read>
|
||||
}
|
||||
None => {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -119,7 +119,7 @@ fn fold(filenames: Vec<String>, 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
|
||||
});
|
||||
|
||||
|
|
|
@ -469,7 +469,7 @@ where
|
|||
stdin_buf = stdin();
|
||||
Box::new(stdin_buf) as Box<dyn Read>
|
||||
} else {
|
||||
file_buf = safe_unwrap!(File::open(filename));
|
||||
file_buf = crash_if_err!(1, File::open(filename));
|
||||
Box::new(file_buf) as Box<dyn Read>
|
||||
});
|
||||
if options.check {
|
||||
|
@ -486,19 +486,25 @@ where
|
|||
} else {
|
||||
"+".to_string()
|
||||
};
|
||||
let gnu_re = safe_unwrap!(Regex::new(&format!(
|
||||
r"^(?P<digest>[a-fA-F0-9]{}) (?P<binary>[ \*])(?P<fileName>.*)",
|
||||
modifier,
|
||||
)));
|
||||
let bsd_re = safe_unwrap!(Regex::new(&format!(
|
||||
r"^{algorithm} \((?P<fileName>.*)\) = (?P<digest>[a-fA-F0-9]{digest_size})",
|
||||
algorithm = options.algoname,
|
||||
digest_size = modifier,
|
||||
)));
|
||||
let gnu_re = crash_if_err!(
|
||||
1,
|
||||
Regex::new(&format!(
|
||||
r"^(?P<digest>[a-fA-F0-9]{}) (?P<binary>[ \*])(?P<fileName>.*)",
|
||||
modifier,
|
||||
))
|
||||
);
|
||||
let bsd_re = crash_if_err!(
|
||||
1,
|
||||
Regex::new(&format!(
|
||||
r"^{algorithm} \((?P<fileName>.*)\) = (?P<digest>[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<dyn Read>);
|
||||
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 {
|
||||
|
|
|
@ -1362,8 +1362,8 @@ fn enter_directory(dir: &PathData, config: &Config, out: &mut BufWriter<Stdout>)
|
|||
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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue