valu3/
to_value.rs

1use crate::prelude::*;
2use std::collections::{BTreeMap, HashMap};
3
4impl ToValueBehavior for Value {
5    fn to_value(&self) -> Value {
6        self.clone()
7    }
8}
9
10#[cfg(feature = "cstring")]
11impl ToValueBehavior for CString {
12    fn to_value(&self) -> Value {
13        Value::String(StringB::from(self.clone()))
14    }
15}
16
17#[cfg(feature = "cstring")]
18impl ToValueBehavior for String {
19    fn to_value(&self) -> Value {
20        Value::String(StringB::from(CString::new(self.clone()).unwrap()))
21    }
22}
23
24#[cfg(not(feature = "cstring"))]
25impl ToValueBehavior for String {
26    fn to_value(&self) -> Value {
27        Value::String(StringB::from(self.clone()))
28    }
29}
30
31impl ToValueBehavior for bool {
32    fn to_value(&self) -> Value {
33        Value::Boolean(*self)
34    }
35}
36
37impl ToValueBehavior for str {
38    fn to_value(&self) -> Value {
39        Value::String(StringB::from(self.to_string()))
40    }
41}
42
43impl ToValueBehavior for &str {
44    fn to_value(&self) -> Value {
45        Value::String(StringB::from(self.to_string()))
46    }
47}
48
49impl ToValueBehavior for StringB {
50    fn to_value(&self) -> Value {
51        Value::String(StringB::from(self.clone()))
52    }
53}
54
55impl ToValueBehavior for Array {
56    fn to_value(&self) -> Value {
57        Value::Array(self.clone())
58    }
59}
60
61impl ToValueBehavior for DateTime {
62    fn to_value(&self) -> Value {
63        Value::DateTime(self.clone())
64    }
65}
66
67impl ToValueBehavior for Number {
68    fn to_value(&self) -> Value {
69        Value::Number(self.clone())
70    }
71}
72
73impl ToValueBehavior for Object {
74    fn to_value(&self) -> Value {
75        Value::Object(self.clone())
76    }
77}
78
79impl<T> ToValueBehavior for Option<T>
80where
81    T: ToValueBehavior,
82{
83    fn to_value(&self) -> Value {
84        match self {
85            Some(value) => value.to_value(),
86            None => Value::Null,
87        }
88    }
89}
90
91impl<V> ToValueBehavior for Vec<V>
92where
93    V: ToValueBehavior,
94{
95    fn to_value(&self) -> Value {
96        Array::from(self.iter().map(|v| v.to_value()).collect::<Vec<Value>>()).to_value()
97    }
98}
99
100impl<K, V> ToValueBehavior for HashMap<K, V>
101where
102    K: ValueKeyBehavior,
103    V: ToValueBehavior,
104{
105    fn to_value(&self) -> Value {
106        Object::from(
107            self.iter()
108                .map(|(k, v)| (k.to_value_key(), v.to_value()))
109                .collect::<HashMap<ValueKey, Value>>(),
110        )
111        .to_value()
112    }
113}
114
115impl<T, V> ToValueBehavior for BTreeMap<T, V>
116where
117    T: ValueKeyBehavior,
118    V: ToValueBehavior,
119{
120    fn to_value(&self) -> Value {
121        Object::from(
122            self.iter()
123                .map(|(k, v)| (k.to_value_key(), v.to_value()))
124                .collect::<HashMap<ValueKey, Value>>(),
125        )
126        .to_value()
127    }
128}
129
130// Numerics
131impl ToValueBehavior for u8 {
132    fn to_value(&self) -> Value {
133        Value::Number(Number::from(*self))
134    }
135}
136
137impl ToValueBehavior for u16 {
138    fn to_value(&self) -> Value {
139        Value::Number(Number::from(*self))
140    }
141}
142
143impl ToValueBehavior for u32 {
144    fn to_value(&self) -> Value {
145        Value::Number(Number::from(*self))
146    }
147}
148
149impl ToValueBehavior for u64 {
150    fn to_value(&self) -> Value {
151        Value::Number(Number::from(*self))
152    }
153}
154
155impl ToValueBehavior for u128 {
156    fn to_value(&self) -> Value {
157        Value::Number(Number::from(*self))
158    }
159}
160
161impl ToValueBehavior for i8 {
162    fn to_value(&self) -> Value {
163        Value::Number(Number::from(*self))
164    }
165}
166
167impl ToValueBehavior for i16 {
168    fn to_value(&self) -> Value {
169        Value::Number(Number::from(*self))
170    }
171}
172
173impl ToValueBehavior for i32 {
174    fn to_value(&self) -> Value {
175        Value::Number(Number::from(*self))
176    }
177}
178
179impl ToValueBehavior for i64 {
180    fn to_value(&self) -> Value {
181        Value::Number(Number::from(*self))
182    }
183}
184
185impl ToValueBehavior for i128 {
186    fn to_value(&self) -> Value {
187        Value::Number(Number::from(*self))
188    }
189}
190
191impl ToValueBehavior for f32 {
192    fn to_value(&self) -> Value {
193        Value::Number(Number::from(*self))
194    }
195}
196
197impl ToValueBehavior for f64 {
198    fn to_value(&self) -> Value {
199        Value::Number(Number::from(*self))
200    }
201}
202impl ToValueBehavior for usize {
203    fn to_value(&self) -> Value {
204        Value::Number(Number::from(*self))
205    }
206}
207
208impl ToValueBehavior for isize {
209    fn to_value(&self) -> Value {
210        Value::Number(Number::from(*self))
211    }
212}
213
214/// Set to_value all items in a vector
215/// # Example
216/// ```
217/// use valu3_parquet::vec_value;
218/// use valu3::Value;
219/// let vec = vec_value![1, 2, 3];
220///
221/// assert_eq!(vec, vec![Value::Number(1), Value::Number(2), Value::Number(3)]);
222/// ```
223#[macro_export]
224macro_rules! vec_value {
225    ($($x:expr),*) => {
226        vec![$($x.to_value()),*]
227    };
228}
229
230#[cfg(test)]
231mod test {
232    use std::collections::HashMap;
233
234    use crate::prelude::*;
235
236    #[test]
237    fn test_boolean() {
238        assert_eq!(true.to_value(), Value::Boolean(true));
239        assert_eq!(false.to_value(), Value::Boolean(false));
240    }
241
242    #[test]
243    fn test_string() {
244        assert_eq!(
245            "test".to_value(),
246            Value::String(StringB::from("test".to_string()))
247        );
248    }
249
250    #[test]
251    fn test_array() {
252        assert_eq!(
253            vec![1, 2, 3].to_value(),
254            Value::Array(Array::from(vec![1, 2, 3]))
255        );
256    }
257
258    #[test]
259    fn test_object() {
260        let mut map = HashMap::new();
261        map.insert("test", 1);
262        assert_eq!(map.to_value(), Object::from(map).to_value());
263    }
264
265    #[test]
266    fn test_vec_value_macro() {
267        assert_eq!(
268            vec_value![1, 2, vec![1, 2, 3]],
269            vec![Value::from(1), Value::from(2), Value::from(vec![1, 2, 3])]
270        );
271    }
272
273    #[test]
274    fn test_from_usize() {
275        let number = 1 as usize;
276        assert_eq!(number.to_value(), Value::Number(Number::from(number)));
277    }
278
279    #[test]
280    fn test_from_isize() {
281        let number = 1 as isize;
282        assert_eq!(number.to_value(), Value::Number(Number::from(number)));
283    }
284}