value_trait/
option.rs

1use crate::{
2    ExtendedValueType, ValueType,
3    base::{TypedValue, ValueAsScalar, ValueIntoString},
4    prelude::{ValueAsArray, ValueAsObject, ValueIntoArray, ValueIntoObject},
5};
6
7impl<V> ValueIntoString for Option<V>
8where
9    V: ValueIntoString,
10{
11    type String = V::String;
12
13    fn into_string(self) -> Option<Self::String> {
14        self.and_then(ValueIntoString::into_string)
15    }
16}
17
18impl<V> ValueIntoArray for Option<V>
19where
20    V: ValueIntoArray,
21{
22    type Array = V::Array;
23    fn into_array(self) -> Option<Self::Array> {
24        self.and_then(ValueIntoArray::into_array)
25    }
26}
27impl<V> ValueIntoObject for Option<V>
28where
29    V: ValueIntoObject,
30{
31    type Object = V::Object;
32    fn into_object(self) -> Option<Self::Object> {
33        self.and_then(ValueIntoObject::into_object)
34    }
35}
36
37impl<V> TypedValue for Option<V>
38where
39    V: TypedValue,
40{
41    fn value_type(&self) -> ValueType {
42        self.as_ref().map_or(
43            ValueType::Extended(ExtendedValueType::None),
44            TypedValue::value_type,
45        )
46    }
47}
48
49impl<V> ValueAsScalar for Option<V>
50where
51    V: ValueAsScalar,
52{
53    fn as_null(&self) -> Option<()> {
54        self.as_ref().and_then(ValueAsScalar::as_null)
55    }
56
57    fn as_bool(&self) -> Option<bool> {
58        self.as_ref().and_then(ValueAsScalar::as_bool)
59    }
60
61    fn as_i64(&self) -> Option<i64> {
62        self.as_ref().and_then(ValueAsScalar::as_i64)
63    }
64
65    fn as_u64(&self) -> Option<u64> {
66        self.as_ref().and_then(ValueAsScalar::as_u64)
67    }
68
69    fn as_f64(&self) -> Option<f64> {
70        self.as_ref().and_then(ValueAsScalar::as_f64)
71    }
72
73    fn as_str(&self) -> Option<&str> {
74        self.as_ref().and_then(ValueAsScalar::as_str)
75    }
76}
77
78impl<V> ValueAsArray for Option<V>
79where
80    V: ValueAsArray,
81{
82    type Array = V::Array;
83
84    fn as_array(&self) -> Option<&Self::Array> {
85        self.as_ref().and_then(ValueAsArray::as_array)
86    }
87}
88impl<V> ValueAsObject for Option<V>
89where
90    V: ValueAsObject,
91{
92    type Object = V::Object;
93
94    fn as_object(&self) -> Option<&Self::Object> {
95        self.as_ref().and_then(ValueAsObject::as_object)
96    }
97}
98
99impl<V, E> ValueIntoString for Result<V, E>
100where
101    V: ValueIntoString,
102{
103    type String = V::String;
104
105    fn into_string(self) -> Option<Self::String> {
106        self.ok().and_then(ValueIntoString::into_string)
107    }
108}
109
110impl<V, E> ValueIntoArray for Result<V, E>
111where
112    V: ValueIntoArray,
113{
114    type Array = V::Array;
115    fn into_array(self) -> Option<Self::Array> {
116        self.ok().and_then(ValueIntoArray::into_array)
117    }
118}
119impl<V, E> ValueIntoObject for Result<V, E>
120where
121    V: ValueIntoObject,
122{
123    type Object = V::Object;
124    fn into_object(self) -> Option<Self::Object> {
125        self.ok().and_then(ValueIntoObject::into_object)
126    }
127}
128
129impl<V, E> TypedValue for Result<V, E>
130where
131    V: TypedValue,
132{
133    fn value_type(&self) -> ValueType {
134        self.as_ref().ok().map_or(
135            ValueType::Extended(ExtendedValueType::None),
136            TypedValue::value_type,
137        )
138    }
139}
140
141impl<V, E> ValueAsScalar for Result<V, E>
142where
143    V: ValueAsScalar,
144{
145    fn as_null(&self) -> Option<()> {
146        self.as_ref().ok().and_then(ValueAsScalar::as_null)
147    }
148    fn as_bool(&self) -> Option<bool> {
149        self.as_ref().ok().and_then(ValueAsScalar::as_bool)
150    }
151
152    fn as_i64(&self) -> Option<i64> {
153        self.as_ref().ok().and_then(ValueAsScalar::as_i64)
154    }
155
156    fn as_u64(&self) -> Option<u64> {
157        self.as_ref().ok().and_then(ValueAsScalar::as_u64)
158    }
159
160    fn as_f64(&self) -> Option<f64> {
161        self.as_ref().ok().and_then(ValueAsScalar::as_f64)
162    }
163
164    fn as_str(&self) -> Option<&str> {
165        self.as_ref().ok().and_then(ValueAsScalar::as_str)
166    }
167}
168impl<V, E> ValueAsArray for Result<V, E>
169where
170    V: ValueAsArray,
171{
172    type Array = V::Array;
173
174    fn as_array(&self) -> Option<&Self::Array> {
175        self.as_ref().ok().and_then(ValueAsArray::as_array)
176    }
177}
178impl<V, E> ValueAsObject for Result<V, E>
179where
180    V: ValueAsObject,
181{
182    type Object = V::Object;
183
184    fn as_object(&self) -> Option<&Self::Object> {
185        self.as_ref().ok().and_then(ValueAsObject::as_object)
186    }
187}
188
189impl<V> TypedValue for &V
190where
191    V: TypedValue,
192{
193    fn value_type(&self) -> ValueType {
194        (*self).value_type()
195    }
196}
197impl<V> ValueAsScalar for &V
198where
199    V: ValueAsScalar,
200{
201    fn as_null(&self) -> Option<()> {
202        (*self).as_null()
203    }
204
205    fn as_bool(&self) -> Option<bool> {
206        (*self).as_bool()
207    }
208
209    fn as_i64(&self) -> Option<i64> {
210        (*self).as_i64()
211    }
212
213    fn as_u64(&self) -> Option<u64> {
214        (*self).as_u64()
215    }
216
217    fn as_f64(&self) -> Option<f64> {
218        (*self).as_f64()
219    }
220
221    fn as_str(&self) -> Option<&str> {
222        (*self).as_str()
223    }
224}
225
226impl<V> ValueAsArray for &V
227where
228    V: ValueAsArray,
229{
230    type Array = V::Array;
231
232    fn as_array(&self) -> Option<&Self::Array> {
233        (*self).as_array()
234    }
235}
236impl<V> ValueAsObject for &V
237where
238    V: ValueAsObject,
239{
240    type Object = V::Object;
241
242    fn as_object(&self) -> Option<&Self::Object> {
243        (*self).as_object()
244    }
245}