]> code.octet-stream.net Git - hashgood/commitdiff
Give more friendly error messages if path is not a regular file
authorThomas Karpiniec <tom.karpiniec@outlook.com>
Thu, 25 Mar 2021 11:34:42 +0000 (22:34 +1100)
committerThomas Karpiniec <tom.karpiniec@outlook.com>
Thu, 25 Mar 2021 11:34:42 +0000 (22:34 +1100)
src/calculate.rs

index 870115c7e89c0300f0b525dd03fb80e8af0aff90..dc059b260d0d3e28188523e1cb4f04be04b6fc2e 100644 (file)
@@ -16,12 +16,27 @@ use std::thread::JoinHandle;
 pub type CalculateResult = Result<Vec<(Algorithm, Vec<u8>)>, Box<dyn Error>>;
 
 /// 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<dyn Read>, Box<dyn Error>> {
+pub fn get_input_reader(input: &PathBuf) -> Result<Box<dyn Read>, 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