@@ -10,7 +10,7 @@ use std::{
1010use ahash:: RandomState ;
1111use hashbrown:: HashMap ;
1212
13- #[ derive( Debug , Clone , PartialEq ) ]
13+ #[ derive( Debug , Clone ) ]
1414pub enum Value < ' a > {
1515 Float ( f64 ) ,
1616 String ( Cow < ' a , [ u8 ] > ) ,
@@ -119,6 +119,37 @@ impl<'a> Div for &'_ Value<'a> {
119119 }
120120}
121121
122+ impl PartialEq for Value < ' _ > {
123+ fn eq ( & self , other : & Self ) -> bool {
124+ match ( self , other) {
125+ // Numeric comparisons
126+ ( & Self :: Float ( lhs) , & Self :: Float ( rhs) ) => lhs == rhs,
127+ ( & Self :: Bool ( lhs) , & Self :: Bool ( rhs) ) => lhs == rhs,
128+ ( & Self :: Float ( f) , & Self :: Bool ( b) ) | ( & Self :: Bool ( b) , & Self :: Float ( f) ) => b && f == 1. ,
129+ // String-based comparisons
130+ ( Self :: String ( lhs) | Self :: Regex ( lhs) , Self :: String ( rhs) | Self :: Regex ( rhs) ) => {
131+ lhs == rhs
132+ }
133+ ( & Self :: Float ( f) , Self :: String ( s) | Self :: Regex ( s) )
134+ | ( Self :: String ( s) | Self :: Regex ( s) , & Self :: Float ( f) ) => {
135+ f. to_string ( ) . as_bytes ( ) == s. as_ref ( )
136+ }
137+ ( & Self :: Bool ( b) , Self :: String ( s) | Self :: Regex ( s) )
138+ | ( Self :: String ( s) | Self :: Regex ( s) , & Self :: Bool ( b) ) => {
139+ ( if b { b"1" } else { b"0" } ) == s. as_ref ( )
140+ }
141+ // True on empty string value.
142+ ( Self :: Untyped | Self :: Unassigned , Self :: String ( s) | Self :: Regex ( s) )
143+ | ( Self :: String ( s) | Self :: Regex ( s) , Self :: Untyped | Self :: Unassigned ) => s. is_empty ( ) ,
144+ ( Self :: Untyped | Self :: Unassigned , Self :: Untyped | Self :: Unassigned ) => true ,
145+ ( Self :: Untyped | Self :: Unassigned , _) | ( _, Self :: Untyped | Self :: Unassigned ) => false ,
146+ ( Self :: Array ( _) , _) | ( _, Self :: Array ( _) ) => {
147+ panic ! ( "Attempted to use array in scalar context!" )
148+ }
149+ }
150+ }
151+ }
152+
122153impl Eq for Value < ' _ > { }
123154impl Hash for Value < ' _ > {
124155 fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
0 commit comments