jzon/value/
implements.rs

1// This is a private module that contains `PartialEq` and `From` trait
2// implementations for `JsonValue`.
3
4use std::collections::{BTreeMap, HashMap};
5
6use crate::short::{self, Short};
7use crate::number::Number;
8use crate::object::Object;
9use crate::value::JsonValue;
10
11macro_rules! implement_eq {
12    ($to:ident, $from:ty) => {
13        impl PartialEq<$from> for JsonValue {
14            fn eq(&self, other: &$from) -> bool {
15                match *self {
16                    JsonValue::$to(ref value) => value == other,
17                    _                         => false
18                }
19            }
20        }
21
22        impl<'a> PartialEq<$from> for &'a JsonValue {
23            fn eq(&self, other: &$from) -> bool {
24                match **self {
25                    JsonValue::$to(ref value) => value == other,
26                    _                         => false
27                }
28            }
29        }
30
31        impl PartialEq<JsonValue> for $from {
32            fn eq(&self, other: &JsonValue) -> bool {
33                match *other {
34                    JsonValue::$to(ref value) => value == self,
35                    _ => false
36                }
37            }
38        }
39    }
40}
41
42macro_rules! implement {
43    ($to:ident, $from:ty as num) => {
44        impl From<$from> for JsonValue {
45            fn from(val: $from) -> JsonValue {
46                JsonValue::$to(val.into())
47            }
48        }
49
50        implement_eq!($to, $from);
51    };
52    ($to:ident, $from:ty) => {
53        impl From<$from> for JsonValue {
54            fn from(val: $from) -> JsonValue {
55                JsonValue::$to(val)
56            }
57        }
58
59        implement_eq!($to, $from);
60    }
61}
62
63impl<'a> From<&'a str> for JsonValue {
64    fn from(val: &'a str) -> JsonValue {
65        if val.len() <= short::MAX_LEN {
66            JsonValue::Short(unsafe { Short::from_slice(val) })
67        } else {
68            JsonValue::String(val.into())
69        }
70    }
71}
72
73impl<T: Into<JsonValue>> From<Option<T>> for JsonValue {
74    fn from(val: Option<T>) -> JsonValue {
75        match val {
76            Some(val) => val.into(),
77            None      => JsonValue::Null,
78        }
79    }
80}
81
82impl<T: Into<JsonValue>> From<Vec<T>> for JsonValue {
83    fn from(val: Vec<T>) -> JsonValue {
84        JsonValue::Array(val.into_iter().map(Into::into).collect())
85    }
86}
87
88impl<'a, T: Into<JsonValue> + Clone> From<&'a [T]> for JsonValue {
89    fn from(val: &'a [T]) -> JsonValue {
90        JsonValue::Array(val.iter().cloned().map(Into::into).collect())
91    }
92}
93
94impl<K: AsRef<str>, V: Into<JsonValue>> From<HashMap<K, V>> for JsonValue {
95    fn from(val: HashMap<K, V>) -> JsonValue {
96        JsonValue::Object(val.into_iter().collect())
97    }
98}
99
100impl<K: AsRef<str>, V: Into<JsonValue>> From<BTreeMap<K, V>> for JsonValue {
101    fn from(val: BTreeMap<K, V>) -> JsonValue {
102        JsonValue::Object(val.into_iter().collect())
103    }
104}
105
106impl<'a> PartialEq<&'a str> for JsonValue {
107    fn eq(&self, other: &&str) -> bool {
108        match *self {
109            JsonValue::Short(ref value)  => value == *other,
110            JsonValue::String(ref value) => value == *other,
111            _ => false
112        }
113    }
114}
115
116impl<'a> PartialEq<JsonValue> for &'a str {
117    fn eq(&self, other: &JsonValue) -> bool {
118        match *other {
119            JsonValue::Short(ref value)  => value == *self,
120            JsonValue::String(ref value) => value == *self,
121            _ => false
122        }
123    }
124}
125
126impl PartialEq<str> for JsonValue {
127    fn eq(&self, other: &str) -> bool {
128        match *self {
129            JsonValue::Short(ref value)  => value == other,
130            JsonValue::String(ref value) => value == other,
131            _ => false
132        }
133    }
134}
135
136impl<'a> PartialEq<JsonValue> for str {
137    fn eq(&self, other: &JsonValue) -> bool {
138        match *other {
139            JsonValue::Short(ref value)  => value == self,
140            JsonValue::String(ref value) => value == self,
141            _ => false
142        }
143    }
144}
145
146implement!(String, String);
147implement!(Number, isize as num);
148implement!(Number, usize as num);
149implement!(Number, i8 as num);
150implement!(Number, i16 as num);
151implement!(Number, i32 as num);
152implement!(Number, i64 as num);
153implement!(Number, u8 as num);
154implement!(Number, u16 as num);
155implement!(Number, u32 as num);
156implement!(Number, u64 as num);
157implement!(Number, f32 as num);
158implement!(Number, f64 as num);
159implement!(Number, Number);
160implement!(Object, Object);
161implement!(Boolean, bool);