diff --git a/src/expressions.rs b/src/expressions.rs index 9e799c1..16d6bba 100644 --- a/src/expressions.rs +++ b/src/expressions.rs @@ -2,9 +2,9 @@ /// EXPRESSIONS //////////////////////////////////////////////////////////////////////// +// An expression may be evaluated against a load order pub trait Expression { fn eval(&self, items: &[String]) -> bool; - fn as_expr(&self) -> EExpression; } #[derive(Clone)] @@ -15,6 +15,7 @@ pub enum EExpression { NOT(NOT), } +// pass-through impl EExpression { pub fn eval(&self, items: &[String]) -> bool { match self { @@ -25,6 +26,31 @@ impl EExpression { } } } +// conversions +impl From for EExpression { + fn from(val: Atomic) -> Self { + EExpression::Atomic(val) + } +} +impl From for EExpression { + fn from(val: ALL) -> Self { + EExpression::ALL(val) + } +} +impl From for EExpression { + fn from(val: ANY) -> Self { + EExpression::ANY(val) + } +} +impl From for EExpression { + fn from(val: NOT) -> Self { + EExpression::NOT(val) + } +} + +//////////////////////////////////////////////////////////////////////// +/// IMPLEMENTATIONS +//////////////////////////////////////////////////////////////////////// /// The atomic expression (EXISTS) /// atomics evaluate as true if the input list contains the item @@ -38,10 +64,6 @@ impl Expression for Atomic { // TODO wildcards items.contains(&self.item) } - - fn as_expr(&self) -> EExpression { - EExpression::Atomic(self.clone()) - } } impl From<&str> for Atomic { fn from(value: &str) -> Self { @@ -77,9 +99,6 @@ impl Expression for ALL { }); r } - fn as_expr(&self) -> EExpression { - EExpression::ALL(self.clone()) - } } /// The ANY expression @@ -105,10 +124,6 @@ impl Expression for ANY { }); r } - - fn as_expr(&self) -> EExpression { - EExpression::ANY(self.clone()) - } } /// The NOT expression @@ -136,7 +151,4 @@ impl Expression for NOT { fn eval(&self, items: &[String]) -> bool { !self.expression.eval(items) } - fn as_expr(&self) -> EExpression { - EExpression::NOT(self.clone()) - } } diff --git a/tests/unit_expression_tests.rs b/tests/unit_expression_tests.rs index 6fc2a04..6767a48 100644 --- a/tests/unit_expression_tests.rs +++ b/tests/unit_expression_tests.rs @@ -10,17 +10,11 @@ mod unit_expressions_tests { .collect(); // check that a and b exist in my load order - let mut expr = ALL::new(vec![ - EExpression::Atomic(Atomic::from("a")), - EExpression::Atomic(Atomic::from("b")), - ]); + let mut expr = ALL::new(vec![Atomic::from("a").into(), Atomic::from("b").into()]); assert!(expr.eval(&mods)); // check that a and x exist in my load order - expr = ALL::new(vec![ - EExpression::Atomic(Atomic::from("a")), - EExpression::Atomic(Atomic::from("x")), - ]); + expr = ALL::new(vec![Atomic::from("a").into(), Atomic::from("x").into()]); assert!(!expr.eval(&mods)); // should fail } @@ -32,17 +26,11 @@ mod unit_expressions_tests { .collect(); // check that a or x exist in my load order - let mut expr = ANY::new(vec![ - EExpression::Atomic(Atomic::from("a")), - EExpression::Atomic(Atomic::from("x")), - ]); + let mut expr = ANY::new(vec![Atomic::from("a").into(), Atomic::from("x").into()]); assert!(expr.eval(&mods)); // check that x or y exist in my load order - expr = ANY::new(vec![ - EExpression::Atomic(Atomic::from("y")), - EExpression::Atomic(Atomic::from("x")), - ]); + expr = ANY::new(vec![Atomic::from("y").into(), Atomic::from("x").into()]); assert!(!expr.eval(&mods)); // should fail } @@ -54,11 +42,11 @@ mod unit_expressions_tests { .collect(); // check that x is not present in my load order - let mut expr = NOT::new(EExpression::Atomic(Atomic::from("x"))); + let mut expr = NOT::new(Atomic::from("x").into()); assert!(expr.eval(&mods)); // check that a is not present in my load order - expr = NOT::new(EExpression::Atomic(Atomic::from("a"))); + expr = NOT::new(Atomic::from("a").into()); assert!(!expr.eval(&mods)); // should fail } @@ -71,51 +59,31 @@ mod unit_expressions_tests { // check that (a and x) are not present in the modlist { - let nested = ALL::new(vec![ - EExpression::Atomic(Atomic::from("a")), - EExpression::Atomic(Atomic::from("x")), - ]); - let expr = NOT::new(nested.as_expr()); + let nested = ALL::new(vec![Atomic::from("a").into(), Atomic::from("x").into()]); + let expr = NOT::new(nested.into()); assert!(expr.eval(&mods)); } // check that (a and b) are not present in the modlist { - let nested = ALL::new(vec![ - EExpression::Atomic(Atomic::from("a")), - EExpression::Atomic(Atomic::from("b")), - ]); - let expr = NOT::new(nested.as_expr()); + let nested = ALL::new(vec![Atomic::from("a").into(), Atomic::from("b").into()]); + let expr = NOT::new(nested.into()); assert!(!expr.eval(&mods)); // should fail } // check that (a and b) are present and that either (x and y) are not present { - let nested1 = ALL::new(vec![ - EExpression::Atomic(Atomic::from("a")), - EExpression::Atomic(Atomic::from("b")), - ]); - let nested2 = NOT::new( - ANY::new(vec![ - EExpression::Atomic(Atomic::from("x")), - EExpression::Atomic(Atomic::from("y")), - ]) - .as_expr(), - ); - let expr = ALL::new(vec![nested1.as_expr(), nested2.as_expr()]); + let nested1 = ALL::new(vec![Atomic::from("a").into(), Atomic::from("b").into()]); + let nested2 = + NOT::new(ANY::new(vec![Atomic::from("x").into(), Atomic::from("y").into()]).into()); + let expr = ALL::new(vec![nested1.into(), nested2.into()]); assert!(expr.eval(&mods)); } // check that (a and b) are present and that either (f and y) are present { - let nested1 = ALL::new(vec![ - EExpression::Atomic(Atomic::from("a")), - EExpression::Atomic(Atomic::from("b")), - ]); - let nested2 = ANY::new(vec![ - EExpression::Atomic(Atomic::from("f")), - EExpression::Atomic(Atomic::from("y")), - ]); - let expr = ALL::new(vec![nested1.as_expr(), nested2.as_expr()]); + let nested1 = ALL::new(vec![Atomic::from("a").into(), Atomic::from("b").into()]); + let nested2 = ANY::new(vec![Atomic::from("f").into(), Atomic::from("y").into()]); + let expr = ALL::new(vec![nested1.into(), nested2.into()]); assert!(expr.eval(&mods)); } } diff --git a/tests/unit_rules_tests.rs b/tests/unit_rules_tests.rs index 12d6a5d..348481d 100644 --- a/tests/unit_rules_tests.rs +++ b/tests/unit_rules_tests.rs @@ -13,7 +13,7 @@ mod unit_rules_tests { .iter() .map(|e| Note { comment: e.1.into(), - expression: EExpression::Atomic(Atomic { item: e.0.into() }), + expression: Atomic { item: e.0.into() }.into(), }) .collect(); @@ -36,13 +36,13 @@ mod unit_rules_tests { let rule1 = Conflict { comment: "a conflicts with b".into(), - expression_a: EExpression::Atomic(Atomic { item: "a".into() }), - expression_b: EExpression::Atomic(Atomic { item: "b".into() }), + expression_a: Atomic { item: "a".into() }.into(), + expression_b: Atomic { item: "b".into() }.into(), }; let rule2 = Conflict { comment: "b conflicts with x".into(), - expression_a: EExpression::Atomic(Atomic { item: "b".into() }), - expression_b: EExpression::Atomic(Atomic { item: "x".into() }), + expression_a: Atomic { item: "b".into() }.into(), + expression_b: Atomic { item: "x".into() }.into(), }; let rules: Vec = vec![ rule1, // a conflicts with a @@ -70,20 +70,20 @@ mod unit_rules_tests { // a requires b Require { comment: "a requires b".into(), - expression_a: EExpression::Atomic(Atomic { item: "a".into() }), - expression_b: EExpression::Atomic(Atomic { item: "b".into() }), + expression_a: Atomic { item: "a".into() }.into(), + expression_b: Atomic { item: "b".into() }.into(), }, // b requires x Require { comment: "b requires x".into(), - expression_a: EExpression::Atomic(Atomic { item: "b".into() }), - expression_b: EExpression::Atomic(Atomic { item: "x".into() }), + expression_a: Atomic { item: "b".into() }.into(), + expression_b: Atomic { item: "x".into() }.into(), }, // x requires y Require { comment: "x requires y".into(), - expression_a: EExpression::Atomic(Atomic { item: "x".into() }), - expression_b: EExpression::Atomic(Atomic { item: "y".into() }), + expression_a: Atomic { item: "x".into() }.into(), + expression_b: Atomic { item: "y".into() }.into(), }, ]; diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs index bb8437b..ea65315 100644 --- a/tests/unit_tests.rs +++ b/tests/unit_tests.rs @@ -80,7 +80,7 @@ mod unit_tests { .iter() .map(|e| Note { comment: e.1.into(), - expression: EExpression::Atomic(Atomic { item: e.0.into() }), + expression: Atomic { item: e.0.into() }.into(), }) .collect(); @@ -104,13 +104,13 @@ mod unit_tests { let rules: Vec = vec![ Conflict { comment: "some a".into(), - expression_a: EExpression::Atomic(Atomic { item: "a".into() }), - expression_b: EExpression::Atomic(Atomic { item: "b".into() }), + expression_a: Atomic { item: "a".into() }.into(), + expression_b: Atomic { item: "b".into() }.into(), }, Conflict { comment: "some b".into(), - expression_a: EExpression::Atomic(Atomic { item: "b".into() }), - expression_b: EExpression::Atomic(Atomic { item: "x".into() }), + expression_a: Atomic { item: "b".into() }.into(), + expression_b: Atomic { item: "x".into() }.into(), }, ];