air_interpreter_value/value/
partial_eq.rs

1/*
2 * Copyright 2024 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * This file is based on serde_json crate by Erick Tryzelaar and David Tolnay
19 * licensed under conditions of MIT License and Apache License, Version 2.0.
20 */
21
22use super::JValue;
23use std::string::String;
24
25fn eq_i64(value: &JValue, other: i64) -> bool {
26    value.as_i64().map_or(false, |i| i == other)
27}
28
29fn eq_u64(value: &JValue, other: u64) -> bool {
30    value.as_u64().map_or(false, |i| i == other)
31}
32
33fn eq_f32(value: &JValue, other: f32) -> bool {
34    match value {
35        // NB: is not same as the original version
36        JValue::Number(n) => n.as_f64().map_or(false, |i| i == other as f64),
37        _ => false,
38    }
39}
40
41fn eq_f64(value: &JValue, other: f64) -> bool {
42    value.as_f64().map_or(false, |i| i == other)
43}
44
45fn eq_bool(value: &JValue, other: bool) -> bool {
46    value.as_bool().map_or(false, |i| i == other)
47}
48
49fn eq_str(value: &JValue, other: &str) -> bool {
50    value.as_str().map_or(false, |i| &**i == other)
51}
52
53impl PartialEq<str> for JValue {
54    fn eq(&self, other: &str) -> bool {
55        eq_str(self, other)
56    }
57}
58
59impl PartialEq<&str> for JValue {
60    fn eq(&self, other: &&str) -> bool {
61        eq_str(self, other)
62    }
63}
64
65impl PartialEq<JValue> for str {
66    fn eq(&self, other: &JValue) -> bool {
67        eq_str(other, self)
68    }
69}
70
71impl PartialEq<JValue> for &str {
72    fn eq(&self, other: &JValue) -> bool {
73        eq_str(other, self)
74    }
75}
76
77impl PartialEq<String> for JValue {
78    fn eq(&self, other: &String) -> bool {
79        eq_str(self, other.as_str())
80    }
81}
82
83impl PartialEq<JValue> for String {
84    fn eq(&self, other: &JValue) -> bool {
85        eq_str(other, self.as_str())
86    }
87}
88
89macro_rules! partialeq_numeric {
90    ($($eq:ident [$($ty:ty)*])*) => {
91        $($(
92            impl PartialEq<$ty> for JValue {
93                fn eq(&self, other: &$ty) -> bool {
94                    $eq(self, *other as _)
95                }
96            }
97
98            impl PartialEq<JValue> for $ty {
99                fn eq(&self, other: &JValue) -> bool {
100                    $eq(other, *self as _)
101                }
102            }
103
104            impl<'a> PartialEq<$ty> for &'a JValue {
105                fn eq(&self, other: &$ty) -> bool {
106                    $eq(*self, *other as _)
107                }
108            }
109        )*)*
110    }
111}
112
113partialeq_numeric! {
114    eq_i64[i8 i16 i32 i64 isize]
115    eq_u64[u8 u16 u32 u64 usize]
116    eq_f32[f32]
117    eq_f64[f64]
118    eq_bool[bool]
119}