1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::cmp::Ordering;

use crate::Value;

/**
PartialOrd
**/

fn eq_i64(value: &Value, other: i64) -> Option<Ordering> {
    value.as_i64().unwrap_or_default().partial_cmp(&other)
}

fn eq_u64(value: &Value, other: u64) -> Option<Ordering> {
    value.as_u64().unwrap_or_default().partial_cmp(&other)
}

fn eq_f64(value: &Value, other: f64) -> Option<Ordering> {
    value.as_f64().unwrap_or_default().partial_cmp(&other)
}

fn eq_bool(value: &Value, other: bool) -> Option<Ordering> {
    value.as_bool().unwrap_or(false).partial_cmp(&other)
}

// fn eq_str(value: &Value, other: &str) -> Option<Ordering> {
//     value.as_str().unwrap_or("").partial_cmp(& other)
// }

macro_rules! impl_numeric_cmp {
    ($($eq:ident [$($ty:ty)*])*) => {
        $($(
            impl PartialOrd<$ty> for Value<'_> {
                fn partial_cmp(&self, other: &$ty) -> Option<Ordering> {
                    $eq(self, *other as _)
                }
            }

            impl PartialOrd<Value<'_>> for $ty {
                fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
                    $eq(other, *self as _)
                }
            }

            impl PartialOrd<&Value<'_>> for $ty {
                fn partial_cmp(&self, other: &&Value)  -> Option<Ordering> {
                    $eq(*other, *self as _)
                }
            }

            impl PartialOrd<&mut Value<'_>> for $ty {
                fn partial_cmp(&self, other: &&mut Value)  -> Option<Ordering> {
                    $eq(*other, *self as _)
                }
            }

            impl<'a> PartialOrd<$ty> for &'a Value<'_> {
                fn partial_cmp(&self, other: &$ty) -> Option<Ordering> {
                    $eq(*self, *other as _)
                }
            }

            impl<'a> PartialOrd<$ty> for &'a mut Value<'_> {
                fn partial_cmp(&self, other: &$ty) -> Option<Ordering> {
                    $eq(*self, *other as _)
                }
            }
        )*)*
    }
}

impl_numeric_cmp! {
    eq_i64[i8 i16 i32 i64 isize]
    eq_u64[u8 u16 u32 u64 usize]
    eq_f64[f32 f64]
    eq_bool[bool]
}

impl <'a,'b>PartialOrd<Value<'a>> for Value<'a> {
    fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
        self.inner.as_f64().unwrap_or_default().partial_cmp(&other.inner.as_f64().unwrap_or_default())
    }
}

impl PartialOrd<Value<'_>> for &Value<'_> {
    fn partial_cmp(&self, other: &Value<'_>) -> Option<Ordering> {
        self.inner.as_f64().unwrap_or_default().partial_cmp(&other.inner.as_f64().unwrap_or_default())
    }
}

impl PartialOrd<&Value<'_>> for Value<'_>{
    fn partial_cmp(&self, other: &&Value) -> Option<Ordering> {
        self.inner.as_f64().unwrap_or_default().partial_cmp(&other.inner.as_f64().unwrap_or_default())
    }
}