diff --git a/src/function/eval.rs b/src/function/eval.rs new file mode 100644 index 0000000..077e075 --- /dev/null +++ b/src/function/eval.rs @@ -0,0 +1,81 @@ +use ::Error; +use super::Function; + +impl Function { + pub fn eval(&self) -> Result { + // TODO: Handle all variants. + // TODO: Paths may not lead outside game directory. + match *self { + Function::FilePath(ref f) => Ok(f.exists()), + _ => Ok(false), + } + } +} + +#[cfg(test)] +mod tests { + use function::Function; + + use std::path::PathBuf; + + #[test] + fn function_file_path_eval_should_return_true_if_the_file_exists_relative_to_the_data_path() { + let function = Function::FilePath(PathBuf::from("Cargo.toml")); + + assert!(function.eval().unwrap()); + + unimplemented!("not yet any way to actually specify the data path"); + } + + #[test] + fn function_file_path_eval_should_return_true_if_given_a_plugin_that_is_ghosted() { + let function = Function::FilePath(PathBuf::from("test.esp")); + + assert!(function.eval().unwrap()); + + unimplemented!("need to add tempdir and create a test.esp.ghost"); + } + + #[test] + #[allow(non_snake_case)] + fn function_file_path_eval_should_be_true_if_given_LOOT() { + unimplemented!(); + } + + #[test] + fn function_file_path_eval_should_not_check_for_ghosted_non_plugin_file() { + unimplemented!(); + } + + #[test] + fn function_file_path_eval_should_error_if_the_path_is_outside_game_directory() { + unimplemented!("to do"); + } + + #[test] + fn function_file_path_eval_should_return_false_if_the_file_does_not_exist() { + let function = Function::FilePath(PathBuf::from("missing")); + + assert!(!function.eval().unwrap()); + } + + #[test] + fn function_file_regex_eval_should_error_if_the_path_is_outside_game_directory() { + unimplemented!(); + } + + #[test] + fn function_file_regex_eval_should_be_false_if_no_directory_entries_match() { + unimplemented!(); + } + + #[test] + fn function_file_regex_eval_should_be_false_if_the_parent_path_part_is_not_a_directory() { + unimplemented!(); + } + + #[test] + fn function_file_regex_eval_should_be_true_if_a_directory_entry_matches() { + unimplemented!(); + } +} diff --git a/src/function/mod.rs b/src/function/mod.rs new file mode 100644 index 0000000..b0fe44b --- /dev/null +++ b/src/function/mod.rs @@ -0,0 +1,28 @@ +use std::path::PathBuf; + +use regex::Regex; + +pub mod eval; +pub mod parse; + +#[derive(Debug, PartialEq, Eq)] +pub enum ComparisonOperator { + Equal, + NotEqual, + LessThan, + GreaterThan, + LessThanOrEqual, + GreaterThanOrEqual, +} + +#[derive(Debug)] +pub enum Function { + FilePath(PathBuf), + FileRegex(Regex), + ActivePath(PathBuf), + ActiveRegex(Regex), + Many(Regex), + ManyActive(Regex), + Checksum(PathBuf, u32), + Version(PathBuf, String, ComparisonOperator), +} diff --git a/src/function.rs b/src/function/parse.rs similarity index 74% rename from src/function.rs rename to src/function/parse.rs index 3bfb485..a4aeb4a 100644 --- a/src/function.rs +++ b/src/function/parse.rs @@ -4,17 +4,7 @@ use std::str; use nom::{hex_digit, Context, Err, ErrorKind, IResult}; use regex::Regex; -use super::Error; - -#[derive(Debug, PartialEq, Eq)] -pub enum ComparisonOperator { - Equal, - NotEqual, - LessThan, - GreaterThan, - LessThanOrEqual, - GreaterThanOrEqual, -} +use super::{ComparisonOperator, Function}; impl ComparisonOperator { pub fn parse(input: &str) -> IResult<&str, ComparisonOperator> { @@ -33,18 +23,6 @@ impl ComparisonOperator { } } -#[derive(Debug)] -pub enum Function { - FilePath(PathBuf), - FileRegex(Regex), - ActivePath(PathBuf), - ActiveRegex(Regex), - Many(Regex), - ManyActive(Regex), - Checksum(PathBuf, u32), - Version(PathBuf, String, ComparisonOperator), -} - const INVALID_PATH_CHARS: &str = "\":*?<>|\\"; // \ is treated as invalid to distinguish regex strings. const INVALID_REGEX_PATH_CHARS: &str = "\"<>"; @@ -92,15 +70,6 @@ fn parse_checksum_args(input: &str) -> IResult<&str, (PathBuf, u32)> { } impl Function { - pub fn eval(&self) -> Result { - // TODO: Handle all variants. - // TODO: Paths may not lead outside game directory. - match *self { - Function::FilePath(ref f) => Ok(f.exists()), - _ => Ok(false), - } - } - pub fn parse(input: &str) -> IResult<&str, Function> { do_parse!( input, @@ -241,68 +210,4 @@ mod tests { _ => panic!("Expected a checksum function"), } } - - #[test] - fn function_file_path_eval_should_return_true_if_the_file_exists_relative_to_the_data_path() { - let function = Function::FilePath(PathBuf::from("Cargo.toml")); - - assert!(function.eval().unwrap()); - - unimplemented!("not yet any way to actually specify the data path"); - } - - #[test] - fn function_file_path_eval_should_return_true_if_given_a_plugin_that_is_ghosted() { - let function = Function::FilePath(PathBuf::from("test.esp")); - - assert!(function.eval().unwrap()); - - unimplemented!("need to add tempdir and create a test.esp.ghost"); - } - - #[test] - fn function_file_path_eval_should_be_true_if_given_LOOT() { - unimplemented!(); - } - - #[test] - fn function_file_path_eval_should_not_check_for_ghosted_non_plugin_file() { - unimplemented!(); - } - - #[test] - fn function_file_path_eval_should_error_if_the_path_is_outside_game_directory() { - unimplemented!("to do"); - } - - #[test] - fn function_file_path_eval_should_return_false_if_the_file_does_not_exist() { - let function = Function::FilePath(PathBuf::from("missing")); - - assert!(!function.eval().unwrap()); - } - - #[test] - fn function_file_regex_eval_should_error_if_the_path_is_outside_game_directory() { - unimplemented!(); - } - - #[test] - fn function_file_regex_eval_should_be_false_if_no_directory_entries_match() { - unimplemented!(); - } - - #[test] - fn function_file_regex_eval_should_be_false_if_the_parent_path_part_is_not_a_directory() { - unimplemented!(); - } - - #[test] - fn function_file_regex_eval_should_be_true_if_a_directory_entry_matches() { - unimplemented!(); - } - - #[test] - fn function_active_path_eval_should_be_false_if - }