add error handling to fs resolver
This commit is contained in:
parent
e048001ce8
commit
f9f1d00194
1 changed files with 75 additions and 10 deletions
|
@ -4,17 +4,82 @@ use std::fs::File;
|
|||
use std::io;
|
||||
|
||||
pub fn resolve(location: &Option<String>, source: &String) -> Result<(BufReader<File>, 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<File>, 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());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue