Skip to content

Commit 3ac3edc

Browse files
committed
Added trait to simplify calling eval
1 parent 0a23ae8 commit 3ac3edc

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

primitives/src/targeting/eval.rs

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ pub enum Error {
1818
UnknownVariable,
1919
}
2020

21+
trait Eval {
22+
fn eval(self, input: &Input, output: &mut Output) -> Result<Option<Value>, Error>;
23+
}
24+
25+
impl Eval for Value {
26+
fn eval(self, input: &Input, output: &mut Output) -> Result<Option<Value>, Error> {
27+
eval(input, output, &Rule::Value(self))
28+
}
29+
}
30+
31+
impl Eval for Function {
32+
fn eval(self, input: &Input, output: &mut Output) -> Result<Option<Value>, Error> {
33+
eval(input, output, &Rule::Function(self))
34+
}
35+
}
36+
37+
impl Eval for &Rule {
38+
fn eval(self, input: &Input, output: &mut Output) -> Result<Option<Value>, Error> {
39+
eval(input, output, self)
40+
}
41+
}
42+
2143
impl fmt::Display for Error {
2244
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2345
match self {
@@ -312,19 +334,12 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
312334
// basic operators
313335
let value = match function {
314336
Function::MulDiv(first_rule, second_rule, third_rule) => {
315-
let product = eval(
316-
input,
317-
output,
318-
&Rule::Function(Function::Mul(first_rule.clone(), second_rule.clone())),
319-
)?
320-
.ok_or(Error::TypeError)?;
337+
let product = Function::Mul(first_rule.clone(), second_rule.clone())
338+
.eval(input, output)?
339+
.ok_or(Error::TypeError)?;
321340
let product_rule = Rule::Value(product);
322341
let boxed_rule = Box::new(product_rule);
323-
eval(
324-
input,
325-
output,
326-
&Rule::Function(Function::Div(boxed_rule, third_rule.clone())),
327-
)?
342+
Function::Div(boxed_rule, third_rule.clone()).eval(input, output)?
328343
}
329344
Function::Div(first_rule, second_rule) => {
330345
let first_eval = first_rule.eval(input, output)?.ok_or(Error::TypeError)?;
@@ -683,13 +698,10 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
683698
Some(value)
684699
}
685700
Function::Neq(first_rule, second_rule) => {
686-
let is_equal = eval(
687-
input,
688-
output,
689-
&Rule::Function(Function::Eq(first_rule.clone(), second_rule.clone())),
690-
)?
691-
.ok_or(Error::TypeError)?
692-
.try_bool()?;
701+
let is_equal = Function::Eq(first_rule.clone(), second_rule.clone())
702+
.eval(input, output)?
703+
.ok_or(Error::TypeError)?
704+
.try_bool()?;
693705
Some(Value::Bool(!is_equal))
694706
}
695707
Function::Intersects(first_rule, second_rule) => {
@@ -711,30 +723,22 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
711723
Some(Value::Bool(b.contains(&a)))
712724
}
713725
Function::Nin(first_rule, second_rule) => {
714-
let is_in = eval(
715-
input,
716-
output,
717-
&Rule::Function(Function::In(first_rule.clone(), second_rule.clone())),
718-
)?
719-
.ok_or(Error::TypeError)?
720-
.try_bool()?;
726+
let is_in = Function::In(first_rule.clone(), second_rule.clone())
727+
.eval(input, output)?
728+
.ok_or(Error::TypeError)?
729+
.try_bool()?;
721730
Some(Value::Bool(!is_in))
722731
}
723732
Function::Between(first_rule, second_rule, third_rule) => {
724-
let is_gte_start = eval(
725-
input,
726-
output,
727-
&Rule::Function(Function::Gte(first_rule.clone(), second_rule.clone())),
728-
)?
729-
.ok_or(Error::TypeError)?
730-
.try_bool()?;
731-
let is_lte_end = eval(
732-
input,
733-
output,
734-
&Rule::Function(Function::Lte(first_rule.clone(), third_rule.clone())),
735-
)?
736-
.ok_or(Error::TypeError)?
737-
.try_bool()?;
733+
let is_gte_start = Function::Gte(first_rule.clone(), second_rule.clone())
734+
.eval(input, output)?
735+
.ok_or(Error::TypeError)?
736+
.try_bool()?;
737+
738+
let is_lte_end = Function::Lte(first_rule.clone(), third_rule.clone())
739+
.eval(input, output)?
740+
.ok_or(Error::TypeError)?
741+
.try_bool()?;
738742
Some(Value::Bool(is_gte_start && is_lte_end))
739743
}
740744
Function::At(first_rule, second_rule) => {
@@ -801,11 +805,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
801805
.ok_or(Error::TypeError)?
802806
.try_bool()?;
803807
let new_rule = Box::new(Rule::Value(Value::Bool(first_eval)));
804-
eval(
805-
input,
806-
output,
807-
&Rule::Function(Function::Set(String::from("show"), new_rule)),
808-
)?
808+
Function::Set(String::from("show"), new_rule).eval(input, output)?
809809
}
810810
Function::Do(first_rule) => eval(input, output, first_rule)?,
811811
Function::Set(key, rule) => {

0 commit comments

Comments
 (0)