mirror of
https://github.com/loot/loot-condition-interpreter.git
synced 2026-07-27 14:16:09 -07:00
Implement Display trait on Expression and all types it depends on
This commit is contained in:
+92
@@ -13,6 +13,7 @@ mod version;
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::ffi::OsStr;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str;
|
||||
@@ -116,6 +117,13 @@ impl Expression {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Expression {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let strings: Vec<String> = self.0.iter().map(CompoundCondition::to_string).collect();
|
||||
write!(f, "{}", strings.join(" or "))
|
||||
}
|
||||
}
|
||||
|
||||
// Conditions joined by 'and'
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct CompoundCondition(Vec<Condition>);
|
||||
@@ -139,6 +147,13 @@ impl CompoundCondition {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CompoundCondition {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let strings: Vec<String> = self.0.iter().map(Condition::to_string).collect();
|
||||
write!(f, "{}", strings.join(" and "))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
enum Condition {
|
||||
Function(Function),
|
||||
@@ -174,6 +189,17 @@ impl Condition {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Condition {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use Condition::*;
|
||||
match self {
|
||||
Function(function) => write!(f, "{}", function),
|
||||
InvertedFunction(function) => write!(f, "not {}", function),
|
||||
Expression(e) => write!(f, "({})", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -478,6 +504,30 @@ mod tests {
|
||||
assert!(condition.eval(&state).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn condition_fmt_should_format_function_correctly() {
|
||||
let condition = Condition::Function(Function::FilePath(PathBuf::from("Cargo.toml")));
|
||||
|
||||
assert_eq!("file(\"Cargo.toml\")", &format!("{}", condition));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn condition_fmt_should_format_inverted_function_correctly() {
|
||||
let condition =
|
||||
Condition::InvertedFunction(Function::FilePath(PathBuf::from("Cargo.toml")));
|
||||
|
||||
assert_eq!("not file(\"Cargo.toml\")", &format!("{}", condition));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn condition_fmt_should_format_expression_correctly() {
|
||||
let condition = Condition::Expression(Expression(vec![CompoundCondition(vec![
|
||||
Condition::Function(Function::FilePath(PathBuf::from("Cargo.toml"))),
|
||||
])]));
|
||||
|
||||
assert_eq!("(file(\"Cargo.toml\"))", &format!("{}", condition));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compound_condition_eval_should_be_true_if_all_conditions_are_true() {
|
||||
let state = state(".");
|
||||
@@ -502,6 +552,25 @@ mod tests {
|
||||
assert!(!compound_condition.eval(&state).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compound_condition_fmt_should_format_correctly() {
|
||||
let compound_condition = CompoundCondition(vec![
|
||||
Condition::Function(Function::FilePath(PathBuf::from("Cargo.toml"))),
|
||||
Condition::Function(Function::FilePath(PathBuf::from("missing"))),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
"file(\"Cargo.toml\") and file(\"missing\")",
|
||||
&format!("{}", compound_condition)
|
||||
);
|
||||
|
||||
let compound_condition = CompoundCondition(vec![Condition::Function(Function::FilePath(
|
||||
PathBuf::from("Cargo.toml"),
|
||||
))]);
|
||||
|
||||
assert_eq!("file(\"Cargo.toml\")", &format!("{}", compound_condition));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expression_eval_should_be_true_if_any_compound_condition_is_true() {
|
||||
let state = state(".");
|
||||
@@ -531,4 +600,27 @@ mod tests {
|
||||
]);
|
||||
assert!(!expression.eval(&state).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expression_fmt_should_format_correctly() {
|
||||
let expression = Expression(vec![
|
||||
CompoundCondition(vec![Condition::Function(Function::FilePath(
|
||||
PathBuf::from("Cargo.toml"),
|
||||
))]),
|
||||
CompoundCondition(vec![Condition::Function(Function::FilePath(
|
||||
PathBuf::from("missing"),
|
||||
))]),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
"file(\"Cargo.toml\") or file(\"missing\")",
|
||||
&format!("{}", expression)
|
||||
);
|
||||
|
||||
let expression = Expression(vec![CompoundCondition(vec![Condition::Function(
|
||||
Function::FilePath(PathBuf::from("Cargo.toml")),
|
||||
)])]);
|
||||
|
||||
assert_eq!("file(\"Cargo.toml\")", &format!("{}", expression));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user