Split function parsing and evaluation code into separate files

This commit is contained in:
Oliver Hamlet
2018-10-03 17:15:34 +01:00
parent 28af316ecc
commit 11fef635d3
3 changed files with 110 additions and 96 deletions
+81
View File
@@ -0,0 +1,81 @@
use ::Error;
use super::Function;
impl Function {
pub fn eval(&self) -> Result<bool, Error> {
// 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!();
}
}
+28
View File
@@ -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),
}
+1 -96
View File
@@ -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<bool, Error> {
// 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
}