s2json_core/
value_impl.rs

1use crate::*;
2use alloc::{
3    string::{String, ToString},
4    vec::Vec,
5};
6use libm::round;
7
8// PrimitiveValue
9impl PrimitiveValue {
10    /// Returns true if the value is null
11    pub fn is_null(&self) -> bool {
12        matches!(self, PrimitiveValue::Null)
13    }
14
15    /// Converts a primitive value to a string
16    pub fn to_string(&self) -> Option<String> {
17        match self {
18            PrimitiveValue::String(v) => Some(v.clone()),
19            _ => None,
20        }
21    }
22
23    /// Converts a primitive value to a u64
24    pub fn to_u64(&self) -> Option<u64> {
25        match self {
26            PrimitiveValue::U64(v) => Some(*v),
27            PrimitiveValue::I64(v) => Some(*v as u64),
28            PrimitiveValue::F64(v) => Some(round(*v) as u64),
29            PrimitiveValue::F32(v) => Some(round((*v).into()) as u64),
30            _ => None,
31        }
32    }
33
34    /// Converts a primitive value to a i64
35    pub fn to_i64(&self) -> Option<i64> {
36        match self {
37            PrimitiveValue::U64(v) => Some(*v as i64),
38            PrimitiveValue::I64(v) => Some(*v),
39            PrimitiveValue::F64(v) => Some(round(*v) as i64),
40            PrimitiveValue::F32(v) => Some(round((*v).into()) as i64),
41            _ => None,
42        }
43    }
44
45    /// Converts a primitive value to a f64
46    pub fn to_f64(&self) -> Option<f64> {
47        match self {
48            PrimitiveValue::U64(v) => Some(*v as f64),
49            PrimitiveValue::I64(v) => Some(*v as f64),
50            PrimitiveValue::F64(v) => Some(*v),
51            PrimitiveValue::F32(v) => Some(*v as f64),
52            _ => None,
53        }
54    }
55
56    /// Converts a primitive value to a f32
57    pub fn to_f32(&self) -> Option<f32> {
58        match self {
59            PrimitiveValue::U64(v) => Some(*v as f32),
60            PrimitiveValue::I64(v) => Some(*v as f32),
61            PrimitiveValue::F64(v) => Some(*v as f32),
62            PrimitiveValue::F32(v) => Some(*v),
63            _ => None,
64        }
65    }
66
67    /// Converts a primitive value to a bool
68    pub fn to_bool(&self) -> Option<bool> {
69        match self {
70            PrimitiveValue::Bool(v) => Some(*v),
71            _ => None,
72        }
73    }
74}
75impl From<&str> for PrimitiveValue {
76    fn from(s: &str) -> Self {
77        PrimitiveValue::String(s.to_string())
78    }
79}
80impl From<String> for PrimitiveValue {
81    fn from(s: String) -> Self {
82        PrimitiveValue::String(s)
83    }
84}
85impl From<u64> for PrimitiveValue {
86    fn from(v: u64) -> Self {
87        PrimitiveValue::U64(v)
88    }
89}
90impl From<i64> for PrimitiveValue {
91    fn from(v: i64) -> Self {
92        PrimitiveValue::I64(v)
93    }
94}
95impl From<f32> for PrimitiveValue {
96    fn from(v: f32) -> Self {
97        PrimitiveValue::F32(v)
98    }
99}
100impl From<f64> for PrimitiveValue {
101    fn from(v: f64) -> Self {
102        PrimitiveValue::F64(v)
103    }
104}
105impl From<bool> for PrimitiveValue {
106    fn from(v: bool) -> Self {
107        PrimitiveValue::Bool(v)
108    }
109}
110impl From<()> for PrimitiveValue {
111    fn from(_: ()) -> Self {
112        PrimitiveValue::Null
113    }
114}
115impl<T> From<Option<T>> for PrimitiveValue
116where
117    T: Into<PrimitiveValue>,
118{
119    fn from(v: Option<T>) -> Self {
120        match v {
121            Some(v) => v.into(),
122            None => PrimitiveValue::Null,
123        }
124    }
125}
126impl From<&PrimitiveValue> for JSONValue {
127    fn from(v: &PrimitiveValue) -> Self {
128        JSONValue::Primitive(v.clone())
129    }
130}
131impl From<&JSONValue> for PrimitiveValue {
132    fn from(v: &JSONValue) -> Self {
133        match v {
134            JSONValue::Primitive(v) => v.clone(),
135            // DROPS VALUES THAT ARE NOT PRIMITIVES
136            _ => PrimitiveValue::Null,
137        }
138    }
139}
140
141// ValuePrimitiveType
142impl ValuePrimitiveType {
143    /// Returns the value as a primitive
144    pub fn to_prim(&self) -> Option<&PrimitiveValue> {
145        match self {
146            ValuePrimitiveType::Primitive(v) => Some(v),
147            _ => None,
148        }
149    }
150
151    /// Returns the value as a nested object
152    pub fn to_nested(&self) -> Option<&ValuePrimitive> {
153        match self {
154            ValuePrimitiveType::NestedPrimitive(v) => Some(v),
155            _ => None,
156        }
157    }
158}
159impl From<&str> for ValuePrimitiveType {
160    fn from(s: &str) -> Self {
161        ValuePrimitiveType::Primitive(PrimitiveValue::String(s.to_string()))
162    }
163}
164impl From<String> for ValuePrimitiveType {
165    fn from(s: String) -> Self {
166        ValuePrimitiveType::Primitive(PrimitiveValue::String(s))
167    }
168}
169impl From<&ValuePrimitiveType> for String {
170    fn from(v: &ValuePrimitiveType) -> Self {
171        match v {
172            ValuePrimitiveType::Primitive(PrimitiveValue::String(s)) => s.to_string(),
173            _ => "".to_string(),
174        }
175    }
176}
177// Implement for u8, u16, u32, u64
178macro_rules! impl_from_uint_uint {
179    ($($t:ty),*) => {
180        $(
181            impl From<$t> for ValuePrimitiveType {
182                fn from(v: $t) -> Self {
183                    ValuePrimitiveType::Primitive(PrimitiveValue::U64(v as u64))
184                }
185            }
186        )*
187    };
188}
189impl_from_uint_uint!(u8, u16, u32, u64, usize);
190macro_rules! impl_from_uint_ref {
191    ($($t:ty),*) => {
192        $(
193            impl<'a> From<&'a $t> for ValuePrimitiveType {
194                fn from(v: &$t) -> Self {
195                    ValuePrimitiveType::Primitive(PrimitiveValue::U64(*v as u64))
196                }
197            }
198        )*
199    };
200}
201impl_from_uint_ref!(u8, u16, u32, u64, usize);
202// Implement for u8, u16, u32, u64
203macro_rules! impl_from_prim_uint {
204    ($($t:ty),*) => {
205        $(
206            impl From<ValuePrimitiveType> for $t {
207                fn from(v: ValuePrimitiveType) -> Self {
208                    match v {
209                        ValuePrimitiveType::Primitive(PrimitiveValue::U64(v)) => v as $t,
210                        _ => 0,
211                    }
212                }
213            }
214        )*
215    };
216}
217impl_from_prim_uint!(u8, u16, u32, u64, usize);
218macro_rules! impl_from_prim_ref_uint {
219    ($($t:ty),*) => {
220        $(
221            impl From<&ValuePrimitiveType> for $t {
222                fn from(v: &ValuePrimitiveType) -> Self {
223                    match v {
224                        ValuePrimitiveType::Primitive(PrimitiveValue::U64(v)) => *v as $t,
225                        _ => 0,
226                    }
227                }
228            }
229        )*
230    };
231}
232impl_from_prim_ref_uint!(u8, u16, u32, u64, usize);
233// Implement for i8, i16, i32, i64, isize
234macro_rules! impl_from_sint_sint {
235    ($($t:ty),*) => {
236        $(
237            impl From<$t> for ValuePrimitiveType {
238                fn from(v: $t) -> Self {
239                    ValuePrimitiveType::Primitive(PrimitiveValue::I64(v as i64))
240                }
241            }
242        )*
243    };
244}
245impl_from_sint_sint!(i8, i16, i32, i64, isize);
246macro_rules! impl_from_sint_ref {
247    ($($t:ty),*) => {
248        $(
249            impl<'a> From<&'a $t> for ValuePrimitiveType {
250                fn from(v: &$t) -> Self {
251                    ValuePrimitiveType::Primitive(PrimitiveValue::I64(*v as i64))
252                }
253            }
254        )*
255    };
256}
257impl_from_sint_ref!(i8, i16, i32, i64, isize);
258// Implement for i8, i16, i32, i64, isize
259macro_rules! impl_from_prim_sint {
260    ($($t:ty),*) => {
261        $(
262            impl From<ValuePrimitiveType> for $t {
263                fn from(v: ValuePrimitiveType) -> Self {
264                    match v {
265                        ValuePrimitiveType::Primitive(PrimitiveValue::I64(v)) => v as $t,
266                        _ => 0,
267                    }
268                }
269            }
270        )*
271    };
272}
273impl_from_prim_sint!(i8, i16, i32, i64, isize);
274macro_rules! impl_from_prim_ref_sint {
275    ($($t:ty),*) => {
276        $(
277            impl From<&ValuePrimitiveType> for $t {
278                fn from(v: &ValuePrimitiveType) -> Self {
279                    match v {
280                        ValuePrimitiveType::Primitive(PrimitiveValue::I64(v)) => *v as $t,
281                        _ => 0,
282                    }
283                }
284            }
285        )*
286    };
287}
288impl_from_prim_ref_sint!(i8, i16, i32, i64, isize);
289impl From<f32> for ValuePrimitiveType {
290    fn from(v: f32) -> Self {
291        ValuePrimitiveType::Primitive(PrimitiveValue::F32(v))
292    }
293}
294impl From<&ValuePrimitiveType> for f32 {
295    fn from(v: &ValuePrimitiveType) -> Self {
296        match v {
297            ValuePrimitiveType::Primitive(PrimitiveValue::F32(v)) => *v,
298            _ => 0.0,
299        }
300    }
301}
302impl From<f64> for ValuePrimitiveType {
303    fn from(v: f64) -> Self {
304        ValuePrimitiveType::Primitive(PrimitiveValue::F64(v))
305    }
306}
307impl From<&ValuePrimitiveType> for f64 {
308    fn from(v: &ValuePrimitiveType) -> Self {
309        match v {
310            ValuePrimitiveType::Primitive(PrimitiveValue::F64(v)) => *v,
311            _ => 0.0,
312        }
313    }
314}
315impl From<bool> for ValuePrimitiveType {
316    fn from(v: bool) -> Self {
317        ValuePrimitiveType::Primitive(PrimitiveValue::Bool(v))
318    }
319}
320impl From<&ValuePrimitiveType> for bool {
321    fn from(v: &ValuePrimitiveType) -> Self {
322        match v {
323            ValuePrimitiveType::Primitive(PrimitiveValue::Bool(v)) => *v,
324            _ => false,
325        }
326    }
327}
328impl From<()> for ValuePrimitiveType {
329    fn from(_: ()) -> Self {
330        ValuePrimitiveType::Primitive(PrimitiveValue::Null)
331    }
332}
333impl From<&ValuePrimitiveType> for () {
334    fn from(_: &ValuePrimitiveType) -> Self {}
335}
336impl From<PrimitiveValue> for ValuePrimitiveType {
337    fn from(v: PrimitiveValue) -> Self {
338        ValuePrimitiveType::Primitive(v)
339    }
340}
341impl From<&ValuePrimitiveType> for PrimitiveValue {
342    fn from(v: &ValuePrimitiveType) -> Self {
343        match v {
344            ValuePrimitiveType::Primitive(v) => v.clone(),
345            _ => PrimitiveValue::Null,
346        }
347    }
348}
349impl From<ValuePrimitive> for ValuePrimitiveType {
350    fn from(v: ValuePrimitive) -> Self {
351        ValuePrimitiveType::NestedPrimitive(v)
352    }
353}
354impl From<&ValuePrimitiveType> for ValuePrimitive {
355    fn from(v: &ValuePrimitiveType) -> Self {
356        match v {
357            ValuePrimitiveType::NestedPrimitive(v) => v.clone(),
358            _ => ValuePrimitive::new(),
359        }
360    }
361}
362impl<T> From<Option<T>> for ValuePrimitiveType
363where
364    T: Into<ValuePrimitiveType>,
365{
366    fn from(v: Option<T>) -> Self {
367        match v {
368            Some(v) => v.into(),
369            None => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
370        }
371    }
372}
373impl From<&ValuePrimitiveType> for JSONValue {
374    fn from(v: &ValuePrimitiveType) -> Self {
375        match v {
376            ValuePrimitiveType::Primitive(v) => JSONValue::Primitive(v.clone()),
377            ValuePrimitiveType::NestedPrimitive(v) => {
378                let mut map = Map::<String, JSONValue>::new();
379                for (k, v) in v.iter() {
380                    map.insert(k.clone(), v.into());
381                }
382                JSONValue::Object(map)
383            }
384        }
385    }
386}
387impl From<&JSONValue> for ValuePrimitiveType {
388    fn from(v: &JSONValue) -> Self {
389        match v {
390            JSONValue::Primitive(v) => ValuePrimitiveType::Primitive(v.clone()),
391            JSONValue::Object(v) => {
392                let mut map = ValuePrimitive::new();
393                for (k, v) in v.iter() {
394                    map.insert(k.clone(), v.into());
395                }
396                ValuePrimitiveType::NestedPrimitive(map)
397            }
398            // DROPS ALL ARRAY DATA AS IT IS NOT SUPPORTED INSIDE VALUE PRIMITIVES
399            _ => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
400        }
401    }
402}
403
404// ValueType
405impl Default for ValueType {
406    fn default() -> Self {
407        ValueType::Primitive(PrimitiveValue::Null)
408    }
409}
410impl ValueType {
411    /// Returns the value as a primitive
412    pub fn to_prim(&self) -> Option<&PrimitiveValue> {
413        match self {
414            ValueType::Primitive(v) => Some(v),
415            _ => None,
416        }
417    }
418
419    /// Returns the value as a vector
420    pub fn to_vec(&self) -> Option<&Vec<ValuePrimitiveType>> {
421        match self {
422            ValueType::Array(v) => Some(v),
423            _ => None,
424        }
425    }
426
427    /// Returns the value as a nested object
428    pub fn to_nested(&self) -> Option<&Value> {
429        match self {
430            ValueType::Nested(v) => Some(v),
431            _ => None,
432        }
433    }
434}
435impl From<&str> for ValueType {
436    fn from(s: &str) -> Self {
437        ValueType::Primitive(PrimitiveValue::String(s.to_string()))
438    }
439}
440// impl AsRef<str> for ValueType {
441//     fn as_ref(&self) -> &str {
442//         match self {
443//             ValueType::Primitive(PrimitiveValue::String(s)) => s.as_str(),
444//             _ => "",
445//         }
446//     }
447// }
448impl From<String> for ValueType {
449    fn from(s: String) -> Self {
450        ValueType::Primitive(PrimitiveValue::String(s))
451    }
452}
453impl From<&String> for ValueType {
454    fn from(s: &String) -> Self {
455        ValueType::Primitive(PrimitiveValue::String(s.into()))
456    }
457}
458impl From<ValueType> for String {
459    fn from(v: ValueType) -> Self {
460        match v {
461            ValueType::Primitive(PrimitiveValue::String(s)) => s,
462            _ => "".to_string(),
463        }
464    }
465}
466impl From<&ValueType> for String {
467    fn from(v: &ValueType) -> Self {
468        match v {
469            ValueType::Primitive(PrimitiveValue::String(s)) => s.into(),
470            _ => "".to_string(),
471        }
472    }
473}
474
475// Implement for u8, u16, u32, u64
476macro_rules! impl_from_uint {
477    ($($t:ty),*) => {
478        $(
479            impl From<$t> for ValueType {
480                fn from(v: $t) -> Self {
481                    ValueType::Primitive(PrimitiveValue::U64(v as u64))
482                }
483            }
484
485            impl From<ValueType> for $t {
486                fn from(v: ValueType) -> Self {
487                    match v {
488                        ValueType::Primitive(PrimitiveValue::U64(v)) => v as $t,
489                        _ => 0,
490                    }
491                }
492            }
493        )*
494    };
495}
496impl_from_uint!(u8, u16, u32, u64, usize);
497macro_rules! impl_from_uint_ref {
498    ($($t:ty),*) => {
499        $(
500            impl From<&ValueType> for $t {
501                fn from(v: &ValueType) -> Self {
502                    match v {
503                        ValueType::Primitive(PrimitiveValue::U64(v)) => *v as $t,
504                        _ => 0,
505                    }
506                }
507            }
508        )*
509    };
510}
511impl_from_uint_ref!(u8, u16, u32, u64, usize);
512// Implement for i8, i16, i32, i64
513macro_rules! impl_from_sint {
514    ($($t:ty),*) => {
515        $(
516            impl From<$t> for ValueType {
517                fn from(v: $t) -> Self {
518                    ValueType::Primitive(PrimitiveValue::I64(v as i64))
519                }
520            }
521
522            impl From<ValueType> for $t {
523                fn from(v: ValueType) -> Self {
524                    match v {
525                        ValueType::Primitive(PrimitiveValue::I64(v)) => v as $t,
526                        _ => 0,
527                    }
528                }
529            }
530        )*
531    };
532}
533impl_from_sint!(i8, i16, i32, i64, isize);
534macro_rules! impl_from_sint_ref {
535    ($($t:ty),*) => {
536        $(
537            impl From<&ValueType> for $t {
538                fn from(v: &ValueType) -> Self {
539                    match v {
540                        ValueType::Primitive(PrimitiveValue::I64(v)) => *v as $t,
541                        _ => 0,
542                    }
543                }
544            }
545        )*
546    };
547}
548impl_from_sint_ref!(i8, i16, i32, i64, isize);
549impl From<f32> for ValueType {
550    fn from(v: f32) -> Self {
551        ValueType::Primitive(PrimitiveValue::F32(v))
552    }
553}
554impl From<ValueType> for f32 {
555    fn from(v: ValueType) -> Self {
556        match v {
557            ValueType::Primitive(PrimitiveValue::F32(v)) => v,
558            _ => 0.0,
559        }
560    }
561}
562impl From<&ValueType> for f32 {
563    fn from(v: &ValueType) -> Self {
564        match v {
565            ValueType::Primitive(PrimitiveValue::F32(v)) => *v,
566            _ => 0.0,
567        }
568    }
569}
570impl From<f64> for ValueType {
571    fn from(v: f64) -> Self {
572        ValueType::Primitive(PrimitiveValue::F64(v))
573    }
574}
575impl From<ValueType> for f64 {
576    fn from(v: ValueType) -> Self {
577        match v {
578            ValueType::Primitive(PrimitiveValue::F64(v)) => v,
579            _ => 0.0,
580        }
581    }
582}
583impl From<&ValueType> for f64 {
584    fn from(v: &ValueType) -> Self {
585        match v {
586            ValueType::Primitive(PrimitiveValue::F64(v)) => *v,
587            _ => 0.0,
588        }
589    }
590}
591impl From<bool> for ValueType {
592    fn from(v: bool) -> Self {
593        ValueType::Primitive(PrimitiveValue::Bool(v))
594    }
595}
596impl From<ValueType> for bool {
597    fn from(v: ValueType) -> Self {
598        match v {
599            ValueType::Primitive(PrimitiveValue::Bool(v)) => v,
600            _ => false,
601        }
602    }
603}
604impl From<&ValueType> for bool {
605    fn from(v: &ValueType) -> Self {
606        match v {
607            ValueType::Primitive(PrimitiveValue::Bool(v)) => *v,
608            _ => false,
609        }
610    }
611}
612impl From<()> for ValueType {
613    fn from(_: ()) -> Self {
614        ValueType::Primitive(PrimitiveValue::Null)
615    }
616}
617impl From<ValueType> for () {
618    fn from(_: ValueType) -> Self {}
619}
620impl From<&ValueType> for () {
621    fn from(_: &ValueType) -> Self {}
622}
623impl<T> From<Vec<T>> for ValueType
624where
625    T: Into<ValuePrimitiveType>,
626{
627    fn from(v: Vec<T>) -> Self {
628        ValueType::Array(v.into_iter().map(Into::into).collect())
629    }
630}
631impl<T> From<&Vec<T>> for ValueType
632where
633    T: Into<ValuePrimitiveType>,
634    ValuePrimitiveType: for<'a> From<&'a T>,
635{
636    fn from(v: &Vec<T>) -> Self {
637        ValueType::Array(v.iter().map(Into::into).collect())
638    }
639}
640impl<T> From<ValueType> for Vec<T>
641where
642    T: From<ValuePrimitiveType>,
643{
644    fn from(v: ValueType) -> Self {
645        match v {
646            ValueType::Array(v) => v.into_iter().map(Into::into).collect(),
647            _ => Vec::new(),
648        }
649    }
650}
651impl<T> From<&ValueType> for Vec<T>
652where
653    T: for<'a> From<&'a ValuePrimitiveType>,
654{
655    fn from(v: &ValueType) -> Self {
656        match v {
657            ValueType::Array(v) => v.iter().map(Into::into).collect(),
658            _ => Vec::new(),
659        }
660    }
661}
662impl From<Value> for ValueType {
663    fn from(v: Value) -> Self {
664        ValueType::Nested(v)
665    }
666}
667impl From<ValueType> for Value {
668    fn from(v: ValueType) -> Self {
669        match v {
670            ValueType::Nested(v) => v,
671            _ => Value::default(),
672        }
673    }
674}
675impl From<&ValueType> for Value {
676    fn from(v: &ValueType) -> Self {
677        match v {
678            ValueType::Nested(v) => v.clone(),
679            _ => Value::default(),
680        }
681    }
682}
683impl<T> From<Option<T>> for ValueType
684where
685    T: Into<ValueType>,
686{
687    fn from(v: Option<T>) -> Self {
688        match v {
689            Some(v) => v.into(),
690            None => ValueType::Primitive(PrimitiveValue::Null),
691        }
692    }
693}
694impl From<&JSONValue> for ValueType {
695    fn from(v: &JSONValue) -> Self {
696        match v {
697            JSONValue::Primitive(v) => ValueType::Primitive(v.clone()),
698            JSONValue::Array(v) => ValueType::Array(v.iter().map(Into::into).collect()),
699            JSONValue::Object(v) => {
700                let mut res = Value::new();
701                for (k, v) in v.iter() {
702                    res.insert(k.clone(), v.into());
703                }
704                ValueType::Nested(res)
705            }
706        }
707    }
708}
709impl From<&ValueType> for JSONValue {
710    fn from(v: &ValueType) -> Self {
711        match v {
712            ValueType::Primitive(v) => JSONValue::Primitive(v.clone()),
713            ValueType::Array(v) => JSONValue::Array(v.iter().map(Into::into).collect()),
714            ValueType::Nested(v) => {
715                let mut res = Map::<String, JSONValue>::new();
716                for (k, v) in v.iter() {
717                    res.insert(k.clone(), v.into());
718                }
719                JSONValue::Object(res)
720            }
721        }
722    }
723}
724
725impl Default for JSONValue {
726    fn default() -> Self {
727        JSONValue::Primitive(PrimitiveValue::Null)
728    }
729}
730impl JSONValue {
731    /// Returns the value as a primitive
732    pub fn to_prim(&self) -> Option<&PrimitiveValue> {
733        match self {
734            JSONValue::Primitive(v) => Some(v),
735            _ => None,
736        }
737    }
738
739    /// Returns the value as a vector
740    pub fn to_vec(&self) -> Option<&Vec<JSONValue>> {
741        match self {
742            JSONValue::Array(v) => Some(v),
743            _ => None,
744        }
745    }
746
747    /// Returns the value as a nested object
748    pub fn to_nested(&self) -> Option<&Map<String, JSONValue>> {
749        match self {
750            JSONValue::Object(v) => Some(v),
751            _ => None,
752        }
753    }
754}
755
756impl MValueCompatible for JSONProperties {}
757impl From<JSONProperties> for MValue {
758    fn from(json: JSONProperties) -> MValue {
759        let mut res = MValue::new();
760        for (k, v) in json.iter() {
761            res.insert(k.clone(), v.into());
762        }
763        res
764    }
765}
766impl From<&JSONProperties> for MValue {
767    fn from(json: &JSONProperties) -> MValue {
768        let mut res = MValue::new();
769        for (k, v) in json.iter() {
770            res.insert(k.clone(), v.into());
771        }
772        res
773    }
774}
775impl From<MValue> for JSONProperties {
776    fn from(v: MValue) -> JSONProperties {
777        let mut res = JSONProperties::new();
778        for (k, v) in v.iter() {
779            res.insert(k.clone(), v.into());
780        }
781        res
782    }
783}
784impl From<&MValue> for JSONProperties {
785    fn from(v: &MValue) -> JSONProperties {
786        let mut res = JSONProperties::new();
787        for (k, v) in v.iter() {
788            res.insert(k.clone(), v.into());
789        }
790        res
791    }
792}
793
794impl MValueCompatible for MapboxProperties {}
795impl From<MapboxProperties> for MValue {
796    fn from(json: MapboxProperties) -> MValue {
797        let mut res = MValue::new();
798        for (k, v) in json.iter() {
799            res.insert(k.clone(), ValueType::Primitive(v.clone()));
800        }
801        res
802    }
803}
804impl From<&MapboxProperties> for MValue {
805    fn from(json: &MapboxProperties) -> MValue {
806        let mut res = MValue::new();
807        for (k, v) in json.iter() {
808            res.insert(k.clone(), ValueType::Primitive(v.clone()));
809        }
810        res
811    }
812}
813impl From<MValue> for MapboxProperties {
814    fn from(v: MValue) -> MapboxProperties {
815        let mut res = MapboxProperties::new();
816        // Only copy over primitive values
817        for (k, v) in v.iter() {
818            let value = v.clone();
819            if let Some(p) = value.to_prim() {
820                res.insert(k.clone(), p.clone());
821            }
822        }
823        res
824    }
825}
826impl From<&MValue> for MapboxProperties {
827    fn from(v: &MValue) -> MapboxProperties {
828        let mut res = MapboxProperties::new();
829        // Only copy over primitive values
830        for (k, v) in v.iter() {
831            let value = v.clone();
832            if let Some(p) = value.to_prim() {
833                res.insert(k.clone(), p.clone());
834            }
835        }
836        res
837    }
838}
839
840#[cfg(test)]
841mod tests {
842    use super::*;
843    use crate::{MValue, MValueCompatible, VectorPoint};
844    use alloc::vec;
845
846    #[test]
847    fn value_default() {
848        let default = ValueType::default();
849        assert_eq!(default, ValueType::Primitive(PrimitiveValue::Null));
850    }
851
852    #[test]
853    fn primitive_value_funcs() {
854        // &str
855        let prim_value: PrimitiveValue = "test".into();
856        assert_eq!(PrimitiveValue::String("test".into()), prim_value);
857        assert_eq!(prim_value.to_u64(), None);
858        assert_eq!(prim_value.to_i64(), None);
859        assert_eq!(prim_value.to_f32(), None);
860        assert_eq!(prim_value.to_f64(), None);
861        assert_eq!(prim_value.to_bool(), None);
862        assert!(!prim_value.is_null());
863        // String
864        let prim_value_str: String = "test".into();
865        let prim_value: PrimitiveValue = prim_value_str.clone().into();
866        assert_eq!(PrimitiveValue::String("test".into()), prim_value);
867        assert_eq!(prim_value.to_string(), Some("test".into()));
868        // u64
869        let prim_value: PrimitiveValue = 1_u64.into();
870        assert_eq!(PrimitiveValue::U64(1), prim_value);
871        assert_eq!(prim_value.to_string(), None);
872        assert_eq!(prim_value.to_u64(), Some(1));
873        assert_eq!(prim_value.to_i64(), Some(1));
874        assert_eq!(prim_value.to_f32(), Some(1.0));
875        assert_eq!(prim_value.to_f64(), Some(1.0));
876        // i64
877        let prim_value: PrimitiveValue = (-1_i64).into();
878        assert_eq!(PrimitiveValue::I64(-1), prim_value);
879        assert_eq!(prim_value.to_u64(), Some(18446744073709551615));
880        assert_eq!(prim_value.to_i64(), Some(-1));
881        assert_eq!(prim_value.to_f32(), Some(-1.0));
882        assert_eq!(prim_value.to_f64(), Some(-1.0));
883        // f32
884        let prim_value: PrimitiveValue = (1.0_f32).into();
885        assert_eq!(PrimitiveValue::F32(1.0), prim_value);
886        assert_eq!(prim_value.to_u64(), Some(1));
887        assert_eq!(prim_value.to_i64(), Some(1));
888        assert_eq!(prim_value.to_f32(), Some(1.0));
889        assert_eq!(prim_value.to_f64(), Some(1.0));
890        // f64
891        let prim_value: PrimitiveValue = (1.0_f64).into();
892        assert_eq!(PrimitiveValue::F64(1.0), prim_value);
893        assert_eq!(prim_value.to_u64(), Some(1));
894        assert_eq!(prim_value.to_i64(), Some(1));
895        assert_eq!(prim_value.to_f32(), Some(1.0));
896        assert_eq!(prim_value.to_f64(), Some(1.0));
897        // bool
898        let prim_value: PrimitiveValue = true.into();
899        assert_eq!(PrimitiveValue::Bool(true), prim_value);
900        assert_eq!(prim_value.to_bool(), Some(true));
901        // ()
902        let prim_value: PrimitiveValue = ().into();
903        assert_eq!(PrimitiveValue::Null, prim_value);
904        assert!(prim_value.is_null());
905        // Option
906        let prim_value: PrimitiveValue = Some(true).into();
907        assert_eq!(PrimitiveValue::Bool(true), prim_value);
908        assert_eq!(prim_value.to_bool(), Some(true));
909        let prim_value: PrimitiveValue = None::<bool>.into();
910        assert_eq!(PrimitiveValue::Null, prim_value);
911        assert!(prim_value.is_null());
912    }
913
914    #[test]
915    fn value_prim_type_funcs() {
916        // &str
917        let prim_value: ValuePrimitiveType = "test".into();
918        assert_eq!(
919            ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
920            prim_value
921        );
922        assert_eq!(prim_value.to_prim(), Some(PrimitiveValue::String("test".into())).as_ref());
923        assert_eq!(prim_value.to_nested(), None);
924        // String
925        let prim_value_str: String = "test".into();
926        let prim_value: ValuePrimitiveType = prim_value_str.clone().into();
927        assert_eq!(
928            ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
929            prim_value
930        );
931        // u64
932        let prim_value: ValuePrimitiveType = 1_u64.into();
933        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::U64(1)), prim_value);
934        // i64
935        let prim_value: ValuePrimitiveType = (-1_i64).into();
936        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::I64(-1)), prim_value);
937        // f32
938        let prim_value: ValuePrimitiveType = (1.0_f32).into();
939        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::F32(1.0)), prim_value);
940        // f64
941        let prim_value: ValuePrimitiveType = (1.0_f64).into();
942        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::F64(1.0)), prim_value);
943        // bool
944        let prim_value: ValuePrimitiveType = true.into();
945        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Bool(true)), prim_value);
946        // ()
947        let prim_value: ValuePrimitiveType = ().into();
948        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Null), prim_value);
949
950        // from prim
951        let nested: ValuePrimitiveType = PrimitiveValue::Bool(true).into();
952        assert_eq!(nested.to_prim().unwrap().to_bool(), Some(true));
953
954        // nested
955        let nested: ValuePrimitiveType =
956            ValuePrimitive::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into())]).into();
957        assert_eq!(nested.to_prim(), None);
958        assert_eq!(
959            nested.to_nested(),
960            Some(ValuePrimitive::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),]))
961                .as_ref()
962        );
963
964        // option
965        let prim_value: ValuePrimitiveType = Some(true).into();
966        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Bool(true)), prim_value);
967        let prim_value: ValuePrimitiveType = None::<bool>.into();
968        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Null), prim_value);
969    }
970
971    #[test]
972    fn value_funcs() {
973        // &str
974        let prim_value: ValueType = "test".into();
975        assert_eq!(ValueType::Primitive(PrimitiveValue::String("test".into())), prim_value);
976        let prim = prim_value.to_prim().unwrap();
977        assert_eq!(*prim, PrimitiveValue::String("test".into()));
978        assert_eq!(prim_value.to_nested(), None);
979        assert_eq!(prim_value.to_vec(), None);
980        // String
981        let prim_value_str: String = "test".into();
982        let prim_value: ValueType = prim_value_str.into();
983        assert_eq!(ValueType::Primitive(PrimitiveValue::String("test".into())), prim_value);
984        // u64
985        let prim_value: ValueType = 1_u64.into();
986        assert_eq!(ValueType::Primitive(PrimitiveValue::U64(1)), prim_value);
987        // i64
988        let prim_value: ValueType = (-1_i64).into();
989        assert_eq!(ValueType::Primitive(PrimitiveValue::I64(-1)), prim_value);
990        // f32
991        let prim_value: ValueType = (1.0_f32).into();
992        assert_eq!(ValueType::Primitive(PrimitiveValue::F32(1.0)), prim_value);
993        // f64
994        let prim_value: ValueType = (1.0_f64).into();
995        assert_eq!(ValueType::Primitive(PrimitiveValue::F64(1.0)), prim_value);
996        // bool
997        let prim_value: ValueType = true.into();
998        assert_eq!(ValueType::Primitive(PrimitiveValue::Bool(true)), prim_value);
999        // ()
1000        let prim_value: ValueType = ().into();
1001        assert_eq!(ValueType::Primitive(PrimitiveValue::Null), prim_value);
1002
1003        // vec
1004        let prim_value: ValueType = vec!["test", "test2"].into();
1005        assert_eq!(prim_value.to_prim(), None);
1006        assert_eq!(
1007            ValueType::Array(vec![
1008                ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
1009                ValuePrimitiveType::Primitive(PrimitiveValue::String("test2".into())),
1010            ]),
1011            prim_value
1012        );
1013        let back_to_vec: Vec<String> =
1014            prim_value.to_vec().unwrap().iter().filter_map(|v| v.to_prim()?.to_string()).collect();
1015        assert_eq!(back_to_vec, vec!["test", "test2"]);
1016
1017        // nested
1018        let nested: ValueType =
1019            Value::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into())]).into();
1020        assert_eq!(nested.to_vec(), None);
1021        assert_eq!(
1022            nested.to_nested(),
1023            Some(Value::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),])).as_ref()
1024        );
1025
1026        // option
1027        let prim_value: ValueType = Some(true).into();
1028        assert_eq!(ValueType::Primitive(PrimitiveValue::Bool(true)), prim_value);
1029        let prim_value: ValueType = None::<bool>.into();
1030        assert_eq!(ValueType::Primitive(PrimitiveValue::Null), prim_value);
1031    }
1032
1033    #[test]
1034    fn test_rgba_struct() {
1035        #[derive(Debug, Clone, Copy, PartialEq, Default)]
1036        pub struct Rgba {
1037            /// Gamma corrected Red between 0 and 1
1038            pub r: f64,
1039            /// Gamma corrected Green between 0 and 1
1040            pub g: f64,
1041            /// Gamma corrected Blue between 0 and 1
1042            pub b: f64,
1043            /// Opacity between 0 and 1 (not gamma corrected as opacity is linear)
1044            pub a: f64,
1045        }
1046        impl Rgba {
1047            /// Create a new RGBA value
1048            pub fn new(r: f64, g: f64, b: f64, a: f64) -> Self {
1049                Self { r, g, b, a }
1050            }
1051        }
1052        impl MValueCompatible for Rgba {}
1053        impl From<Rgba> for MValue {
1054            fn from(rgba: Rgba) -> MValue {
1055                MValue::from([
1056                    ("r".into(), (rgba.r).into()),
1057                    ("g".into(), (rgba.g).into()),
1058                    ("b".into(), (rgba.b).into()),
1059                    ("a".into(), (rgba.a).into()),
1060                ])
1061            }
1062        }
1063        impl From<&Rgba> for MValue {
1064            fn from(rgba: &Rgba) -> MValue {
1065                MValue::from([
1066                    ("r".into(), (rgba.r).into()),
1067                    ("g".into(), (rgba.g).into()),
1068                    ("b".into(), (rgba.b).into()),
1069                    ("a".into(), (rgba.a).into()),
1070                ])
1071            }
1072        }
1073        impl From<MValue> for Rgba {
1074            fn from(mvalue: MValue) -> Self {
1075                let r: f64 = mvalue.get("r").unwrap().to_prim().unwrap().to_f64().unwrap();
1076                let g = mvalue.get("g").unwrap().to_prim().unwrap().to_f64().unwrap();
1077                let b = mvalue.get("b").unwrap().to_prim().unwrap().to_f64().unwrap();
1078                let a = mvalue.get("a").unwrap().to_prim().unwrap().to_f64().unwrap();
1079                Rgba::new(r, g, b, a)
1080            }
1081        }
1082        impl From<&MValue> for Rgba {
1083            fn from(mvalue: &MValue) -> Self {
1084                let r: f64 = mvalue.get("r").unwrap().to_prim().unwrap().to_f64().unwrap();
1085                let g = mvalue.get("g").unwrap().to_prim().unwrap().to_f64().unwrap();
1086                let b = mvalue.get("b").unwrap().to_prim().unwrap().to_f64().unwrap();
1087                let a = mvalue.get("a").unwrap().to_prim().unwrap().to_f64().unwrap();
1088                Rgba::new(r, g, b, a)
1089            }
1090        }
1091
1092        let rgba = Rgba::new(0.1, 0.2, 0.3, 0.4);
1093        let rgba_mvalue: MValue = (&rgba).into();
1094        assert_eq!(
1095            rgba_mvalue,
1096            MValue::from([
1097                ("r".into(), ValueType::Primitive(PrimitiveValue::F64(0.1))),
1098                ("g".into(), ValueType::Primitive(PrimitiveValue::F64(0.2))),
1099                ("b".into(), ValueType::Primitive(PrimitiveValue::F64(0.3))),
1100                ("a".into(), ValueType::Primitive(PrimitiveValue::F64(0.4))),
1101            ])
1102        );
1103        let back_to_rgba: Rgba = (&rgba_mvalue).into();
1104        assert_eq!(rgba, back_to_rgba);
1105        let back_to_rgba: Rgba = rgba_mvalue.clone().into();
1106        assert_eq!(rgba, back_to_rgba);
1107
1108        let vp: VectorPoint<Rgba> = VectorPoint { x: 1.0, y: 2.0, z: None, m: Some(rgba), t: None };
1109        let vp_mvalue: MValue = vp.m.unwrap().into();
1110        assert_eq!(vp_mvalue, rgba_mvalue);
1111
1112        // distance
1113        let a: VectorPoint<Rgba> = VectorPoint { x: 1.0, y: 2.0, z: None, m: Some(rgba), t: None };
1114        let b: VectorPoint = VectorPoint::new(3.0, 4.0, None, None);
1115        let dist = a.distance(&b);
1116        assert_eq!(dist, 2.8284271247461903);
1117    }
1118
1119    #[test]
1120    fn to_mapbox() {
1121        let value: MValue = MValue::from([
1122            ("a".into(), "b".into()),
1123            ("c".into(), 2.0_f32.into()),
1124            (
1125                "d".into(),
1126                MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
1127            ),
1128        ]);
1129        let mapbox_value: MapboxProperties = value.clone().into();
1130        assert_eq!(
1131            mapbox_value,
1132            MapboxProperties::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),])
1133        );
1134        let mapbox_value: MapboxProperties = (&value).into();
1135        assert_eq!(
1136            mapbox_value,
1137            MapboxProperties::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),])
1138        );
1139    }
1140
1141    #[test]
1142    fn from_mapbox() {
1143        let mapbox_value: MapboxProperties = MapboxProperties::from([("a".into(), "b".into())]);
1144        let value: MValue = mapbox_value.clone().into();
1145        assert_eq!(value, MValue::from([("a".into(), "b".into()),]));
1146        let value: MValue = (&mapbox_value).into();
1147        assert_eq!(value, MValue::from([("a".into(), "b".into()),]));
1148    }
1149
1150    #[test]
1151    fn to_json_obj() {
1152        let value: MValue = MValue::from([
1153            ("a".into(), "b".into()),
1154            ("c".into(), 2.0_f32.into()),
1155            (
1156                "d".into(),
1157                MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
1158            ),
1159            (
1160                "e".into(),
1161                Vec::<ValuePrimitiveType>::from(["a".into(), "b".into(), "c".into()]).into(),
1162            ),
1163        ]);
1164        let json_value: JSONProperties = value.clone().into();
1165        assert_eq!(
1166            json_value,
1167            JSONProperties::from([
1168                ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
1169                ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1170                (
1171                    "d".into(),
1172                    JSONValue::Object(JSONProperties::from([
1173                        ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
1174                        ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1175                    ]))
1176                ),
1177                (
1178                    "e".into(),
1179                    JSONValue::Array(Vec::from([
1180                        JSONValue::Primitive(PrimitiveValue::String("a".into())),
1181                        JSONValue::Primitive(PrimitiveValue::String("b".into())),
1182                        JSONValue::Primitive(PrimitiveValue::String("c".into())),
1183                    ]))
1184                ),
1185            ])
1186        );
1187        let json_value: JSONProperties = (&value).into();
1188        assert_eq!(
1189            json_value,
1190            JSONProperties::from([
1191                ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
1192                ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1193                (
1194                    "d".into(),
1195                    JSONValue::Object(JSONProperties::from([
1196                        ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
1197                        ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1198                    ]))
1199                ),
1200                (
1201                    "e".into(),
1202                    JSONValue::Array(Vec::from([
1203                        JSONValue::Primitive(PrimitiveValue::String("a".into())),
1204                        JSONValue::Primitive(PrimitiveValue::String("b".into())),
1205                        JSONValue::Primitive(PrimitiveValue::String("c".into())),
1206                    ]))
1207                ),
1208            ])
1209        );
1210
1211        // get prim
1212        let prim_a = json_value.get("a").unwrap().to_prim().unwrap().to_string().unwrap();
1213        assert_eq!(prim_a, "b");
1214        let failed_to_prim = json_value.get("d").unwrap().to_prim();
1215        assert_eq!(failed_to_prim, None);
1216
1217        // get array
1218        let array_e = json_value.get("e").unwrap().to_vec().unwrap();
1219        assert_eq!(
1220            *array_e,
1221            Vec::from([
1222                JSONValue::Primitive(PrimitiveValue::String("a".into())),
1223                JSONValue::Primitive(PrimitiveValue::String("b".into())),
1224                JSONValue::Primitive(PrimitiveValue::String("c".into())),
1225            ])
1226        );
1227        let array_fail = json_value.get("a").unwrap().to_vec();
1228        assert_eq!(array_fail, None);
1229
1230        // get obj
1231        let obj_d = json_value.get("d").unwrap().to_nested().unwrap();
1232        assert_eq!(
1233            *obj_d,
1234            JSONProperties::from([
1235                ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
1236                ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1237            ])
1238        );
1239        let obj_fail = json_value.get("a").unwrap().to_nested();
1240        assert_eq!(obj_fail, None);
1241    }
1242
1243    #[test]
1244    fn from_json_obj() {
1245        let json_value = JSONProperties::from([
1246            ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
1247            ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1248            (
1249                "d".into(),
1250                JSONValue::Object(JSONProperties::from([
1251                    ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
1252                    ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1253                ])),
1254            ),
1255            (
1256                "e".into(),
1257                JSONValue::Array(Vec::from([
1258                    JSONValue::Primitive(PrimitiveValue::String("a".into())),
1259                    JSONValue::Primitive(PrimitiveValue::String("b".into())),
1260                    JSONValue::Primitive(PrimitiveValue::String("c".into())),
1261                ])),
1262            ),
1263        ]);
1264        let value: MValue = json_value.clone().into();
1265        assert_eq!(
1266            value,
1267            MValue::from([
1268                ("a".into(), "b".into()),
1269                ("c".into(), 2.0_f32.into()),
1270                (
1271                    "d".into(),
1272                    MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
1273                ),
1274                (
1275                    "e".into(),
1276                    Vec::<ValuePrimitiveType>::from(["a".into(), "b".into(), "c".into()]).into(),
1277                ),
1278            ])
1279        );
1280        let value: MValue = (&json_value).into();
1281        assert_eq!(
1282            value,
1283            MValue::from([
1284                ("a".into(), "b".into()),
1285                ("c".into(), 2.0_f32.into()),
1286                (
1287                    "d".into(),
1288                    MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
1289                ),
1290                (
1291                    "e".into(),
1292                    Vec::<ValuePrimitiveType>::from(["a".into(), "b".into(), "c".into()]).into(),
1293                ),
1294            ])
1295        );
1296    }
1297
1298    #[test]
1299    fn test_prim_to_json() {
1300        let json: JSONValue = (&PrimitiveValue::String("test".into())).into();
1301        assert_eq!(json, JSONValue::Primitive(PrimitiveValue::String("test".into())));
1302
1303        let prim: PrimitiveValue = (&json).into();
1304        assert_eq!(prim, PrimitiveValue::String("test".into()));
1305
1306        // to prim but json is not a prim
1307        let json = JSONValue::Array(Vec::new());
1308        let prim: PrimitiveValue = (&json).into();
1309        assert_eq!(prim, PrimitiveValue::Null);
1310    }
1311
1312    #[test]
1313    fn test_value_prim_type_to_json() {
1314        let prim = ValuePrimitiveType::NestedPrimitive(Map::from([
1315            ("a".into(), "b".into()),
1316            ("c".into(), 2.0_f32.into()),
1317        ]));
1318        let json: JSONValue = (&prim).into();
1319        assert_eq!(
1320            json,
1321            JSONValue::Object(JSONProperties::from([
1322                ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
1323                ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1324            ]))
1325        );
1326
1327        let json = JSONValue::Object(JSONProperties::from([
1328            ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
1329            ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1330        ]));
1331
1332        let prim: ValuePrimitiveType = (&json).into();
1333        assert_eq!(
1334            prim,
1335            ValuePrimitiveType::NestedPrimitive(Map::from([
1336                ("2".into(), "3".into()),
1337                ("4".into(), 2.0_f32.into()),
1338            ]))
1339        );
1340
1341        // Array Fails
1342        let json = JSONValue::Array(Vec::from([
1343            JSONValue::Primitive(PrimitiveValue::String("c".into())),
1344            JSONValue::Primitive(PrimitiveValue::String("d".into())),
1345        ]));
1346
1347        let prim: ValuePrimitiveType = (&json).into();
1348        assert_eq!(prim, ValuePrimitiveType::Primitive(PrimitiveValue::Null));
1349    }
1350
1351    #[test]
1352    fn test_prim_to_value_prim_type() {
1353        // f32
1354        let value: ValuePrimitiveType = 2.0_f32.into();
1355        assert_eq!(value, ValuePrimitiveType::Primitive(2.0_f32.into()));
1356        let back_to_num: f32 = (&value).into();
1357        assert_eq!(back_to_num, 2.0_f32);
1358        // f64
1359        let value: ValuePrimitiveType = (-2.2_f64).into();
1360        assert_eq!(value, ValuePrimitiveType::Primitive((-2.2_f64).into()));
1361        let back_to_num: f64 = (&value).into();
1362        assert_eq!(back_to_num, (-2.2_f64));
1363        // u64
1364        let value: ValuePrimitiveType = 2_u64.into();
1365        assert_eq!(value, ValuePrimitiveType::Primitive(2_u64.into()));
1366        let back_to_num: u64 = (&value).into();
1367        assert_eq!(back_to_num, 2_u64);
1368        // i64
1369        let value: ValuePrimitiveType = 2_i64.into();
1370        assert_eq!(value, ValuePrimitiveType::Primitive(2_i64.into()));
1371        let back_to_num: i64 = (&value).into();
1372        assert_eq!(back_to_num, 2_i64);
1373        let back_to_str: String = (&value).into();
1374        assert_eq!(back_to_str, "");
1375
1376        // string
1377        let value: ValuePrimitiveType = "test".into();
1378        assert_eq!(value, ValuePrimitiveType::Primitive("test".into()));
1379        let back_to_str: String = (&value).into();
1380        assert_eq!(back_to_str, "test");
1381        let back_to_num: i64 = (&value).into();
1382        assert_eq!(back_to_num, 0);
1383        let back_to_num: u64 = (&value).into();
1384        assert_eq!(back_to_num, 0);
1385        let back_to_num: f32 = (&value).into();
1386        assert_eq!(back_to_num, 0.);
1387        let back_to_num: f64 = (&value).into();
1388        assert_eq!(back_to_num, 0.);
1389        let back_to_bool: bool = (&value).into();
1390        assert!(!back_to_bool);
1391        // bool
1392        let value: ValuePrimitiveType = true.into();
1393        assert_eq!(value, ValuePrimitiveType::Primitive(true.into()));
1394        let back_to_bool: bool = (&value).into();
1395        assert!(back_to_bool);
1396        // ()
1397        let value: ValuePrimitiveType = ().into();
1398        assert_eq!(value, ValuePrimitiveType::Primitive(PrimitiveValue::Null));
1399        let _back_to_base: () = (&value).into();
1400
1401        // PrimitiveValue
1402        let value: ValuePrimitiveType = PrimitiveValue::Null.into();
1403        assert_eq!(value, ValuePrimitiveType::Primitive(PrimitiveValue::Null));
1404        let back_to_base: PrimitiveValue = (&value).into();
1405        assert_eq!(back_to_base, PrimitiveValue::Null);
1406        let value: ValuePrimitiveType = Map::new().into();
1407        let bac_to_base: PrimitiveValue = (&value).into();
1408        assert_eq!(bac_to_base, PrimitiveValue::Null);
1409
1410        // ValuePrimitive
1411        let value: ValuePrimitiveType = ValuePrimitive::new().into();
1412        let back_to_base: ValuePrimitive = (&value).into();
1413        assert_eq!(back_to_base, ValuePrimitive::new());
1414        let value: ValuePrimitiveType = PrimitiveValue::Null.into();
1415        let back_to_base: ValuePrimitive = (&value).into();
1416        assert_eq!(back_to_base, ValuePrimitive::new());
1417    }
1418
1419    #[test]
1420    fn test_value_type() {
1421        // ref string value
1422        let val_type: ValueType = "test".into();
1423        assert_eq!(val_type, ValueType::Primitive(PrimitiveValue::String("test".into())));
1424        let back_to_str: String = (&val_type).into();
1425        assert_eq!(back_to_str, "test");
1426        // direct string value
1427        let string: String = "test".into();
1428        let val_type: ValueType = (&string).into();
1429        assert_eq!(val_type, ValueType::Primitive(PrimitiveValue::String("test".into())));
1430        let back_to_str: String = val_type.into();
1431        assert_eq!(back_to_str, "test");
1432
1433        // direct fake a string
1434        let value: ValueType = 2_i64.into();
1435        let back_to_str: String = (&value).into();
1436        assert_eq!(back_to_str, "");
1437        // ref fake a string
1438        let value: ValueType = 2_i64.into();
1439        let back_to_str: String = value.into();
1440        assert_eq!(back_to_str, "");
1441
1442        // f32
1443        let value: ValueType = 2.0_f32.into();
1444        assert_eq!(value, ValueType::Primitive(2.0_f32.into()));
1445        let back_to_num: f32 = (&value).into();
1446        assert_eq!(back_to_num, 2.0_f32);
1447        // f32 no ref
1448        let value: ValueType = 2.0_f32.into();
1449        assert_eq!(value, ValueType::Primitive(2.0_f32.into()));
1450        let back_to_num: f32 = value.into();
1451        assert_eq!(back_to_num, 2.0_f32);
1452        // f64
1453        let value: ValueType = (-2.2_f64).into();
1454        assert_eq!(value, ValueType::Primitive((-2.2_f64).into()));
1455        let back_to_num: f64 = (&value).into();
1456        assert_eq!(back_to_num, (-2.2_f64));
1457        // f64 no ref
1458        let value: ValueType = (-2.2_f64).into();
1459        assert_eq!(value, ValueType::Primitive((-2.2_f64).into()));
1460        let back_to_num: f64 = value.into();
1461        assert_eq!(back_to_num, (-2.2_f64));
1462        // u64
1463        let value: ValueType = 2_u64.into();
1464        assert_eq!(value, ValueType::Primitive(2_u64.into()));
1465        let back_to_num: u64 = (&value).into();
1466        assert_eq!(back_to_num, 2_u64);
1467        // u64 no ref
1468        let value: ValueType = 2_u64.into();
1469        assert_eq!(value, ValueType::Primitive(2_u64.into()));
1470        let back_to_num: u64 = value.into();
1471        assert_eq!(back_to_num, 2_u64);
1472        // i64
1473        let value: ValueType = 2_i64.into();
1474        assert_eq!(value, ValueType::Primitive(2_i64.into()));
1475        let back_to_num: i64 = (&value).into();
1476        assert_eq!(back_to_num, 2_i64);
1477        let back_to_str: String = (&value).into();
1478        assert_eq!(back_to_str, "");
1479        // i64 no ref
1480        let value: ValueType = 2_i64.into();
1481        assert_eq!(value, ValueType::Primitive(2_i64.into()));
1482        let back_to_num: i64 = value.into();
1483        assert_eq!(back_to_num, 2_i64);
1484
1485        // string
1486        let value: ValueType = "test".into();
1487        assert_eq!(value, ValueType::Primitive("test".into()));
1488        let back_to_str: String = (&value).into();
1489        assert_eq!(back_to_str, "test");
1490        let back_to_num: i64 = (&value).into();
1491        assert_eq!(back_to_num, 0);
1492        let back_to_num: u64 = (&value).into();
1493        assert_eq!(back_to_num, 0);
1494        let back_to_num: usize = (&value).into();
1495        assert_eq!(back_to_num, 0);
1496        let back_to_num: f32 = (&value).into();
1497        assert_eq!(back_to_num, 0.);
1498        let back_to_num: f64 = (&value).into();
1499        assert_eq!(back_to_num, 0.);
1500        let back_to_bool: bool = (&value).into();
1501        assert!(!back_to_bool);
1502        // string no ref
1503        let value: ValueType = "test".into();
1504        assert_eq!(value, ValueType::Primitive("test".into()));
1505        let back_to_str: String = value.clone().into();
1506        assert_eq!(back_to_str, "test");
1507        let back_to_num: i64 = value.clone().into();
1508        assert_eq!(back_to_num, 0);
1509        let back_to_num: u64 = value.clone().into();
1510        assert_eq!(back_to_num, 0);
1511        let back_to_num: usize = value.clone().into();
1512        assert_eq!(back_to_num, 0);
1513        let back_to_num: f32 = value.clone().into();
1514        assert_eq!(back_to_num, 0.);
1515        let back_to_num: f64 = value.clone().into();
1516        assert_eq!(back_to_num, 0.);
1517        let back_to_bool: bool = value.into();
1518        assert!(!back_to_bool);
1519        // bool
1520        let value: ValueType = true.into();
1521        assert_eq!(value, ValueType::Primitive(true.into()));
1522        let back_to_bool: bool = (&value).into();
1523        assert!(back_to_bool);
1524        // bool no ref
1525        let value: ValueType = true.into();
1526        assert_eq!(value, ValueType::Primitive(true.into()));
1527        let back_to_bool: bool = value.into();
1528        assert!(back_to_bool);
1529        // ()
1530        let value: ValueType = ().into();
1531        assert_eq!(value, ValueType::Primitive(PrimitiveValue::Null));
1532        let _back_to_base: () = (&value).into();
1533        // () no ref
1534        let value: ValueType = ().into();
1535        assert_eq!(value, ValueType::Primitive(PrimitiveValue::Null));
1536        let _back_to_base: () = value.into();
1537
1538        // vec
1539        let data: Vec<u64> = vec![1, 2, 3];
1540        let value: ValueType = data.into();
1541        let back_to_vec: Vec<u64> = (&value).into();
1542        assert_eq!(back_to_vec, vec![1, 2, 3]);
1543        // vec ref
1544        let data: Vec<u64> = vec![1, 2, 3];
1545        let value: ValueType = (&data).into();
1546        let back_to_vec: Vec<u64> = value.into();
1547        assert_eq!(back_to_vec, vec![1, 2, 3]);
1548        // vec ref i
1549        let data: Vec<isize> = vec![1, 2, 3];
1550        let value: ValueType = (&data).into();
1551        let back_to_vec: Vec<isize> = value.into();
1552        assert_eq!(back_to_vec, vec![1, 2, 3]);
1553        // vec from ValueType not array
1554        let value: ValueType = ValueType::Primitive("test".into());
1555        let back_to_vec: Vec<u64> = (&value).into();
1556        assert_eq!(back_to_vec, Vec::<u64>::new());
1557
1558        // Value
1559        let data: Value = Value::from([("a".into(), "b".into())]);
1560        let value: ValueType = data.into();
1561        let back_to_value: Value = value.into();
1562        assert_eq!(back_to_value, Value::from([("a".into(), "b".into())]));
1563        // Value ref
1564        let data: Value = Value::from([("a".into(), "b".into())]);
1565        let value: ValueType = data.into();
1566        let back_to_value: Value = value.into();
1567        assert_eq!(back_to_value, Value::from([("a".into(), "b".into())]));
1568
1569        // ValueType not nested
1570        let value: ValueType = ValueType::Primitive("test".into());
1571        let to_value: Value = value.into();
1572        assert_eq!(to_value, Value::default());
1573        // ValueType not nested ref
1574        let value: ValueType = ValueType::Primitive("test".into());
1575        let to_value: Value = (&value).into();
1576        assert_eq!(to_value, Value::default());
1577    }
1578}