diff --git a/src/cmp.rs b/src/cmp.rs index 8f14868..0175466 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -71,9 +71,7 @@ fn is_stdout_dev_null() -> bool { } pub fn parse_params>(mut opts: Peekable) -> Result { - let Some(executable) = opts.next() else { - return Err("Usage: ".to_string()); - }; + let executable = opts.next().ok_or("Usage: ".to_string())?; let executable_str = executable.to_string_lossy().to_string(); let parse_skip = |param: &str, skip_desc: &str| -> Result { @@ -293,27 +291,15 @@ fn prepare_reader( let mut reader: Box = if path == "-" { Box::new(BufReader::new(io::stdin())) } else { - match fs::File::open(path) { - Ok(file) => Box::new(BufReader::new(file)), - Err(e) => { - return Err(format_failure_to_read_input_file( - ¶ms.executable, - path, - &e, - )); - } - } + let file = fs::File::open(path) + .map_err(|e| format_failure_to_read_input_file(¶ms.executable, path, &e))?; + Box::new(BufReader::new(file)) }; if let Some(skip) = skip { // cast as u64 must remain, because value of IgnInit data type could be changed. - if let Err(e) = io::copy(&mut reader.by_ref().take(*skip), &mut io::sink()) { - return Err(format_failure_to_read_input_file( - ¶ms.executable, - path, - &e, - )); - } + io::copy(&mut reader.by_ref().take(*skip), &mut io::sink()) + .map_err(|e| format_failure_to_read_input_file(¶ms.executable, path, &e))?; } Ok(reader) @@ -360,27 +346,13 @@ pub fn cmp(params: &Params) -> Result { let mut compare = Cmp::Equal; loop { // Fill up our buffers. - let from_buf = match from.fill_buf() { - Ok(buf) => buf, - Err(e) => { - return Err(format_failure_to_read_input_file( - ¶ms.executable, - ¶ms.from, - &e, - )); - } - }; + let from_buf = from + .fill_buf() + .map_err(|e| format_failure_to_read_input_file(¶ms.executable, ¶ms.from, &e))?; - let to_buf = match to.fill_buf() { - Ok(buf) => buf, - Err(e) => { - return Err(format_failure_to_read_input_file( - ¶ms.executable, - ¶ms.to, - &e, - )); - } - }; + let to_buf = to + .fill_buf() + .map_err(|e| format_failure_to_read_input_file(¶ms.executable, ¶ms.to, &e))?; // Check for EOF conditions. if from_buf.is_empty() && to_buf.is_empty() {