From 18630e80189942b821fbe5502572acc21bb0543e Mon Sep 17 00:00:00 2001 From: Thomas Karpiniec Date: Thu, 25 Mar 2021 22:34:42 +1100 Subject: [PATCH 1/1] Give more friendly error messages if path is not a regular file --- src/calculate.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/calculate.rs b/src/calculate.rs index 870115c..dc059b2 100644 --- a/src/calculate.rs +++ b/src/calculate.rs @@ -16,12 +16,27 @@ use std::thread::JoinHandle; pub type CalculateResult = Result)>, Box>; /// For a given path to the input (may be "-" for STDIN), try to obtain a reader for the data within it. -pub fn get_input_reader(input: &PathBuf) -> Result, Box> { +pub fn get_input_reader(input: &PathBuf) -> Result, String> { if input.to_str() == Some("-") { // Special case: standard input return Ok(Box::new(std::io::stdin())); } - Ok(Box::new(File::open(input)?)) + if !input.exists() { + return Err(format!( + "The path '{}' does not exist.", + input.to_string_lossy() + )); + } + if !input.is_file() { + return Err(format!( + "The path '{}' is not a regular file.", + input.to_string_lossy() + )); + } + match File::open(input) { + Ok(f) => Ok(Box::new(f)), + Err(e) => Err(format!("File open: {}", e)), + } } /// For the given input stream, calculate all requested digest types -- 2.39.5