s2json_core/impls/
value.rs

1use crate::*;
2use alloc::{string::String, vec, vec::Vec};
3use core::cmp::Ordering;
4use libm::round;
5use pbf::{ProtoRead, ProtoWrite, Protobuf, Type};
6
7// PrimitiveValue
8impl PrimitiveValue {
9    /// Returns true if the value is null
10    pub fn is_null(&self) -> bool {
11        matches!(self, PrimitiveValue::Null)
12    }
13
14    /// returns true if the shape is a number type
15    pub fn is_number(&self) -> bool {
16        matches!(
17            self,
18            PrimitiveValue::F64(_)
19                | PrimitiveValue::F32(_)
20                | PrimitiveValue::I64(_)
21                | PrimitiveValue::U64(_)
22        )
23    }
24
25    /// Converts a primitive value to a string
26    pub fn to_string(&self) -> Option<String> {
27        match self {
28            PrimitiveValue::String(v) => Some(v.clone()),
29            _ => None,
30        }
31    }
32
33    /// Converts a primitive value to a u64
34    pub fn to_u64(&self) -> Option<u64> {
35        match self {
36            PrimitiveValue::String(v) => v.parse().ok(),
37            PrimitiveValue::U64(v) => Some(*v),
38            PrimitiveValue::I64(v) => Some(*v as u64),
39            PrimitiveValue::F64(v) => Some(round(*v) as u64),
40            PrimitiveValue::F32(v) => Some(round((*v).into()) as u64),
41            _ => None,
42        }
43    }
44
45    /// Converts a primitive value to a i64
46    pub fn to_i64(&self) -> Option<i64> {
47        match self {
48            PrimitiveValue::String(v) => v.parse().ok(),
49            PrimitiveValue::U64(v) => Some(*v as i64),
50            PrimitiveValue::I64(v) => Some(*v),
51            PrimitiveValue::F64(v) => Some(round(*v) as i64),
52            PrimitiveValue::F32(v) => Some(round((*v).into()) as i64),
53            _ => None,
54        }
55    }
56
57    /// Converts a primitive value to a f64
58    pub fn to_f64(&self) -> Option<f64> {
59        match self {
60            PrimitiveValue::String(v) => v.parse().ok(),
61            PrimitiveValue::U64(v) => Some(*v as f64),
62            PrimitiveValue::I64(v) => Some(*v as f64),
63            PrimitiveValue::F64(v) => Some(*v),
64            PrimitiveValue::F32(v) => Some(*v as f64),
65            _ => None,
66        }
67    }
68
69    /// Converts a primitive value to a f32
70    pub fn to_f32(&self) -> Option<f32> {
71        match self {
72            PrimitiveValue::String(v) => v.parse().ok(),
73            PrimitiveValue::U64(v) => Some(*v as f32),
74            PrimitiveValue::I64(v) => Some(*v as f32),
75            PrimitiveValue::F64(v) => Some(*v as f32),
76            PrimitiveValue::F32(v) => Some(*v),
77            _ => None,
78        }
79    }
80
81    /// Converts a primitive value to a bool
82    pub fn to_bool(&self) -> Option<bool> {
83        match self {
84            PrimitiveValue::String(v) => {
85                if v == "true" {
86                    Some(true)
87                } else if v == "false" {
88                    Some(false)
89                } else {
90                    None
91                }
92            }
93            PrimitiveValue::Bool(v) => Some(*v),
94            _ => None,
95        }
96    }
97}
98impl From<&str> for PrimitiveValue {
99    fn from(s: &str) -> Self {
100        PrimitiveValue::String(s.into())
101    }
102}
103impl From<String> for PrimitiveValue {
104    fn from(s: String) -> Self {
105        PrimitiveValue::String(s)
106    }
107}
108impl From<&PrimitiveValue> for String {
109    fn from(v: &PrimitiveValue) -> Self {
110        v.to_string().unwrap_or_default()
111    }
112}
113// Implement for u8, u16, u32, u64
114macro_rules! impl_from_uint_to_prim_val {
115    ($($t:ty),*) => {
116        $(
117            impl From<$t> for PrimitiveValue {
118                fn from(v: $t) -> Self {
119                    PrimitiveValue::U64(v as u64)
120                }
121            }
122        )*
123    };
124}
125impl_from_uint_to_prim_val!(u8, u16, u32, u64, usize);
126macro_rules! impl_from_value_prim_to_uint {
127    ($($t:ty),*) => {
128        $(
129            impl From<&PrimitiveValue> for $t {
130                fn from(v: &PrimitiveValue) -> Self {
131                    v.to_u64().unwrap_or_default() as $t
132                }
133            }
134        )*
135    };
136}
137impl_from_value_prim_to_uint!(u8, u16, u32, u64, usize);
138// Implement for i8, i16, i32, i64, and isize
139macro_rules! impl_from_sint_to_prim_val {
140    ($($t:ty),*) => {
141        $(
142            impl From<$t> for PrimitiveValue {
143                fn from(v: $t) -> Self {
144                    PrimitiveValue::I64(v as i64)
145                }
146            }
147        )*
148    };
149}
150impl_from_sint_to_prim_val!(i8, i16, i32, i64, isize);
151macro_rules! impl_from_value_prim_to_sint {
152    ($($t:ty),*) => {
153        $(
154            impl From<&PrimitiveValue> for $t {
155                fn from(v: &PrimitiveValue) -> Self {
156                    v.to_i64().unwrap_or_default() as $t
157                }
158            }
159        )*
160    };
161}
162impl_from_value_prim_to_sint!(i8, i16, i32, i64, isize);
163impl From<f32> for PrimitiveValue {
164    fn from(v: f32) -> Self {
165        PrimitiveValue::F32(v)
166    }
167}
168impl From<&PrimitiveValue> for f32 {
169    fn from(v: &PrimitiveValue) -> Self {
170        v.to_f32().unwrap_or_default()
171    }
172}
173impl From<f64> for PrimitiveValue {
174    fn from(v: f64) -> Self {
175        PrimitiveValue::F64(v)
176    }
177}
178impl From<&PrimitiveValue> for f64 {
179    fn from(v: &PrimitiveValue) -> Self {
180        v.to_f64().unwrap_or_default()
181    }
182}
183impl From<bool> for PrimitiveValue {
184    fn from(v: bool) -> Self {
185        PrimitiveValue::Bool(v)
186    }
187}
188impl From<&PrimitiveValue> for bool {
189    fn from(v: &PrimitiveValue) -> Self {
190        v.to_bool().unwrap_or_default()
191    }
192}
193impl From<()> for PrimitiveValue {
194    fn from(_: ()) -> Self {
195        PrimitiveValue::Null
196    }
197}
198impl From<&PrimitiveValue> for () {
199    fn from(_v: &PrimitiveValue) -> Self {}
200}
201impl<T> From<Option<T>> for PrimitiveValue
202where
203    T: Into<PrimitiveValue>,
204{
205    fn from(v: Option<T>) -> Self {
206        match v {
207            Some(v) => v.into(),
208            None => PrimitiveValue::Null,
209        }
210    }
211}
212impl From<&PrimitiveValue> for JSONValue {
213    fn from(v: &PrimitiveValue) -> Self {
214        JSONValue::Primitive(v.clone())
215    }
216}
217impl From<&JSONValue> for PrimitiveValue {
218    fn from(v: &JSONValue) -> Self {
219        match v {
220            JSONValue::Primitive(v) => v.clone(),
221            // DROPS VALUES THAT ARE NOT PRIMITIVES
222            _ => PrimitiveValue::Null,
223        }
224    }
225}
226impl PartialEq for PrimitiveValue {
227    fn eq(&self, other: &Self) -> bool {
228        match (self, other) {
229            (PrimitiveValue::String(a), PrimitiveValue::String(b)) => a == b,
230            (PrimitiveValue::U64(a), PrimitiveValue::U64(b)) => a == b,
231            (PrimitiveValue::I64(a), PrimitiveValue::I64(b)) => a == b,
232            (PrimitiveValue::F32(a), PrimitiveValue::F32(b)) => a.to_bits() == b.to_bits(),
233            (PrimitiveValue::F64(a), PrimitiveValue::F64(b)) => a.to_bits() == b.to_bits(),
234            (PrimitiveValue::Bool(a), PrimitiveValue::Bool(b)) => a == b,
235            (PrimitiveValue::Null, PrimitiveValue::Null) => true,
236            _ => false,
237        }
238    }
239}
240impl Eq for PrimitiveValue {}
241impl PartialOrd for PrimitiveValue {
242    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
243        Some(self.cmp(other))
244    }
245}
246impl Ord for PrimitiveValue {
247    fn cmp(&self, other: &Self) -> Ordering {
248        fn type_order(value: &PrimitiveValue) -> u8 {
249            match value {
250                PrimitiveValue::Null => 0,
251                PrimitiveValue::Bool(_) => 1,
252                PrimitiveValue::I64(_) => 2,
253                PrimitiveValue::U64(_) => 3,
254                PrimitiveValue::F32(_) => 4,
255                PrimitiveValue::F64(_) => 5,
256                PrimitiveValue::String(_) => 6,
257            }
258        }
259
260        match (self, other) {
261            (PrimitiveValue::String(a), PrimitiveValue::String(b)) => a.cmp(b),
262            (PrimitiveValue::U64(a), PrimitiveValue::U64(b)) => a.cmp(b),
263            (PrimitiveValue::I64(a), PrimitiveValue::I64(b)) => a.cmp(b),
264            (PrimitiveValue::F32(a), PrimitiveValue::F32(b)) => a.to_bits().cmp(&b.to_bits()),
265            (PrimitiveValue::F64(a), PrimitiveValue::F64(b)) => a.to_bits().cmp(&b.to_bits()),
266            (PrimitiveValue::Bool(a), PrimitiveValue::Bool(b)) => a.cmp(b),
267            (PrimitiveValue::Null, PrimitiveValue::Null) => Ordering::Equal,
268            // Different types: Order by predefined ranking
269            _ => type_order(self).cmp(&type_order(other)),
270        }
271    }
272}
273impl ProtoRead for PrimitiveValue {
274    fn read(&mut self, tag: u64, pb: &mut Protobuf) {
275        *self = match tag {
276            1 => PrimitiveValue::String(pb.read_string()),
277            2 => PrimitiveValue::F32(pb.read_varint()),
278            3 => PrimitiveValue::F64(pb.read_varint()),
279            5 => PrimitiveValue::U64(pb.read_varint()),
280            4 | 6 => PrimitiveValue::I64(pb.read_s_varint()),
281            7 => PrimitiveValue::Bool(pb.read_varint()),
282            _ => PrimitiveValue::Null,
283        }
284    }
285}
286impl ProtoWrite for PrimitiveValue {
287    fn write(&self, pbf: &mut Protobuf) {
288        match self {
289            PrimitiveValue::Null => pbf.write_field(0, Type::None),
290            PrimitiveValue::String(value) => pbf.write_string_field(1, value),
291            PrimitiveValue::F32(value) => pbf.write_varint_field(2, *value),
292            PrimitiveValue::F64(value) => pbf.write_varint_field(3, *value),
293            PrimitiveValue::U64(value) => pbf.write_varint_field(5, *value),
294            PrimitiveValue::I64(value) => pbf.write_s_varint_field(6, *value),
295            PrimitiveValue::Bool(value) => pbf.write_varint_field(7, *value),
296        }
297    }
298}
299
300// ValuePrimitiveType
301impl ValuePrimitiveType {
302    /// Returns the value as a primitive
303    pub fn to_prim(&self) -> Option<&PrimitiveValue> {
304        match self {
305            ValuePrimitiveType::Primitive(v) => Some(v),
306            _ => None,
307        }
308    }
309
310    /// Returns the value as a nested object
311    pub fn to_nested(&self) -> Option<&ValuePrimitive> {
312        match self {
313            ValuePrimitiveType::NestedPrimitive(v) => Some(v),
314            _ => None,
315        }
316    }
317}
318impl From<&str> for ValuePrimitiveType {
319    fn from(s: &str) -> Self {
320        ValuePrimitiveType::Primitive(PrimitiveValue::String(s.into()))
321    }
322}
323impl From<String> for ValuePrimitiveType {
324    fn from(s: String) -> Self {
325        ValuePrimitiveType::Primitive(PrimitiveValue::String(s))
326    }
327}
328impl From<&ValuePrimitiveType> for String {
329    fn from(v: &ValuePrimitiveType) -> Self {
330        match v {
331            ValuePrimitiveType::Primitive(PrimitiveValue::String(s)) => s.into(),
332            _ => "".into(),
333        }
334    }
335}
336// Implement for u8, u16, u32, u64
337macro_rules! impl_from_uint_uint {
338    ($($t:ty),*) => {
339        $(
340            impl From<$t> for ValuePrimitiveType {
341                fn from(v: $t) -> Self {
342                    ValuePrimitiveType::Primitive(PrimitiveValue::U64(v as u64))
343                }
344            }
345        )*
346    };
347}
348impl_from_uint_uint!(u8, u16, u32, u64, usize);
349macro_rules! impl_from_uint_ref {
350    ($($t:ty),*) => {
351        $(
352            impl<'a> From<&'a $t> for ValuePrimitiveType {
353                fn from(v: &$t) -> Self {
354                    ValuePrimitiveType::Primitive(PrimitiveValue::U64(*v as u64))
355                }
356            }
357        )*
358    };
359}
360impl_from_uint_ref!(u8, u16, u32, u64, usize);
361// Implement for u8, u16, u32, u64
362macro_rules! impl_from_prim_uint {
363    ($($t:ty),*) => {
364        $(
365            impl From<ValuePrimitiveType> for $t {
366                fn from(v: ValuePrimitiveType) -> Self {
367                    match v {
368                        ValuePrimitiveType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
369                        ValuePrimitiveType::Primitive(PrimitiveValue::U64(v)) => v as $t,
370                        _ => 0,
371                    }
372                }
373            }
374        )*
375    };
376}
377impl_from_prim_uint!(u8, u16, u32, u64, usize);
378macro_rules! impl_from_prim_ref_uint {
379    ($($t:ty),*) => {
380        $(
381            impl From<&ValuePrimitiveType> for $t {
382                fn from(v: &ValuePrimitiveType) -> Self {
383                    match v {
384                        ValuePrimitiveType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
385                        ValuePrimitiveType::Primitive(PrimitiveValue::U64(v)) => *v as $t,
386                        _ => 0,
387                    }
388                }
389            }
390        )*
391    };
392}
393impl_from_prim_ref_uint!(u8, u16, u32, u64, usize);
394// Implement for i8, i16, i32, i64, isize
395macro_rules! impl_from_sint_sint {
396    ($($t:ty),*) => {
397        $(
398            impl From<$t> for ValuePrimitiveType {
399                fn from(v: $t) -> Self {
400                    ValuePrimitiveType::Primitive(PrimitiveValue::I64(v as i64))
401                }
402            }
403        )*
404    };
405}
406impl_from_sint_sint!(i8, i16, i32, i64, isize);
407macro_rules! impl_from_sint_ref {
408    ($($t:ty),*) => {
409        $(
410            impl<'a> From<&'a $t> for ValuePrimitiveType {
411                fn from(v: &$t) -> Self {
412                    ValuePrimitiveType::Primitive(PrimitiveValue::I64(*v as i64))
413                }
414            }
415        )*
416    };
417}
418impl_from_sint_ref!(i8, i16, i32, i64, isize);
419// Implement for i8, i16, i32, i64, isize
420macro_rules! impl_from_prim_sint {
421    ($($t:ty),*) => {
422        $(
423            impl From<ValuePrimitiveType> for $t {
424                fn from(v: ValuePrimitiveType) -> Self {
425                    match v {
426                        ValuePrimitiveType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
427                        ValuePrimitiveType::Primitive(PrimitiveValue::I64(v)) => v as $t,
428                        _ => 0,
429                    }
430                }
431            }
432        )*
433    };
434}
435impl_from_prim_sint!(i8, i16, i32, i64, isize);
436macro_rules! impl_from_prim_ref_sint {
437    ($($t:ty),*) => {
438        $(
439            impl From<&ValuePrimitiveType> for $t {
440                fn from(v: &ValuePrimitiveType) -> Self {
441                    match v {
442                        ValuePrimitiveType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
443                        ValuePrimitiveType::Primitive(PrimitiveValue::I64(v)) => *v as $t,
444                        _ => 0,
445                    }
446                }
447            }
448        )*
449    };
450}
451impl_from_prim_ref_sint!(i8, i16, i32, i64, isize);
452impl From<f32> for ValuePrimitiveType {
453    fn from(v: f32) -> Self {
454        ValuePrimitiveType::Primitive(PrimitiveValue::F32(v))
455    }
456}
457impl From<&ValuePrimitiveType> for f32 {
458    fn from(v: &ValuePrimitiveType) -> Self {
459        match v {
460            ValuePrimitiveType::Primitive(PrimitiveValue::String(v)) => {
461                v.parse().unwrap_or_default()
462            }
463            ValuePrimitiveType::Primitive(PrimitiveValue::F32(v)) => *v,
464            _ => 0.0,
465        }
466    }
467}
468impl From<ValuePrimitiveType> for f32 {
469    fn from(v: ValuePrimitiveType) -> Self {
470        match v {
471            ValuePrimitiveType::Primitive(PrimitiveValue::String(v)) => {
472                v.parse().unwrap_or_default()
473            }
474            ValuePrimitiveType::Primitive(PrimitiveValue::F32(v)) => v,
475            _ => 0.0,
476        }
477    }
478}
479impl From<f64> for ValuePrimitiveType {
480    fn from(v: f64) -> Self {
481        ValuePrimitiveType::Primitive(PrimitiveValue::F64(v))
482    }
483}
484impl From<&ValuePrimitiveType> for f64 {
485    fn from(v: &ValuePrimitiveType) -> Self {
486        match v {
487            ValuePrimitiveType::Primitive(PrimitiveValue::String(v)) => {
488                v.parse().unwrap_or_default()
489            }
490            ValuePrimitiveType::Primitive(PrimitiveValue::F64(v)) => *v,
491            _ => 0.0,
492        }
493    }
494}
495impl From<ValuePrimitiveType> for f64 {
496    fn from(v: ValuePrimitiveType) -> Self {
497        match v {
498            ValuePrimitiveType::Primitive(PrimitiveValue::String(v)) => {
499                v.parse().unwrap_or_default()
500            }
501            ValuePrimitiveType::Primitive(PrimitiveValue::F64(v)) => v,
502            _ => 0.0,
503        }
504    }
505}
506impl From<bool> for ValuePrimitiveType {
507    fn from(v: bool) -> Self {
508        ValuePrimitiveType::Primitive(PrimitiveValue::Bool(v))
509    }
510}
511impl From<ValuePrimitiveType> for bool {
512    fn from(v: ValuePrimitiveType) -> Self {
513        match v {
514            ValuePrimitiveType::Primitive(PrimitiveValue::Bool(v)) => v,
515            _ => false,
516        }
517    }
518}
519impl From<&ValuePrimitiveType> for bool {
520    fn from(v: &ValuePrimitiveType) -> Self {
521        match v {
522            ValuePrimitiveType::Primitive(PrimitiveValue::Bool(v)) => *v,
523            _ => false,
524        }
525    }
526}
527impl From<()> for ValuePrimitiveType {
528    fn from(_: ()) -> Self {
529        ValuePrimitiveType::Primitive(PrimitiveValue::Null)
530    }
531}
532impl From<&ValuePrimitiveType> for () {
533    fn from(_: &ValuePrimitiveType) -> Self {}
534}
535impl From<PrimitiveValue> for ValuePrimitiveType {
536    fn from(v: PrimitiveValue) -> Self {
537        ValuePrimitiveType::Primitive(v)
538    }
539}
540impl From<&ValuePrimitiveType> for PrimitiveValue {
541    fn from(v: &ValuePrimitiveType) -> Self {
542        match v {
543            ValuePrimitiveType::Primitive(v) => v.clone(),
544            _ => PrimitiveValue::Null,
545        }
546    }
547}
548impl From<ValuePrimitive> for ValuePrimitiveType {
549    fn from(v: ValuePrimitive) -> Self {
550        ValuePrimitiveType::NestedPrimitive(v)
551    }
552}
553impl From<&ValuePrimitiveType> for ValuePrimitive {
554    fn from(v: &ValuePrimitiveType) -> Self {
555        match v {
556            ValuePrimitiveType::NestedPrimitive(v) => v.clone(),
557            _ => ValuePrimitive::new(),
558        }
559    }
560}
561impl<T> From<Option<T>> for ValuePrimitiveType
562where
563    T: Into<ValuePrimitiveType>,
564{
565    fn from(v: Option<T>) -> Self {
566        match v {
567            Some(v) => v.into(),
568            None => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
569        }
570    }
571}
572impl From<&ValuePrimitiveType> for JSONValue {
573    fn from(v: &ValuePrimitiveType) -> Self {
574        match v {
575            ValuePrimitiveType::Primitive(v) => JSONValue::Primitive(v.clone()),
576            ValuePrimitiveType::NestedPrimitive(v) => {
577                let mut map = Map::<String, JSONValue>::new();
578                for (k, v) in v.iter() {
579                    map.insert(k.clone(), v.into());
580                }
581                JSONValue::Object(map)
582            }
583        }
584    }
585}
586impl From<&JSONValue> for ValuePrimitiveType {
587    fn from(v: &JSONValue) -> Self {
588        match v {
589            JSONValue::Primitive(v) => ValuePrimitiveType::Primitive(v.clone()),
590            JSONValue::Object(v) => {
591                let mut map = ValuePrimitive::new();
592                for (k, v) in v.iter() {
593                    map.insert(k.clone(), v.into());
594                }
595                ValuePrimitiveType::NestedPrimitive(map)
596            }
597            // DROPS ALL ARRAY DATA AS IT IS NOT SUPPORTED INSIDE VALUE PRIMITIVES
598            _ => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
599        }
600    }
601}
602
603// ValueType
604impl Default for ValueType {
605    fn default() -> Self {
606        ValueType::Primitive(PrimitiveValue::Null)
607    }
608}
609impl ValueType {
610    /// Returns the value as a primitive
611    pub fn to_prim(&self) -> Option<&PrimitiveValue> {
612        match self {
613            ValueType::Primitive(v) => Some(v),
614            _ => None,
615        }
616    }
617    /// Returns true if the value is a primitive
618    pub fn is_prim(&self) -> bool {
619        matches!(self, ValueType::Primitive(_))
620    }
621    /// Returns the value as a vector
622    pub fn to_vec(&self) -> Option<&Vec<ValuePrimitiveType>> {
623        match self {
624            ValueType::Array(v) => Some(v),
625            _ => None,
626        }
627    }
628    /// Returns true if the value is a vector
629    pub fn is_vec(&self) -> bool {
630        matches!(self, ValueType::Array(_))
631    }
632    /// Returns the value as a nested object
633    pub fn to_nested(&self) -> Option<&Value> {
634        match self {
635            ValueType::Nested(v) => Some(v),
636            _ => None,
637        }
638    }
639    /// Returns true if the value is a nested object
640    pub fn is_nested(&self) -> bool {
641        matches!(self, ValueType::Nested(_))
642    }
643}
644impl From<&str> for ValueType {
645    fn from(s: &str) -> Self {
646        ValueType::Primitive(PrimitiveValue::String(s.into()))
647    }
648}
649// impl AsRef<str> for ValueType {
650//     fn as_ref(&self) -> &str {
651//         match self {
652//             ValueType::Primitive(PrimitiveValue::String(s)) => s.as_str(),
653//             _ => "",
654//         }
655//     }
656// }
657impl From<&ValueType> for ValueType {
658    fn from(v: &ValueType) -> Self {
659        v.clone()
660    }
661}
662impl From<String> for ValueType {
663    fn from(s: String) -> Self {
664        ValueType::Primitive(PrimitiveValue::String(s))
665    }
666}
667impl From<&String> for ValueType {
668    fn from(s: &String) -> Self {
669        ValueType::Primitive(PrimitiveValue::String(s.into()))
670    }
671}
672impl From<ValueType> for String {
673    fn from(v: ValueType) -> Self {
674        match v {
675            ValueType::Primitive(PrimitiveValue::String(s)) => s,
676            _ => "".into(),
677        }
678    }
679}
680impl From<&ValueType> for String {
681    fn from(v: &ValueType) -> Self {
682        match v {
683            ValueType::Primitive(PrimitiveValue::String(s)) => s.into(),
684            _ => "".into(),
685        }
686    }
687}
688
689// Implement for u8, u16, u32, u64
690macro_rules! impl_from_uint {
691    ($($t:ty),*) => {
692        $(
693            impl From<$t> for ValueType {
694                fn from(v: $t) -> Self {
695                    ValueType::Primitive(PrimitiveValue::U64(v as u64))
696                }
697            }
698
699            impl From<ValueType> for $t {
700                fn from(v: ValueType) -> Self {
701                    match v {
702                        ValueType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
703                        ValueType::Primitive(PrimitiveValue::U64(v)) => v as $t,
704                        _ => 0,
705                    }
706                }
707            }
708        )*
709    };
710}
711impl_from_uint!(u8, u16, u32, u64, usize);
712macro_rules! impl_from_uint_ref {
713    ($($t:ty),*) => {
714        $(
715            impl From<&ValueType> for $t {
716                fn from(v: &ValueType) -> Self {
717                    match v {
718                        ValueType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
719                        ValueType::Primitive(PrimitiveValue::U64(v)) => *v as $t,
720                        _ => 0,
721                    }
722                }
723            }
724        )*
725    };
726}
727impl_from_uint_ref!(u8, u16, u32, u64, usize);
728// Implement for i8, i16, i32, i64
729macro_rules! impl_from_sint {
730    ($($t:ty),*) => {
731        $(
732            impl From<$t> for ValueType {
733                fn from(v: $t) -> Self {
734                    ValueType::Primitive(PrimitiveValue::I64(v as i64))
735                }
736            }
737
738            impl From<ValueType> for $t {
739                fn from(v: ValueType) -> Self {
740                    match v {
741                        ValueType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
742                        ValueType::Primitive(PrimitiveValue::I64(v)) => v as $t,
743                        _ => 0,
744                    }
745                }
746            }
747        )*
748    };
749}
750impl_from_sint!(i8, i16, i32, i64, isize);
751macro_rules! impl_from_sint_ref {
752    ($($t:ty),*) => {
753        $(
754            impl From<&ValueType> for $t {
755                fn from(v: &ValueType) -> Self {
756                    match v {
757                        ValueType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
758                        ValueType::Primitive(PrimitiveValue::I64(v)) => *v as $t,
759                        _ => 0,
760                    }
761                }
762            }
763        )*
764    };
765}
766impl_from_sint_ref!(i8, i16, i32, i64, isize);
767impl From<f32> for ValueType {
768    fn from(v: f32) -> Self {
769        ValueType::Primitive(PrimitiveValue::F32(v))
770    }
771}
772impl From<ValueType> for f32 {
773    fn from(v: ValueType) -> Self {
774        match v {
775            ValueType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
776            ValueType::Primitive(PrimitiveValue::F32(v)) => v,
777            _ => 0.0,
778        }
779    }
780}
781impl From<&ValueType> for f32 {
782    fn from(v: &ValueType) -> Self {
783        match v {
784            ValueType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
785            ValueType::Primitive(PrimitiveValue::F32(v)) => *v,
786            _ => 0.0,
787        }
788    }
789}
790impl From<f64> for ValueType {
791    fn from(v: f64) -> Self {
792        ValueType::Primitive(PrimitiveValue::F64(v))
793    }
794}
795impl From<ValueType> for f64 {
796    fn from(v: ValueType) -> Self {
797        match v {
798            ValueType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
799            ValueType::Primitive(PrimitiveValue::F64(v)) => v,
800            _ => 0.0,
801        }
802    }
803}
804impl From<&ValueType> for f64 {
805    fn from(v: &ValueType) -> Self {
806        match v {
807            ValueType::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
808            ValueType::Primitive(PrimitiveValue::F64(v)) => *v,
809            _ => 0.0,
810        }
811    }
812}
813impl From<bool> for ValueType {
814    fn from(v: bool) -> Self {
815        ValueType::Primitive(PrimitiveValue::Bool(v))
816    }
817}
818impl From<ValueType> for bool {
819    fn from(v: ValueType) -> Self {
820        match v {
821            ValueType::Primitive(PrimitiveValue::String(v)) => v == "true",
822            ValueType::Primitive(PrimitiveValue::Bool(v)) => v,
823            _ => false,
824        }
825    }
826}
827impl From<&ValueType> for bool {
828    fn from(v: &ValueType) -> Self {
829        match v {
830            ValueType::Primitive(PrimitiveValue::String(v)) => v == "true",
831            ValueType::Primitive(PrimitiveValue::Bool(v)) => *v,
832            _ => false,
833        }
834    }
835}
836impl From<()> for ValueType {
837    fn from(_: ()) -> Self {
838        ValueType::Primitive(PrimitiveValue::Null)
839    }
840}
841impl From<ValueType> for () {
842    fn from(_: ValueType) -> Self {}
843}
844impl From<&ValueType> for () {
845    fn from(_: &ValueType) -> Self {}
846}
847impl<T> From<Vec<T>> for ValueType
848where
849    T: Into<ValuePrimitiveType>,
850{
851    fn from(v: Vec<T>) -> Self {
852        ValueType::Array(v.into_iter().map(Into::into).collect())
853    }
854}
855impl<T> From<&Vec<T>> for ValueType
856where
857    T: Into<ValuePrimitiveType>,
858    ValuePrimitiveType: for<'a> From<&'a T>,
859{
860    fn from(v: &Vec<T>) -> Self {
861        ValueType::Array(v.iter().map(Into::into).collect())
862    }
863}
864impl<T> From<ValueType> for Vec<T>
865where
866    T: From<ValuePrimitiveType>,
867{
868    fn from(v: ValueType) -> Self {
869        match v {
870            ValueType::Array(v) => v.into_iter().map(Into::into).collect(),
871            _ => Vec::new(),
872        }
873    }
874}
875impl<T> From<&ValueType> for Vec<T>
876where
877    T: for<'a> From<&'a ValuePrimitiveType>,
878{
879    fn from(v: &ValueType) -> Self {
880        match v {
881            ValueType::Array(v) => v.iter().map(Into::into).collect(),
882            _ => Vec::new(),
883        }
884    }
885}
886impl From<Value> for ValueType {
887    fn from(v: Value) -> Self {
888        ValueType::Nested(v)
889    }
890}
891impl From<ValueType> for Value {
892    fn from(v: ValueType) -> Self {
893        match v {
894            ValueType::Nested(v) => v,
895            _ => Value::default(),
896        }
897    }
898}
899impl From<&ValueType> for Value {
900    fn from(v: &ValueType) -> Self {
901        match v {
902            ValueType::Nested(v) => v.clone(),
903            _ => Value::default(),
904        }
905    }
906}
907impl<T> From<Option<T>> for ValueType
908where
909    T: Into<ValueType>,
910{
911    fn from(v: Option<T>) -> Self {
912        match v {
913            Some(v) => v.into(),
914            None => ValueType::Primitive(PrimitiveValue::Null),
915        }
916    }
917}
918impl From<&JSONValue> for ValueType {
919    fn from(v: &JSONValue) -> Self {
920        match v {
921            JSONValue::Primitive(v) => ValueType::Primitive(v.clone()),
922            JSONValue::Array(v) => ValueType::Array(v.iter().map(Into::into).collect()),
923            JSONValue::Object(v) => {
924                let mut res = Value::new();
925                for (k, v) in v.iter() {
926                    res.insert(k.clone(), v.into());
927                }
928                ValueType::Nested(res)
929            }
930        }
931    }
932}
933impl From<&ValueType> for JSONValue {
934    fn from(v: &ValueType) -> Self {
935        match v {
936            ValueType::Primitive(v) => JSONValue::Primitive(v.clone()),
937            ValueType::Array(v) => JSONValue::Array(v.iter().map(Into::into).collect()),
938            ValueType::Nested(v) => {
939                let mut res = Map::<String, JSONValue>::new();
940                for (k, v) in v.iter() {
941                    res.insert(k.clone(), v.into());
942                }
943                JSONValue::Object(res)
944            }
945        }
946    }
947}
948
949impl MValueCompatible for JSONProperties {}
950impl From<JSONProperties> for MValue {
951    fn from(json: JSONProperties) -> MValue {
952        let mut res = MValue::new();
953        for (k, v) in json.iter() {
954            res.insert(k.clone(), v.into());
955        }
956        res
957    }
958}
959impl From<&JSONProperties> for MValue {
960    fn from(json: &JSONProperties) -> MValue {
961        let mut res = MValue::new();
962        for (k, v) in json.iter() {
963            res.insert(k.clone(), v.into());
964        }
965        res
966    }
967}
968impl From<MValue> for JSONProperties {
969    fn from(v: MValue) -> JSONProperties {
970        let mut res = JSONProperties::new();
971        for (k, v) in v.iter() {
972            res.insert(k.clone(), v.into());
973        }
974        res
975    }
976}
977impl From<&MValue> for JSONProperties {
978    fn from(v: &MValue) -> JSONProperties {
979        let mut res = JSONProperties::new();
980        for (k, v) in v.iter() {
981            res.insert(k.clone(), v.into());
982        }
983        res
984    }
985}
986
987impl MValueCompatible for MapboxProperties {}
988impl From<MapboxProperties> for MValue {
989    fn from(json: MapboxProperties) -> MValue {
990        let mut res = MValue::new();
991        for (k, v) in json.iter() {
992            res.insert(k.clone(), ValueType::Primitive(v.clone()));
993        }
994        res
995    }
996}
997impl From<&MapboxProperties> for MValue {
998    fn from(json: &MapboxProperties) -> MValue {
999        let mut res = MValue::new();
1000        for (k, v) in json.iter() {
1001            res.insert(k.clone(), ValueType::Primitive(v.clone()));
1002        }
1003        res
1004    }
1005}
1006impl From<MValue> for MapboxProperties {
1007    fn from(v: MValue) -> MapboxProperties {
1008        let mut res = MapboxProperties::new();
1009        // Only copy over primitive values
1010        for (k, v) in v.iter() {
1011            let value = v.clone();
1012            if let Some(p) = value.to_prim() {
1013                res.insert(k.clone(), p.clone());
1014            }
1015        }
1016        res
1017    }
1018}
1019impl From<&MValue> for MapboxProperties {
1020    fn from(v: &MValue) -> MapboxProperties {
1021        let mut res = MapboxProperties::new();
1022        // Only copy over primitive values
1023        for (k, v) in v.iter() {
1024            let value = v.clone();
1025            if let Some(p) = value.to_prim() {
1026                res.insert(k.clone(), p.clone());
1027            }
1028        }
1029        res
1030    }
1031}
1032
1033// Geometry
1034
1035impl From<&Point> for ValueType {
1036    fn from(v: &Point) -> Self {
1037        ValueType::Array(vec![v.0.into(), v.1.into()])
1038    }
1039}
1040impl From<&ValueType> for Point {
1041    fn from(v: &ValueType) -> Self {
1042        match v {
1043            ValueType::Array(arr) => {
1044                if let Some(x) = arr.first()
1045                    && let Some(y) = arr.get(1)
1046                {
1047                    Point(
1048                        x.to_prim()
1049                            .unwrap_or(&PrimitiveValue::default())
1050                            .to_f64()
1051                            .unwrap_or_default(),
1052                        y.to_prim()
1053                            .unwrap_or(&PrimitiveValue::default())
1054                            .to_f64()
1055                            .unwrap_or_default(),
1056                    )
1057                } else {
1058                    Point::default()
1059                }
1060            }
1061            _ => Point::default(),
1062        }
1063    }
1064}
1065
1066// Serde compatibility for testing
1067
1068impl From<&serde_json::Value> for Value {
1069    fn from(val: &serde_json::Value) -> Self {
1070        let mut res = Value::new();
1071        if let serde_json::Value::Object(o) = val {
1072            for (k, v) in o.iter() {
1073                res.insert(k.clone(), v.into());
1074            }
1075        }
1076
1077        res
1078    }
1079}
1080impl From<&serde_json::Value> for ValueType {
1081    fn from(val: &serde_json::Value) -> Self {
1082        match val {
1083            serde_json::Value::Null => ValueType::Primitive(PrimitiveValue::Null),
1084            serde_json::Value::Bool(b) => ValueType::Primitive(PrimitiveValue::Bool(*b)),
1085            serde_json::Value::Number(num) => {
1086                ValueType::Primitive(PrimitiveValue::F64(num.as_f64().unwrap_or_default()))
1087            }
1088            serde_json::Value::String(s) => ValueType::Primitive(PrimitiveValue::String(s.clone())),
1089            serde_json::Value::Array(values) => {
1090                ValueType::Array(values.iter().map(Into::into).collect())
1091            }
1092            serde_json::Value::Object(map) => ValueType::Nested(map.into()),
1093        }
1094    }
1095}
1096impl From<&serde_json::Value> for ValuePrimitiveType {
1097    fn from(val: &serde_json::Value) -> Self {
1098        match val {
1099            serde_json::Value::Null => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
1100            serde_json::Value::Bool(b) => ValuePrimitiveType::Primitive(PrimitiveValue::Bool(*b)),
1101            serde_json::Value::Number(num) => {
1102                ValuePrimitiveType::Primitive(PrimitiveValue::F64(num.as_f64().unwrap_or_default()))
1103            }
1104            serde_json::Value::String(s) => {
1105                ValuePrimitiveType::Primitive(PrimitiveValue::String(s.clone()))
1106            }
1107            _ => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
1108        }
1109    }
1110}
1111impl From<&serde_json::Map<String, serde_json::Value>> for Value {
1112    fn from(val: &serde_json::Map<String, serde_json::Value>) -> Self {
1113        let mut res = Value::new();
1114        for (k, v) in val.iter() {
1115            res.insert(k.clone(), v.into());
1116        }
1117        res
1118    }
1119}