From e6aad9505547574c267232057528917b81f4306c Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 3 Jun 2024 20:17:00 +0200 Subject: [PATCH] cksum/hashsum: move the file mgmt into a function --- src/uucore/src/lib/features/checksum.rs | 36 +++++++++++++------------ 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/uucore/src/lib/features/checksum.rs b/src/uucore/src/lib/features/checksum.rs index a2b341b40..9be3d6f56 100644 --- a/src/uucore/src/lib/features/checksum.rs +++ b/src/uucore/src/lib/features/checksum.rs @@ -358,6 +358,24 @@ fn get_expected_checksum( } } +fn get_input_file(filename_input: &OsStr, input_is_stdin: bool) -> UResult> { + if input_is_stdin { + Ok(Box::new(stdin())) // Use stdin if "-" is specified + } else { + match File::open(filename_input) { + Ok(f) => Ok(Box::new(f)), + Err(_) => Err(io::Error::new( + io::ErrorKind::Other, + format!( + "{}: No such file or directory", + filename_input.to_string_lossy() + ), + ) + .into()), + } + } +} + /*** * Do the checksum validation (can be strict or not) */ @@ -386,23 +404,7 @@ where let mut properly_formatted = false; let input_is_stdin = filename_input == OsStr::new("-"); - let file: Box = if input_is_stdin { - Box::new(stdin()) // Use stdin if "-" is specified - } else { - match File::open(filename_input) { - Ok(f) => Box::new(f), - Err(_) => { - return Err(io::Error::new( - io::ErrorKind::Other, - format!( - "{}: No such file or directory", - filename_input.to_string_lossy() - ), - ) - .into()); - } - } - }; + let file: Box = get_input_file(filename_input, input_is_stdin)?; let reader = BufReader::new(file); let lines: Vec = reader.lines().collect::>()?;