@@ -41,6 +41,29 @@ pub enum Value {
4141}
4242
4343impl Value {
44+ fn variant_order ( & self ) -> u8 {
45+ match self {
46+ Value :: String ( _) => 0 ,
47+ Value :: F32 ( _) => 1 ,
48+ Value :: F64 ( _) => 2 ,
49+ Value :: I8 ( _) => 3 ,
50+ Value :: I16 ( _) => 4 ,
51+ Value :: I32 ( _) => 5 ,
52+ Value :: I64 ( _) => 6 ,
53+ Value :: U8 ( _) => 7 ,
54+ Value :: U16 ( _) => 8 ,
55+ Value :: U32 ( _) => 9 ,
56+ Value :: U64 ( _) => 10 ,
57+ Value :: U128 ( _) => 11 ,
58+ Value :: Date ( _) => 12 ,
59+ Value :: Boolean ( _) => 13 ,
60+ Value :: Id ( _) => 14 ,
61+ Value :: Array ( _) => 15 ,
62+ Value :: Object ( _) => 16 ,
63+ Value :: Empty => 17 ,
64+ }
65+ }
66+
4467 pub fn inner_stringify ( & self ) -> String {
4568 match self {
4669 Value :: String ( s) => s. to_string ( ) ,
@@ -221,10 +244,10 @@ impl Ord for Value {
221244 cmp
222245 }
223246 }
224- Err ( _) => Ordering :: Equal ,
247+ Err ( _) => self . variant_order ( ) . cmp ( & other . variant_order ( ) ) ,
225248 }
226249 }
227- ( _, _) => Ordering :: Equal ,
250+ ( _, _) => self . variant_order ( ) . cmp ( & other . variant_order ( ) ) ,
228251 }
229252 }
230253}
@@ -2725,12 +2748,21 @@ mod tests {
27252748
27262749 #[ test]
27272750 fn test_value_ordering_mixed_types ( ) {
2728- // Non-comparable types should return Equal
2729- assert_eq ! (
2730- Value :: String ( "test" . to_string( ) ) . cmp( & Value :: I32 ( 42 ) ) ,
2731- Ordering :: Equal
2732- ) ;
2733- assert_eq ! ( Value :: Boolean ( true ) . cmp( & Value :: F64 ( 3.14 ) ) , Ordering :: Equal ) ;
2751+ // Non-comparable types should return a consistent ordering based on variant
2752+ // discriminant, not Equal (which would violate the Ord contract since
2753+ // PartialEq returns false for these pairs).
2754+ let s = Value :: String ( "test" . to_string ( ) ) ;
2755+ let i = Value :: I32 ( 42 ) ;
2756+ let b = Value :: Boolean ( true ) ;
2757+ let f = Value :: F64 ( 3.14 ) ;
2758+
2759+ // Ordering must be consistent (antisymmetric)
2760+ assert_eq ! ( s. cmp( & i) , i. cmp( & s) . reverse( ) ) ;
2761+ assert_eq ! ( b. cmp( & f) , f. cmp( & b) . reverse( ) ) ;
2762+
2763+ // Ordering must not be Equal for different variant types
2764+ assert_ne ! ( s. cmp( & i) , Ordering :: Equal ) ;
2765+ assert_ne ! ( b. cmp( & f) , Ordering :: Equal ) ;
27342766 }
27352767
27362768 #[ test]
0 commit comments