Skip to content

Commit ccbbc5e

Browse files
oech3oech3
authored andcommitted
cmp: use .map_err
1 parent 1a8d7f9 commit ccbbc5e

1 file changed

Lines changed: 12 additions & 40 deletions

File tree

src/cmp.rs

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ fn is_stdout_dev_null() -> bool {
7171
}
7272

7373
pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Result<Params, String> {
74-
let Some(executable) = opts.next() else {
75-
return Err("Usage: <exe> <from> <to>".to_string());
76-
};
74+
let executable = opts.next().ok_or("Usage: <exe> <from> <to>".to_string())?;
7775
let executable_str = executable.to_string_lossy().to_string();
7876

7977
let parse_skip = |param: &str, skip_desc: &str| -> Result<SkipU64, String> {
@@ -293,27 +291,15 @@ fn prepare_reader(
293291
let mut reader: Box<dyn BufRead> = if path == "-" {
294292
Box::new(BufReader::new(io::stdin()))
295293
} else {
296-
match fs::File::open(path) {
297-
Ok(file) => Box::new(BufReader::new(file)),
298-
Err(e) => {
299-
return Err(format_failure_to_read_input_file(
300-
&params.executable,
301-
path,
302-
&e,
303-
));
304-
}
305-
}
294+
let file = fs::File::open(path)
295+
.map_err(|e| format_failure_to_read_input_file(&params.executable, path, &e))?;
296+
Box::new(BufReader::new(file))
306297
};
307298

308299
if let Some(skip) = skip {
309300
// cast as u64 must remain, because value of IgnInit data type could be changed.
310-
if let Err(e) = io::copy(&mut reader.by_ref().take(*skip), &mut io::sink()) {
311-
return Err(format_failure_to_read_input_file(
312-
&params.executable,
313-
path,
314-
&e,
315-
));
316-
}
301+
io::copy(&mut reader.by_ref().take(*skip), &mut io::sink())
302+
.map_err(|e| format_failure_to_read_input_file(&params.executable, path, &e))?;
317303
}
318304

319305
Ok(reader)
@@ -360,27 +346,13 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {
360346
let mut compare = Cmp::Equal;
361347
loop {
362348
// Fill up our buffers.
363-
let from_buf = match from.fill_buf() {
364-
Ok(buf) => buf,
365-
Err(e) => {
366-
return Err(format_failure_to_read_input_file(
367-
&params.executable,
368-
&params.from,
369-
&e,
370-
));
371-
}
372-
};
349+
let from_buf = from
350+
.fill_buf()
351+
.map_err(|e| format_failure_to_read_input_file(&params.executable, &params.from, &e))?;
373352

374-
let to_buf = match to.fill_buf() {
375-
Ok(buf) => buf,
376-
Err(e) => {
377-
return Err(format_failure_to_read_input_file(
378-
&params.executable,
379-
&params.to,
380-
&e,
381-
));
382-
}
383-
};
353+
let to_buf = to
354+
.fill_buf()
355+
.map_err(|e| format_failure_to_read_input_file(&params.executable, &params.to, &e))?;
384356

385357
// Check for EOF conditions.
386358
if from_buf.is_empty() && to_buf.is_empty() {

0 commit comments

Comments
 (0)