@@ -88,6 +88,7 @@ impl TryFrom<SerdeValue> for Value {
88
88
#[ serde( rename_all = "camelCase" ) ]
89
89
// TODO: https://github.com/AdExNetwork/adex-validator-stack-rust/issues/296
90
90
pub enum Function {
91
+ /// Multiplies first two values and then divides product by third value
91
92
MulDiv ( Box < Rule > , Box < Rule > , Box < Rule > ) ,
92
93
Div ( Box < Rule > , Box < Rule > ) ,
93
94
Mul ( Box < Rule > , Box < Rule > ) ,
@@ -103,14 +104,23 @@ pub enum Function {
103
104
Or ( Box < Rule > , Box < Rule > ) ,
104
105
Xor ( Box < Rule > , Box < Rule > ) ,
105
106
Not ( Box < Rule > ) ,
107
+ /// Is the first value Lesser than second value
106
108
Lt ( Box < Rule > , Box < Rule > ) ,
109
+ /// Is the first value Lesser than or equal to the second value
107
110
Lte ( Box < Rule > , Box < Rule > ) ,
111
+ /// Is the first value Greater than second value
108
112
Gt ( Box < Rule > , Box < Rule > ) ,
113
+ /// Is the first value Greater than or equal to the second value
109
114
Gte ( Box < Rule > , Box < Rule > ) ,
110
- Equals ( Box < Rule > , Box < Rule > ) ,
111
- NotEquals ( Box < Rule > , Box < Rule > ) ,
115
+ /// Are values equal
116
+ Eq ( Box < Rule > , Box < Rule > ) ,
117
+ /// Are values NOT equal
118
+ Neq ( Box < Rule > , Box < Rule > ) ,
119
+ /// Is first value included in an array (second value)
112
120
In ( Box < Rule > , Box < Rule > ) ,
121
+ /// Is first value NOT included in an array (second value)
113
122
NotIn ( Box < Rule > , Box < Rule > ) ,
123
+ /// Gets the element at a certain position (second value) of an array (first value)
114
124
At ( Box < Rule > , Box < Rule > ) ,
115
125
/// Note: this is inclusive of the start and end value
116
126
Between ( Box < Rule > , Box < Rule > , Box < Rule > ) ,
@@ -119,6 +129,7 @@ pub enum Function {
119
129
EndsWith ( Box < Rule > , Box < Rule > ) ,
120
130
OnlyShowIf ( Box < Rule > ) ,
121
131
Intersects ( Box < Rule > , Box < Rule > ) ,
132
+ /// Evaluates rule
122
133
Do ( Box < Rule > ) ,
123
134
Get ( String ) ,
124
135
/// Output variables can be set any number of times by different rules, except `show`
@@ -537,7 +548,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
537
548
. ok_or ( Error :: TypeError ) ?
538
549
. try_bool ( ) ?;
539
550
540
- Some ( Value :: Bool ( ( a && !b ) || ( !a && b ) ) )
551
+ Some ( Value :: Bool ( a ^ b ) )
541
552
}
542
553
Function :: Not ( first_rule) => {
543
554
let a = eval ( input, output, first_rule) ?
@@ -638,7 +649,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
638
649
639
650
Some ( value)
640
651
}
641
- Function :: Equals ( first_rule, second_rule) => {
652
+ Function :: Eq ( first_rule, second_rule) => {
642
653
let first_eval = first_rule. eval ( input, output) ?. ok_or ( Error :: TypeError ) ?;
643
654
let second_eval = second_rule. eval ( input, output) ?. ok_or ( Error :: TypeError ) ?;
644
655
@@ -654,7 +665,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
654
665
Value :: Bool ( lhs_bignum. eq ( & rhs_bignum) )
655
666
}
656
667
( Value :: Number ( lhs) , Value :: Number ( rhs) ) => {
657
- Value :: Bool ( compare_numbers ( lhs, rhs, ComparisonOperator :: Equals ) ?)
668
+ Value :: Bool ( compare_numbers ( lhs, rhs, ComparisonOperator :: Eq ) ?)
658
669
}
659
670
( Value :: Bool ( lhs) , Value :: Bool ( rhs) ) => Value :: Bool ( lhs == rhs) ,
660
671
( Value :: String ( lhs) , Value :: String ( rhs) ) => Value :: Bool ( lhs == rhs) ,
@@ -671,38 +682,15 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
671
682
672
683
Some ( value)
673
684
}
674
- Function :: NotEquals ( first_rule, second_rule) => {
675
- let first_eval = first_rule. eval ( input, output) ?. ok_or ( Error :: TypeError ) ?;
676
- let second_eval = second_rule. eval ( input, output) ?. ok_or ( Error :: TypeError ) ?;
677
-
678
- let value = match ( first_eval, second_eval) {
679
- ( Value :: BigNum ( bignum) , rhs_value) => {
680
- let rhs_bignum = BigNum :: try_from ( rhs_value) ?;
681
-
682
- Value :: Bool ( bignum. ne ( & rhs_bignum) )
683
- }
684
- ( lhs_value, Value :: BigNum ( rhs_bignum) ) => {
685
- let lhs_bignum = BigNum :: try_from ( lhs_value) ?;
686
-
687
- Value :: Bool ( lhs_bignum. ne ( & rhs_bignum) )
688
- }
689
- ( Value :: Number ( lhs) , Value :: Number ( rhs) ) => {
690
- Value :: Bool ( compare_numbers ( lhs, rhs, ComparisonOperator :: NotEquals ) ?)
691
- }
692
- ( Value :: Bool ( lhs) , Value :: Bool ( rhs) ) => Value :: Bool ( lhs != rhs) ,
693
- ( Value :: String ( lhs) , Value :: String ( rhs) ) => Value :: Bool ( lhs != rhs) ,
694
- ( Value :: Array ( lhs) , Value :: Array ( rhs) ) => {
695
- if lhs. len ( ) != rhs. len ( ) {
696
- Value :: Bool ( true )
697
- } else {
698
- let are_same = lhs. iter ( ) . zip ( rhs. iter ( ) ) . all ( |( a, b) | a == b) ;
699
- Value :: Bool ( !are_same)
700
- }
701
- }
702
- _ => return Err ( Error :: TypeError ) ,
703
- } ;
704
-
705
- Some ( value)
685
+ 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 ( ) ?;
693
+ Some ( Value :: Bool ( !is_equal) )
706
694
}
707
695
Function :: Intersects ( first_rule, second_rule) => {
708
696
let a = eval ( input, output, first_rule) ?
@@ -723,23 +711,31 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
723
711
Some ( Value :: Bool ( b. contains ( & a) ) )
724
712
}
725
713
Function :: NotIn ( first_rule, second_rule) => {
726
- let a = eval ( input, output, first_rule) ?. ok_or ( Error :: TypeError ) ?;
727
- let b = eval ( input, output, second_rule) ?
728
- . ok_or ( Error :: TypeError ) ?
729
- . try_array ( ) ?;
730
-
731
- Some ( Value :: Bool ( !b. contains ( & a) ) )
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 ( ) ?;
721
+ Some ( Value :: Bool ( !is_in) )
732
722
}
733
723
Function :: Between ( first_rule, second_rule, third_rule) => {
734
- let first_eval = first_rule. eval ( input, output) ?. ok_or ( Error :: TypeError ) ?;
735
- let second_eval = second_rule. eval ( input, output) ?. ok_or ( Error :: TypeError ) ?;
736
- let third_eval = third_rule. eval ( input, output) ?. ok_or ( Error :: TypeError ) ?;
737
-
738
- let value = BigNum :: try_from ( first_eval) ?;
739
- let start = BigNum :: try_from ( second_eval) ?;
740
- let end = BigNum :: try_from ( third_eval) ?;
741
-
742
- Some ( Value :: Bool ( value. ge ( & start) && value. le ( & end) ) )
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 ( ) ?;
738
+ Some ( Value :: Bool ( is_gte_start && is_lte_end) )
743
739
}
744
740
Function :: At ( first_rule, second_rule) => {
745
741
let mut first_eval = first_rule
@@ -752,14 +748,12 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
752
748
. try_number ( ) ?
753
749
. as_u64 ( )
754
750
. ok_or ( Error :: TypeError ) ?;
755
- let index = usize:: try_from ( second_eval) . unwrap ( ) ;
756
- let value = if first_eval. get ( index) . is_none ( ) {
757
- None
751
+ let index = usize:: try_from ( second_eval) . map_err ( |_| Error :: TypeError ) ? ;
752
+ if first_eval. get ( index) . is_none ( ) {
753
+ return Err ( Error :: TypeError ) ;
758
754
} else {
759
755
Some ( first_eval. swap_remove ( index) )
760
- } ;
761
- let value = value. ok_or ( Error :: TypeError ) ?;
762
- Some ( value)
756
+ }
763
757
}
764
758
Function :: Split ( first_rule, second_rule) => {
765
759
let first_eval = first_rule
@@ -771,12 +765,11 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
771
765
. ok_or ( Error :: TypeError ) ?
772
766
. try_string ( ) ?;
773
767
774
- let after_split: Vec < & str > = first_eval. split ( & second_eval) . collect ( ) ;
775
- let mapped_to_value = after_split
776
- . into_iter ( )
777
- . map ( |x| Value :: new_string ( x) )
768
+ let after_split = first_eval
769
+ . split ( & second_eval)
770
+ . map ( Value :: new_string)
778
771
. collect ( ) ;
779
- Some ( Value :: Array ( mapped_to_value ) )
772
+ Some ( Value :: Array ( after_split ) )
780
773
}
781
774
Function :: StartsWith ( first_rule, second_rule) => {
782
775
let first_eval = first_rule
@@ -880,57 +873,38 @@ enum MathOperator {
880
873
}
881
874
882
875
enum ComparisonOperator {
876
+ /// First value is greater than second value
883
877
Gt ,
878
+ /// First value is greater than or equal to second value
884
879
Gte ,
880
+ /// First value is lesser than second value
885
881
Lt ,
882
+ /// First value is lesser than or equal to second value
886
883
Lte ,
887
- Equals ,
888
- NotEquals ,
884
+ /// Values are equal
885
+ Eq ,
889
886
}
890
887
891
888
fn compare_numbers ( lhs : Number , rhs : Number , ops : ComparisonOperator ) -> Result < bool , Error > {
892
889
match ( lhs. as_u64 ( ) , rhs. as_u64 ( ) ) {
893
- ( Some ( lhs) , Some ( rhs) ) => handle_comparisons ( lhs, rhs, ops) ,
890
+ ( Some ( lhs) , Some ( rhs) ) => Ok ( handle_comparisons ( lhs, rhs, ops) ) ,
894
891
_ => match ( lhs. as_i64 ( ) , rhs. as_i64 ( ) ) {
895
- ( Some ( lhs) , Some ( rhs) ) => handle_comparisons ( lhs, rhs, ops) ,
892
+ ( Some ( lhs) , Some ( rhs) ) => Ok ( handle_comparisons ( lhs, rhs, ops) ) ,
896
893
_ => match ( lhs. as_f64 ( ) , rhs. as_f64 ( ) ) {
897
- ( Some ( lhs) , Some ( rhs) ) => handle_comparisons ( lhs, rhs, ops) ,
894
+ ( Some ( lhs) , Some ( rhs) ) => Ok ( handle_comparisons ( lhs, rhs, ops) ) ,
898
895
_ => Err ( Error :: TypeError ) ,
899
896
} ,
900
897
} ,
901
898
}
902
899
}
903
900
904
- fn handle_comparisons < T : PartialOrd > (
905
- lhs : T ,
906
- rhs : T ,
907
- ops : ComparisonOperator ,
908
- ) -> Result < bool , Error > {
901
+ fn handle_comparisons < T : PartialOrd > ( lhs : T , rhs : T , ops : ComparisonOperator ) -> bool {
909
902
match ops {
910
- ComparisonOperator :: Lt => {
911
- let is_lt = lhs < rhs;
912
- Ok ( is_lt)
913
- }
914
- ComparisonOperator :: Lte => {
915
- let is_lte = lhs <= rhs;
916
- Ok ( is_lte)
917
- }
918
- ComparisonOperator :: Gt => {
919
- let is_gt = lhs > rhs;
920
- Ok ( is_gt)
921
- }
922
- ComparisonOperator :: Gte => {
923
- let is_gte = lhs >= rhs;
924
- Ok ( is_gte)
925
- }
926
- ComparisonOperator :: Equals => {
927
- let is_equal = lhs == rhs;
928
- Ok ( is_equal)
929
- }
930
- ComparisonOperator :: NotEquals => {
931
- let is_not_equal = lhs != rhs;
932
- Ok ( is_not_equal)
933
- }
903
+ ComparisonOperator :: Lt => lhs. lt ( & rhs) ,
904
+ ComparisonOperator :: Lte => lhs. le ( & rhs) ,
905
+ ComparisonOperator :: Gt => lhs. gt ( & rhs) ,
906
+ ComparisonOperator :: Gte => lhs. ge ( & rhs) ,
907
+ ComparisonOperator :: Eq => lhs. eq ( & rhs) ,
934
908
}
935
909
}
936
910
0 commit comments