From f9f1d00194842b3fc344d1536353958f5ccbced1 Mon Sep 17 00:00:00 2001 From: schaeff Date: Thu, 16 Aug 2018 10:49:08 +0200 Subject: [PATCH] add error handling to fs resolver --- zokrates_fs_resolver/src/lib.rs | 85 +++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 10 deletions(-) diff --git a/zokrates_fs_resolver/src/lib.rs b/zokrates_fs_resolver/src/lib.rs index 12f85cda..2bbf832e 100644 --- a/zokrates_fs_resolver/src/lib.rs +++ b/zokrates_fs_resolver/src/lib.rs @@ -4,17 +4,82 @@ use std::fs::File; use std::io; pub fn resolve(location: &Option, source: &String) -> Result<(BufReader, String, String), io::Error> { - let path = match location { - Some(location) => { - PathBuf::from(location).join(PathBuf::from(source)) - }, - None => { - PathBuf::from(source) - } - }; + // the fs resolver has to be provided a location, as it supports relative paths + match location { + Some(location) => resolve_with_location(location, source), + None => Err(io::Error::new(io::ErrorKind::Other, "No location provided")) + } +} - let next_location = path.parent().unwrap().to_path_buf().into_os_string().into_string().unwrap(); - let alias = path.file_stem().unwrap().to_os_string().to_string_lossy().to_string(); +fn resolve_with_location(location: &String, source: &String) -> Result<(BufReader, String, String), io::Error> { + let path = PathBuf::from(location).join(PathBuf::from(source)); + let (next_location, alias) = generate_next_parameters(&path)?; File::open(path).and_then(|f| Ok((BufReader::new(f), next_location, alias))) +} + +fn generate_next_parameters(path: &PathBuf) -> Result<(String, String), io::Error> { + match (path.parent(), path.file_stem()) { + (Some(parent), Some(stem)) => Ok( + ( + parent.to_path_buf().into_os_string().into_string().unwrap(), + stem.to_os_string().to_string_lossy().to_string() + ) + ), + _ => Err(io::Error::new(io::ErrorKind::Other, "Invalid path")) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn valid_path_with_location() { + let (_, next_location, alias) = resolve(&Some(String::from("./src")), &String::from("lib.rs")).unwrap(); + assert_eq!(next_location, String::from("./src")); + assert_eq!(alias, String::from("lib")); + } + + #[test] + fn valid_path_without_location() { + let res = resolve(&None, &String::from("./src/lib.rs")); + assert!(res.is_err()); + } + + #[test] + fn non_existing_file() { + let res = resolve(&Some(String::from("./src")), &String::from("rubbish.rs")); + assert!(res.is_err()); + } + + #[test] + fn invalid_location() { + let res = resolve(&Some(String::from(",8!-$2abc")), &String::from("foo.code")); + assert!(res.is_err()); + } + + #[test] + fn invalid_file() { + let res = resolve(&Some(String::from("./src")), &String::from(",8!-$2abc")); + assert!(res.is_err()); + } + + #[test] + fn not_a_file() { + let res = resolve(&Some(String::from(".")), &String::from("/src/")); + assert!(res.is_err()); + } + + #[test] + fn no_parent() { + let res = resolve(&Some(String::from(".")), &String::from(".")); + assert!(res.is_err()); + } + + #[test] + fn no_file_name() { + let res = resolve(&Some(String::from(".")), &String::from("")); + assert!(res.is_err()); + } } \ No newline at end of file