Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rfuzzo committed Dec 8, 2023
1 parent 908b7a0 commit 5f2f459
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 80 deletions.
42 changes: 27 additions & 15 deletions src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -15,6 +15,7 @@ pub enum EExpression {
NOT(NOT),
}

// pass-through
impl EExpression {
pub fn eval(&self, items: &[String]) -> bool {
match self {
Expand All @@ -25,6 +26,31 @@ impl EExpression {
}
}
}
// conversions
impl From<Atomic> for EExpression {
fn from(val: Atomic) -> Self {
EExpression::Atomic(val)
}
}
impl From<ALL> for EExpression {
fn from(val: ALL) -> Self {
EExpression::ALL(val)
}
}
impl From<ANY> for EExpression {
fn from(val: ANY) -> Self {
EExpression::ANY(val)
}
}
impl From<NOT> 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
Expand All @@ -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 {
Expand Down Expand Up @@ -77,9 +99,6 @@ impl Expression for ALL {
});
r
}
fn as_expr(&self) -> EExpression {
EExpression::ALL(self.clone())
}
}

/// The ANY expression
Expand All @@ -105,10 +124,6 @@ impl Expression for ANY {
});
r
}

fn as_expr(&self) -> EExpression {
EExpression::ANY(self.clone())
}
}

/// The NOT expression
Expand Down Expand Up @@ -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())
}
}
66 changes: 17 additions & 49 deletions tests/unit_expression_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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));
}
}
Expand Down
22 changes: 11 additions & 11 deletions tests/unit_rules_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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<Conflict> = vec![
rule1, // a conflicts with a
Expand Down Expand Up @@ -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(),
},
];

Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -104,13 +104,13 @@ mod unit_tests {
let rules: Vec<Conflict> = 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(),
},
];

Expand Down

0 comments on commit 5f2f459

Please sign in to comment.