mirror of
https://github.com/loot/loot-condition-interpreter.git
synced 2026-07-27 14:16:09 -07:00
Split function parsing and evaluation code into separate files
This commit is contained in:
@@ -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!();
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
}
|
||||
@@ -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
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user