@@ -18,6 +18,28 @@ pub enum Error {
18
18
UnknownVariable ,
19
19
}
20
20
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
+
21
43
impl fmt:: Display for Error {
22
44
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
23
45
match self {
@@ -312,19 +334,12 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
312
334
// basic operators
313
335
let value = match function {
314
336
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 ) ?;
321
340
let product_rule = Rule :: Value ( product) ;
322
341
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) ?
328
343
}
329
344
Function :: Div ( first_rule, second_rule) => {
330
345
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>
683
698
Some ( value)
684
699
}
685
700
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 ( ) ?;
693
705
Some ( Value :: Bool ( !is_equal) )
694
706
}
695
707
Function :: Intersects ( first_rule, second_rule) => {
@@ -711,30 +723,22 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
711
723
Some ( Value :: Bool ( b. contains ( & a) ) )
712
724
}
713
725
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 ( ) ?;
721
730
Some ( Value :: Bool ( !is_in) )
722
731
}
723
732
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 ( ) ?;
738
742
Some ( Value :: Bool ( is_gte_start && is_lte_end) )
739
743
}
740
744
Function :: At ( first_rule, second_rule) => {
@@ -801,11 +805,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
801
805
. ok_or ( Error :: TypeError ) ?
802
806
. try_bool ( ) ?;
803
807
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) ?
809
809
}
810
810
Function :: Do ( first_rule) => eval ( input, output, first_rule) ?,
811
811
Function :: Set ( key, rule) => {
0 commit comments