tower_sesh/value/
partial_eq.rs

1// Adapted from https://github.com/serde-rs/json.
2
3use super::Value;
4
5fn eq_i64(value: &Value, other: i64) -> bool {
6    value.as_i64() == Some(other)
7}
8
9fn eq_u64(value: &Value, other: u64) -> bool {
10    value.as_u64() == Some(other)
11}
12
13fn eq_f32(value: &Value, other: f32) -> bool {
14    match value {
15        Value::Number(n) => n.as_f32() == Some(other),
16        _ => false,
17    }
18}
19
20fn eq_f64(value: &Value, other: f64) -> bool {
21    value.as_f64() == Some(other)
22}
23
24fn eq_bool(value: &Value, other: bool) -> bool {
25    value.as_bool() == Some(other)
26}
27
28fn eq_str(value: &Value, other: &str) -> bool {
29    value.as_str() == Some(other)
30}
31
32macro_rules! partial_eq_numeric {
33    ($($eq:ident [$($ty:ty)*])*) => {
34        $($(
35            impl PartialEq<$ty> for Value {
36                fn eq(&self, other: &$ty) -> bool {
37                    $eq(self, *other as _)
38                }
39            }
40
41            impl<'a> PartialEq<$ty> for &'a Value {
42                fn eq(&self, other: &$ty) -> bool {
43                    $eq(self, *other as _)
44                }
45            }
46
47            impl<'a> PartialEq<$ty> for &'a mut Value {
48                fn eq(&self, other: &$ty) -> bool {
49                    $eq(self, *other as _)
50                }
51            }
52        )*)*
53    };
54}
55
56partial_eq_numeric! {
57    eq_i64[i8 i16 i32 i64 isize]
58    eq_u64[u8 u16 u32 u64 usize]
59    eq_f32[f32]
60    eq_f64[f64]
61    eq_bool[bool]
62}
63
64macro_rules! partial_eq_str {
65    ($($ty:ty)*) => {
66        $(
67            impl PartialEq<$ty> for Value {
68                fn eq(&self, other: &$ty) -> bool {
69                    eq_str(self, other)
70                }
71            }
72        )*
73    };
74}
75
76partial_eq_str! {
77    str &str String
78}