mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-05 23:47: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> {
|
pub fn get_input<'a>(config: &Config, stdin_ref: &'a Stdin) -> Box<dyn Read + 'a> {
|
||||||
match &config.to_read {
|
match &config.to_read {
|
||||||
Some(name) => {
|
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>
|
Box::new(BufReader::new(file_buf)) // as Box<dyn Read>
|
||||||
}
|
}
|
||||||
None => {
|
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
|
// now dump out either spaces if we're expanding, or a literal tab if we're not
|
||||||
if init || !options.iflag {
|
if init || !options.iflag {
|
||||||
if nts <= options.tspaces.len() {
|
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 {
|
} else {
|
||||||
safe_unwrap!(output.write_all(" ".repeat(nts).as_bytes()));
|
crash_if_err!(1, output.write_all(" ".repeat(nts).as_bytes()));
|
||||||
};
|
};
|
||||||
} else {
|
} 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;
|
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
|
byte += nbytes; // advance the pointer
|
||||||
}
|
}
|
||||||
|
|
||||||
safe_unwrap!(output.flush());
|
crash_if_err!(1, output.flush());
|
||||||
buf.truncate(0); // clear the buffer
|
buf.truncate(0); // clear the buffer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,7 @@ fn fold(filenames: Vec<String>, bytes: bool, spaces: bool, width: usize) {
|
||||||
stdin_buf = stdin();
|
stdin_buf = stdin();
|
||||||
&mut stdin_buf as &mut dyn Read
|
&mut stdin_buf as &mut dyn Read
|
||||||
} else {
|
} 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
|
&mut file_buf as &mut dyn Read
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -469,7 +469,7 @@ where
|
||||||
stdin_buf = stdin();
|
stdin_buf = stdin();
|
||||||
Box::new(stdin_buf) as Box<dyn Read>
|
Box::new(stdin_buf) as Box<dyn Read>
|
||||||
} else {
|
} 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>
|
Box::new(file_buf) as Box<dyn Read>
|
||||||
});
|
});
|
||||||
if options.check {
|
if options.check {
|
||||||
|
@ -486,19 +486,25 @@ where
|
||||||
} else {
|
} else {
|
||||||
"+".to_string()
|
"+".to_string()
|
||||||
};
|
};
|
||||||
let gnu_re = safe_unwrap!(Regex::new(&format!(
|
let gnu_re = crash_if_err!(
|
||||||
r"^(?P<digest>[a-fA-F0-9]{}) (?P<binary>[ \*])(?P<fileName>.*)",
|
1,
|
||||||
modifier,
|
Regex::new(&format!(
|
||||||
)));
|
r"^(?P<digest>[a-fA-F0-9]{}) (?P<binary>[ \*])(?P<fileName>.*)",
|
||||||
let bsd_re = safe_unwrap!(Regex::new(&format!(
|
modifier,
|
||||||
r"^{algorithm} \((?P<fileName>.*)\) = (?P<digest>[a-fA-F0-9]{digest_size})",
|
))
|
||||||
algorithm = options.algoname,
|
);
|
||||||
digest_size = 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;
|
let buffer = file;
|
||||||
for (i, line) in buffer.lines().enumerate() {
|
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) {
|
let (ck_filename, sum, binary_check) = match gnu_re.captures(&line) {
|
||||||
Some(caps) => (
|
Some(caps) => (
|
||||||
caps.name("fileName").unwrap().as_str(),
|
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 mut ckf = BufReader::new(Box::new(f) as Box<dyn Read>);
|
||||||
let real_sum = safe_unwrap!(digest_reader(
|
let real_sum = crash_if_err!(
|
||||||
&mut *options.digest,
|
1,
|
||||||
&mut ckf,
|
digest_reader(
|
||||||
binary_check,
|
&mut *options.digest,
|
||||||
options.output_bits
|
&mut ckf,
|
||||||
))
|
binary_check,
|
||||||
|
options.output_bits
|
||||||
|
)
|
||||||
|
)
|
||||||
.to_ascii_lowercase();
|
.to_ascii_lowercase();
|
||||||
if sum == real_sum {
|
if sum == real_sum {
|
||||||
if !options.quiet {
|
if !options.quiet {
|
||||||
|
@ -549,12 +558,15 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let sum = safe_unwrap!(digest_reader(
|
let sum = crash_if_err!(
|
||||||
&mut *options.digest,
|
1,
|
||||||
&mut file,
|
digest_reader(
|
||||||
options.binary,
|
&mut *options.digest,
|
||||||
options.output_bits
|
&mut file,
|
||||||
));
|
options.binary,
|
||||||
|
options.output_bits
|
||||||
|
)
|
||||||
|
);
|
||||||
if options.tag {
|
if options.tag {
|
||||||
println!("{} ({}) = {}", options.algoname, filename.display(), sum);
|
println!("{} ({}) = {}", options.algoname, filename.display(), sum);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1362,8 +1362,8 @@ fn enter_directory(dir: &PathData, config: &Config, out: &mut BufWriter<Stdout>)
|
||||||
vec![]
|
vec![]
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut temp: Vec<_> = safe_unwrap!(fs::read_dir(&dir.p_buf))
|
let mut temp: Vec<_> = crash_if_err!(1, fs::read_dir(&dir.p_buf))
|
||||||
.map(|res| safe_unwrap!(res))
|
.map(|res| crash_if_err!(1, res))
|
||||||
.filter(|e| should_display(e, config))
|
.filter(|e| should_display(e, config))
|
||||||
.map(|e| PathData::new(DirEntry::path(&e), Some(e.file_type()), None, config, false))
|
.map(|e| PathData::new(DirEntry::path(&e), Some(e.file_type()), None, config, false))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
@ -291,7 +291,7 @@ impl Pinky {
|
||||||
|
|
||||||
let mut s = ut.host();
|
let mut s = ut.host();
|
||||||
if self.include_where && !s.is_empty() {
|
if self.include_where && !s.is_empty() {
|
||||||
s = safe_unwrap!(ut.canon_host());
|
s = crash_if_err!(1, ut.canon_host());
|
||||||
print!(" {}", s);
|
print!(" {}", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -178,13 +178,13 @@ fn write_tabs(
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
safe_unwrap!(output.write_all(b"\t"));
|
crash_if_err!(1, output.write_all(b"\t"));
|
||||||
scol += nts;
|
scol += nts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while col > scol {
|
while col > scol {
|
||||||
safe_unwrap!(output.write_all(b" "));
|
crash_if_err!(1, output.write_all(b" "));
|
||||||
scol += 1;
|
scol += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,7 +272,7 @@ fn unexpand(options: Options) {
|
||||||
init,
|
init,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
safe_unwrap!(output.write_all(&buf[byte..]));
|
crash_if_err!(1, output.write_all(&buf[byte..]));
|
||||||
scol = col;
|
scol = col;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -292,7 +292,7 @@ fn unexpand(options: Options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
if !tabs_buffered {
|
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
|
scol = col; // now printed up to this column
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -317,7 +317,7 @@ fn unexpand(options: Options) {
|
||||||
} else {
|
} else {
|
||||||
0
|
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
|
scol = col; // we've now printed up to this column
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -336,7 +336,7 @@ fn unexpand(options: Options) {
|
||||||
init,
|
init,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
safe_unwrap!(output.flush());
|
crash_if_err!(1, output.flush());
|
||||||
buf.truncate(0); // clear out the buffer
|
buf.truncate(0); // clear out the buffer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -492,7 +492,7 @@ impl Who {
|
||||||
};
|
};
|
||||||
|
|
||||||
let s = if self.do_lookup {
|
let s = if self.do_lookup {
|
||||||
safe_unwrap!(ut.canon_host())
|
crash_if_err!(1, ut.canon_host())
|
||||||
} else {
|
} else {
|
||||||
ut.host()
|
ut.host()
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue