s2json-core 1.17.0

This library supports the S2JSON 1.0 Specification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
use crate::*;
use alloc::{
    string::{String, ToString},
    vec::Vec,
};
use libm::round;

// PrimitiveValue
impl PrimitiveValue {
    /// Returns true if the value is null
    pub fn is_null(&self) -> bool {
        matches!(self, PrimitiveValue::Null)
    }

    /// Converts a primitive value to a string
    pub fn to_string(&self) -> Option<String> {
        match self {
            PrimitiveValue::String(v) => Some(v.clone()),
            _ => None,
        }
    }

    /// Converts a primitive value to a u64
    pub fn to_u64(&self) -> Option<u64> {
        match self {
            PrimitiveValue::U64(v) => Some(*v),
            PrimitiveValue::I64(v) => Some(*v as u64),
            PrimitiveValue::F64(v) => Some(round(*v) as u64),
            PrimitiveValue::F32(v) => Some(round((*v).into()) as u64),
            _ => None,
        }
    }

    /// Converts a primitive value to a i64
    pub fn to_i64(&self) -> Option<i64> {
        match self {
            PrimitiveValue::U64(v) => Some(*v as i64),
            PrimitiveValue::I64(v) => Some(*v),
            PrimitiveValue::F64(v) => Some(round(*v) as i64),
            PrimitiveValue::F32(v) => Some(round((*v).into()) as i64),
            _ => None,
        }
    }

    /// Converts a primitive value to a f64
    pub fn to_f64(&self) -> Option<f64> {
        match self {
            PrimitiveValue::U64(v) => Some(*v as f64),
            PrimitiveValue::I64(v) => Some(*v as f64),
            PrimitiveValue::F64(v) => Some(*v),
            PrimitiveValue::F32(v) => Some(*v as f64),
            _ => None,
        }
    }

    /// Converts a primitive value to a f32
    pub fn to_f32(&self) -> Option<f32> {
        match self {
            PrimitiveValue::U64(v) => Some(*v as f32),
            PrimitiveValue::I64(v) => Some(*v as f32),
            PrimitiveValue::F64(v) => Some(*v as f32),
            PrimitiveValue::F32(v) => Some(*v),
            _ => None,
        }
    }

    /// Converts a primitive value to a bool
    pub fn to_bool(&self) -> Option<bool> {
        match self {
            PrimitiveValue::Bool(v) => Some(*v),
            _ => None,
        }
    }
}
impl From<&str> for PrimitiveValue {
    fn from(s: &str) -> Self {
        PrimitiveValue::String(s.to_string())
    }
}
impl From<String> for PrimitiveValue {
    fn from(s: String) -> Self {
        PrimitiveValue::String(s)
    }
}
impl From<u64> for PrimitiveValue {
    fn from(v: u64) -> Self {
        PrimitiveValue::U64(v)
    }
}
impl From<i64> for PrimitiveValue {
    fn from(v: i64) -> Self {
        PrimitiveValue::I64(v)
    }
}
impl From<f32> for PrimitiveValue {
    fn from(v: f32) -> Self {
        PrimitiveValue::F32(v)
    }
}
impl From<f64> for PrimitiveValue {
    fn from(v: f64) -> Self {
        PrimitiveValue::F64(v)
    }
}
impl From<bool> for PrimitiveValue {
    fn from(v: bool) -> Self {
        PrimitiveValue::Bool(v)
    }
}
impl From<()> for PrimitiveValue {
    fn from(_: ()) -> Self {
        PrimitiveValue::Null
    }
}
impl<T> From<Option<T>> for PrimitiveValue
where
    T: Into<PrimitiveValue>,
{
    fn from(v: Option<T>) -> Self {
        match v {
            Some(v) => v.into(),
            None => PrimitiveValue::Null,
        }
    }
}
impl From<&PrimitiveValue> for JSONValue {
    fn from(v: &PrimitiveValue) -> Self {
        JSONValue::Primitive(v.clone())
    }
}
impl From<&JSONValue> for PrimitiveValue {
    fn from(v: &JSONValue) -> Self {
        match v {
            JSONValue::Primitive(v) => v.clone(),
            // DROPS VALUES THAT ARE NOT PRIMITIVES
            _ => PrimitiveValue::Null,
        }
    }
}

// ValuePrimitiveType
impl ValuePrimitiveType {
    /// Returns the value as a primitive
    pub fn to_prim(&self) -> Option<&PrimitiveValue> {
        match self {
            ValuePrimitiveType::Primitive(v) => Some(v),
            _ => None,
        }
    }

    /// Returns the value as a nested object
    pub fn to_nested(&self) -> Option<&ValuePrimitive> {
        match self {
            ValuePrimitiveType::NestedPrimitive(v) => Some(v),
            _ => None,
        }
    }
}
impl From<&str> for ValuePrimitiveType {
    fn from(s: &str) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::String(s.to_string()))
    }
}
impl From<String> for ValuePrimitiveType {
    fn from(s: String) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::String(s))
    }
}
impl From<u64> for ValuePrimitiveType {
    fn from(v: u64) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::U64(v))
    }
}
impl From<i64> for ValuePrimitiveType {
    fn from(v: i64) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::I64(v))
    }
}
impl From<f32> for ValuePrimitiveType {
    fn from(v: f32) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::F32(v))
    }
}
impl From<f64> for ValuePrimitiveType {
    fn from(v: f64) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::F64(v))
    }
}
impl From<bool> for ValuePrimitiveType {
    fn from(v: bool) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::Bool(v))
    }
}
impl From<()> for ValuePrimitiveType {
    fn from(_: ()) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::Null)
    }
}
impl From<PrimitiveValue> for ValuePrimitiveType {
    fn from(v: PrimitiveValue) -> Self {
        ValuePrimitiveType::Primitive(v)
    }
}
impl From<ValuePrimitive> for ValuePrimitiveType {
    fn from(v: ValuePrimitive) -> Self {
        ValuePrimitiveType::NestedPrimitive(v)
    }
}
impl<T> From<Option<T>> for ValuePrimitiveType
where
    T: Into<ValuePrimitiveType>,
{
    fn from(v: Option<T>) -> Self {
        match v {
            Some(v) => v.into(),
            None => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
        }
    }
}
impl From<&ValuePrimitiveType> for JSONValue {
    fn from(v: &ValuePrimitiveType) -> Self {
        match v {
            ValuePrimitiveType::Primitive(v) => JSONValue::Primitive(v.clone()),
            ValuePrimitiveType::NestedPrimitive(v) => {
                let mut map = Map::<String, JSONValue>::new();
                for (k, v) in v.iter() {
                    map.insert(k.clone(), v.into());
                }
                JSONValue::Object(map)
            }
        }
    }
}
impl From<&JSONValue> for ValuePrimitiveType {
    fn from(v: &JSONValue) -> Self {
        match v {
            JSONValue::Primitive(v) => ValuePrimitiveType::Primitive(v.clone()),
            JSONValue::Object(v) => {
                let mut map = ValuePrimitive::new();
                for (k, v) in v.iter() {
                    map.insert(k.clone(), v.into());
                }
                ValuePrimitiveType::NestedPrimitive(map)
            }
            // DROPS ALL ARRAY DATA AS IT IS NOT SUPPORTED INSIDE VALUE PRIMITIVES
            _ => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
        }
    }
}

// ValueType
impl Default for ValueType {
    fn default() -> Self {
        ValueType::Primitive(PrimitiveValue::Null)
    }
}
impl ValueType {
    /// Returns the value as a primitive
    pub fn to_prim(&self) -> Option<&PrimitiveValue> {
        match self {
            ValueType::Primitive(v) => Some(v),
            _ => None,
        }
    }

    /// Returns the value as a vector
    pub fn to_vec(&self) -> Option<&Vec<ValuePrimitiveType>> {
        match self {
            ValueType::Array(v) => Some(v),
            _ => None,
        }
    }

    /// Returns the value as a nested object
    pub fn to_nested(&self) -> Option<&Value> {
        match self {
            ValueType::Nested(v) => Some(v),
            _ => None,
        }
    }
}
impl From<&str> for ValueType {
    fn from(s: &str) -> Self {
        ValueType::Primitive(PrimitiveValue::String(s.to_string()))
    }
}
impl AsRef<str> for ValueType {
    fn as_ref(&self) -> &str {
        match self {
            ValueType::Primitive(PrimitiveValue::String(s)) => s.as_str(),
            _ => "",
        }
    }
}
impl From<String> for ValueType {
    fn from(s: String) -> Self {
        ValueType::Primitive(PrimitiveValue::String(s))
    }
}
impl From<ValueType> for String {
    fn from(v: ValueType) -> Self {
        match v {
            ValueType::Primitive(PrimitiveValue::String(s)) => s,
            _ => "".to_string(),
        }
    }
}

// Implement for u8, u16, u32, u64
macro_rules! impl_from_int {
    ($($t:ty),*) => {
        $(
            impl From<$t> for ValueType {
                fn from(v: $t) -> Self {
                    ValueType::Primitive(PrimitiveValue::U64(v as u64))
                }
            }

            impl From<ValueType> for $t {
                fn from(v: ValueType) -> Self {
                    match v {
                        ValueType::Primitive(PrimitiveValue::U64(v)) => v as $t,
                        _ => 0,
                    }
                }
            }
        )*
    };
}
impl_from_int!(u8, u16, u32, u64, usize);
// Implement for i8, i16, i32, i64
macro_rules! impl_from_int {
    ($($t:ty),*) => {
        $(
            impl From<$t> for ValueType {
                fn from(v: $t) -> Self {
                    ValueType::Primitive(PrimitiveValue::I64(v as i64))
                }
            }

            impl From<ValueType> for $t {
                fn from(v: ValueType) -> Self {
                    match v {
                        ValueType::Primitive(PrimitiveValue::I64(v)) => v as $t,
                        _ => 0,
                    }
                }
            }
        )*
    };
}
impl_from_int!(i8, i16, i32, i64, isize);
impl From<f32> for ValueType {
    fn from(v: f32) -> Self {
        ValueType::Primitive(PrimitiveValue::F32(v))
    }
}
impl From<ValueType> for f32 {
    fn from(v: ValueType) -> Self {
        match v {
            ValueType::Primitive(PrimitiveValue::F32(v)) => v,
            _ => 0.0,
        }
    }
}
impl From<f64> for ValueType {
    fn from(v: f64) -> Self {
        ValueType::Primitive(PrimitiveValue::F64(v))
    }
}
impl From<ValueType> for f64 {
    fn from(v: ValueType) -> Self {
        match v {
            ValueType::Primitive(PrimitiveValue::F64(v)) => v,
            _ => 0.0,
        }
    }
}
impl From<bool> for ValueType {
    fn from(v: bool) -> Self {
        ValueType::Primitive(PrimitiveValue::Bool(v))
    }
}
impl From<ValueType> for bool {
    fn from(v: ValueType) -> Self {
        match v {
            ValueType::Primitive(PrimitiveValue::Bool(v)) => v,
            _ => false,
        }
    }
}
impl From<()> for ValueType {
    fn from(_: ()) -> Self {
        ValueType::Primitive(PrimitiveValue::Null)
    }
}
impl From<ValueType> for () {
    fn from(_: ValueType) -> Self {}
}
impl<T> From<Vec<T>> for ValueType
where
    T: Into<ValuePrimitiveType>,
{
    fn from(v: Vec<T>) -> Self {
        ValueType::Array(v.into_iter().map(Into::into).collect())
    }
}
impl<T> From<ValueType> for Vec<T>
where
    T: From<ValuePrimitiveType>,
{
    fn from(v: ValueType) -> Self {
        match v {
            ValueType::Array(v) => v.into_iter().map(Into::into).collect(),
            _ => Vec::new(),
        }
    }
}
impl From<Value> for ValueType {
    fn from(v: Value) -> Self {
        ValueType::Nested(v)
    }
}
impl From<ValueType> for Value {
    fn from(v: ValueType) -> Self {
        match v {
            ValueType::Nested(v) => v,
            _ => Value::default(),
        }
    }
}
impl<T> From<Option<T>> for ValueType
where
    T: Into<ValueType>,
{
    fn from(v: Option<T>) -> Self {
        match v {
            Some(v) => v.into(),
            None => ValueType::Primitive(PrimitiveValue::Null),
        }
    }
}
// TODO: Find a simpler methodology for this
/// This trait is used to ensure that only types that implement From<ValueType> are allowed
pub trait NotValueType {}
/// A macro to implement the `NotValueType` trait for multiple types at once.
macro_rules! impl_not_value_type {
    ( $( $t:ty ),* ) => {
        $(
            impl NotValueType for $t {}
        )*
    };
}
impl_not_value_type!(
    u8,
    u16,
    u32,
    u64,
    usize,
    i8,
    i16,
    i32,
    i64,
    isize,
    f32,
    f64,
    String,
    &str,
    bool,
    ()
);
impl<T> From<ValueType> for Option<T>
where
    T: From<ValueType> + NotValueType, /* This ensures that only types that implement From<ValueType> are allowed */
{
    fn from(v: ValueType) -> Self {
        match v {
            ValueType::Primitive(PrimitiveValue::Null) => None,
            _ => Some(v.into()),
        }
    }
}
// First, implement the `From<ValueType>` trait for `Option<T>` where T is not ValueType.
// impl<T> From<ValueType> for Option<T>
// where
//     T: From<ValueType>,
// {
//     fn from(v: ValueType) -> Self {
//         match v {
//             ValueType::Primitive(PrimitiveValue::Null) => None,
//             v => Some(v.into()), // Use the custom Into implementation
//         }
//     }
// }
impl From<&JSONValue> for ValueType {
    fn from(v: &JSONValue) -> Self {
        match v {
            JSONValue::Primitive(v) => ValueType::Primitive(v.clone()),
            JSONValue::Array(v) => ValueType::Array(v.iter().map(Into::into).collect()),
            JSONValue::Object(v) => {
                let mut res = Value::new();
                for (k, v) in v.iter() {
                    res.insert(k.clone(), v.into());
                }
                ValueType::Nested(res)
            }
        }
    }
}
impl From<&ValueType> for JSONValue {
    fn from(v: &ValueType) -> Self {
        match v {
            ValueType::Primitive(v) => JSONValue::Primitive(v.clone()),
            ValueType::Array(v) => JSONValue::Array(v.iter().map(Into::into).collect()),
            ValueType::Nested(v) => {
                let mut res = Map::<String, JSONValue>::new();
                for (k, v) in v.iter() {
                    res.insert(k.clone(), v.into());
                }
                JSONValue::Object(res)
            }
        }
    }
}

impl Default for JSONValue {
    fn default() -> Self {
        JSONValue::Primitive(PrimitiveValue::Null)
    }
}
impl JSONValue {
    /// Returns the value as a primitive
    pub fn to_prim(&self) -> Option<&PrimitiveValue> {
        match self {
            JSONValue::Primitive(v) => Some(v),
            _ => None,
        }
    }

    /// Returns the value as a vector
    pub fn to_vec(&self) -> Option<&Vec<JSONValue>> {
        match self {
            JSONValue::Array(v) => Some(v),
            _ => None,
        }
    }

    /// Returns the value as a nested object
    pub fn to_nested(&self) -> Option<&Map<String, JSONValue>> {
        match self {
            JSONValue::Object(v) => Some(v),
            _ => None,
        }
    }
}

impl MValueCompatible for JSONProperties {}
impl From<JSONProperties> for MValue {
    fn from(json: JSONProperties) -> MValue {
        let mut res = MValue::new();
        for (k, v) in json.iter() {
            res.insert(k.clone(), v.into());
        }
        res
    }
}
impl From<MValue> for JSONProperties {
    fn from(v: MValue) -> JSONProperties {
        let mut res = JSONProperties::new();
        for (k, v) in v.iter() {
            res.insert(k.clone(), v.into());
        }
        res
    }
}

impl MValueCompatible for MapboxProperties {}
impl From<MapboxProperties> for MValue {
    fn from(json: MapboxProperties) -> MValue {
        let mut res = MValue::new();
        for (k, v) in json.iter() {
            res.insert(k.clone(), ValueType::Primitive(v.clone()));
        }
        res
    }
}
impl From<MValue> for MapboxProperties {
    fn from(v: MValue) -> MapboxProperties {
        let mut res = MapboxProperties::new();
        // Only copy over primitive values
        for (k, v) in v.iter() {
            let value = v.clone();
            if let Some(p) = value.to_prim() {
                res.insert(k.clone(), p.clone());
            }
        }
        res
    }
}

#[cfg(test)]
mod tests {
    use alloc::vec;

    use crate::{MValue, MValueCompatible, VectorPoint};

    use super::*;

    #[test]
    fn value_default() {
        let default = ValueType::default();
        assert_eq!(default, ValueType::Primitive(PrimitiveValue::Null));
    }

    #[test]
    fn primitive_value_funcs() {
        // &str
        let prim_value: PrimitiveValue = "test".into();
        assert_eq!(PrimitiveValue::String("test".into()), prim_value);
        assert_eq!(prim_value.to_u64(), None);
        assert_eq!(prim_value.to_i64(), None);
        assert_eq!(prim_value.to_f32(), None);
        assert_eq!(prim_value.to_f64(), None);
        assert_eq!(prim_value.to_bool(), None);
        assert!(!prim_value.is_null());
        // String
        let prim_value_str: String = "test".into();
        let prim_value: PrimitiveValue = prim_value_str.clone().into();
        assert_eq!(PrimitiveValue::String("test".into()), prim_value);
        assert_eq!(prim_value.to_string(), Some("test".into()));
        // u64
        let prim_value: PrimitiveValue = 1_u64.into();
        assert_eq!(PrimitiveValue::U64(1), prim_value);
        assert_eq!(prim_value.to_string(), None);
        assert_eq!(prim_value.to_u64(), Some(1));
        assert_eq!(prim_value.to_i64(), Some(1));
        assert_eq!(prim_value.to_f32(), Some(1.0));
        assert_eq!(prim_value.to_f64(), Some(1.0));
        // i64
        let prim_value: PrimitiveValue = (-1_i64).into();
        assert_eq!(PrimitiveValue::I64(-1), prim_value);
        assert_eq!(prim_value.to_u64(), Some(18446744073709551615));
        assert_eq!(prim_value.to_i64(), Some(-1));
        assert_eq!(prim_value.to_f32(), Some(-1.0));
        assert_eq!(prim_value.to_f64(), Some(-1.0));
        // f32
        let prim_value: PrimitiveValue = (1.0_f32).into();
        assert_eq!(PrimitiveValue::F32(1.0), prim_value);
        assert_eq!(prim_value.to_u64(), Some(1));
        assert_eq!(prim_value.to_i64(), Some(1));
        assert_eq!(prim_value.to_f32(), Some(1.0));
        assert_eq!(prim_value.to_f64(), Some(1.0));
        // f64
        let prim_value: PrimitiveValue = (1.0_f64).into();
        assert_eq!(PrimitiveValue::F64(1.0), prim_value);
        assert_eq!(prim_value.to_u64(), Some(1));
        assert_eq!(prim_value.to_i64(), Some(1));
        assert_eq!(prim_value.to_f32(), Some(1.0));
        assert_eq!(prim_value.to_f64(), Some(1.0));
        // bool
        let prim_value: PrimitiveValue = true.into();
        assert_eq!(PrimitiveValue::Bool(true), prim_value);
        assert_eq!(prim_value.to_bool(), Some(true));
        // ()
        let prim_value: PrimitiveValue = ().into();
        assert_eq!(PrimitiveValue::Null, prim_value);
        assert!(prim_value.is_null());
        // Option
        let prim_value: PrimitiveValue = Some(true).into();
        assert_eq!(PrimitiveValue::Bool(true), prim_value);
        assert_eq!(prim_value.to_bool(), Some(true));
        let prim_value: PrimitiveValue = None::<bool>.into();
        assert_eq!(PrimitiveValue::Null, prim_value);
        assert!(prim_value.is_null());
    }

    #[test]
    fn value_prim_type_funcs() {
        // &str
        let prim_value: ValuePrimitiveType = "test".into();
        assert_eq!(
            ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
            prim_value
        );
        assert_eq!(prim_value.to_prim(), Some(PrimitiveValue::String("test".into())).as_ref());
        assert_eq!(prim_value.to_nested(), None);
        // String
        let prim_value_str: String = "test".into();
        let prim_value: ValuePrimitiveType = prim_value_str.clone().into();
        assert_eq!(
            ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
            prim_value
        );
        // u64
        let prim_value: ValuePrimitiveType = 1_u64.into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::U64(1)), prim_value);
        // i64
        let prim_value: ValuePrimitiveType = (-1_i64).into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::I64(-1)), prim_value);
        // f32
        let prim_value: ValuePrimitiveType = (1.0_f32).into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::F32(1.0)), prim_value);
        // f64
        let prim_value: ValuePrimitiveType = (1.0_f64).into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::F64(1.0)), prim_value);
        // bool
        let prim_value: ValuePrimitiveType = true.into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Bool(true)), prim_value);
        // ()
        let prim_value: ValuePrimitiveType = ().into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Null), prim_value);

        // from prim
        let nested: ValuePrimitiveType = PrimitiveValue::Bool(true).into();
        assert_eq!(nested.to_prim().unwrap().to_bool(), Some(true));

        // nested
        let nested: ValuePrimitiveType =
            ValuePrimitive::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into())]).into();
        assert_eq!(nested.to_prim(), None);
        assert_eq!(
            nested.to_nested(),
            Some(ValuePrimitive::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),]))
                .as_ref()
        );

        // option
        let prim_value: ValuePrimitiveType = Some(true).into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Bool(true)), prim_value);
        let prim_value: ValuePrimitiveType = None::<bool>.into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Null), prim_value);
    }

    #[test]
    fn value_funcs() {
        // &str
        let prim_value: ValueType = "test".into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::String("test".into())), prim_value);
        let prim = prim_value.to_prim().unwrap();
        assert_eq!(*prim, PrimitiveValue::String("test".into()));
        assert_eq!(prim_value.to_nested(), None);
        assert_eq!(prim_value.to_vec(), None);
        // String
        let prim_value_str: String = "test".into();
        let prim_value: ValueType = prim_value_str.into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::String("test".into())), prim_value);
        // u64
        let prim_value: ValueType = 1_u64.into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::U64(1)), prim_value);
        // i64
        let prim_value: ValueType = (-1_i64).into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::I64(-1)), prim_value);
        // f32
        let prim_value: ValueType = (1.0_f32).into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::F32(1.0)), prim_value);
        // f64
        let prim_value: ValueType = (1.0_f64).into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::F64(1.0)), prim_value);
        // bool
        let prim_value: ValueType = true.into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::Bool(true)), prim_value);
        // ()
        let prim_value: ValueType = ().into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::Null), prim_value);

        // vec
        let prim_value: ValueType = vec!["test", "test2"].into();
        assert_eq!(prim_value.to_prim(), None);
        assert_eq!(
            ValueType::Array(vec![
                ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
                ValuePrimitiveType::Primitive(PrimitiveValue::String("test2".into())),
            ]),
            prim_value
        );
        let back_to_vec: Vec<String> =
            prim_value.to_vec().unwrap().iter().filter_map(|v| v.to_prim()?.to_string()).collect();
        assert_eq!(back_to_vec, vec!["test", "test2"]);

        // nested
        let nested: ValueType =
            Value::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into())]).into();
        assert_eq!(nested.to_vec(), None);
        assert_eq!(
            nested.to_nested(),
            Some(Value::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),])).as_ref()
        );

        // option
        let prim_value: ValueType = Some(true).into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::Bool(true)), prim_value);
        let prim_value: ValueType = None::<bool>.into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::Null), prim_value);
    }

    #[test]
    fn test_rgba_struct() {
        #[derive(Debug, Clone, Copy, PartialEq, Default)]
        pub struct Rgba {
            /// Gamma corrected Red between 0 and 1
            pub r: f64,
            /// Gamma corrected Green between 0 and 1
            pub g: f64,
            /// Gamma corrected Blue between 0 and 1
            pub b: f64,
            /// Opacity between 0 and 1 (not gamma corrected as opacity is linear)
            pub a: f64,
        }
        impl Rgba {
            /// Create a new RGBA value
            pub fn new(r: f64, g: f64, b: f64, a: f64) -> Self {
                Self { r, g, b, a }
            }
        }
        impl MValueCompatible for Rgba {}
        impl From<Rgba> for MValue {
            fn from(rgba: Rgba) -> MValue {
                MValue::from([
                    ("r".into(), (rgba.r).into()),
                    ("g".into(), (rgba.g).into()),
                    ("b".into(), (rgba.b).into()),
                    ("a".into(), (rgba.a).into()),
                ])
            }
        }
        impl From<MValue> for Rgba {
            fn from(mvalue: MValue) -> Self {
                let r: f64 = mvalue.get("r").unwrap().to_prim().unwrap().to_f64().unwrap();
                let g = mvalue.get("g").unwrap().to_prim().unwrap().to_f64().unwrap();
                let b = mvalue.get("b").unwrap().to_prim().unwrap().to_f64().unwrap();
                let a = mvalue.get("a").unwrap().to_prim().unwrap().to_f64().unwrap();
                Rgba::new(r, g, b, a)
            }
        }

        let rgba = Rgba::new(0.1, 0.2, 0.3, 0.4);
        let rgba_mvalue: MValue = rgba.into();
        assert_eq!(
            rgba_mvalue,
            MValue::from([
                ("r".into(), ValueType::Primitive(PrimitiveValue::F64(0.1))),
                ("g".into(), ValueType::Primitive(PrimitiveValue::F64(0.2))),
                ("b".into(), ValueType::Primitive(PrimitiveValue::F64(0.3))),
                ("a".into(), ValueType::Primitive(PrimitiveValue::F64(0.4))),
            ])
        );
        let back_to_rgba: Rgba = rgba_mvalue.clone().into();
        assert_eq!(rgba, back_to_rgba);

        let vp: VectorPoint<Rgba> = VectorPoint { x: 1.0, y: 2.0, z: None, m: Some(rgba), t: None };
        let vp_mvalue: MValue = vp.m.unwrap().into();
        assert_eq!(vp_mvalue, rgba_mvalue);

        // distance
        let a: VectorPoint<Rgba> = VectorPoint { x: 1.0, y: 2.0, z: None, m: Some(rgba), t: None };
        let b: VectorPoint = VectorPoint::new(3.0, 4.0, None, None);
        let dist = a.distance(&b);
        assert_eq!(dist, 2.8284271247461903);
    }

    #[test]
    fn to_mapbox() {
        let value: MValue = MValue::from([
            ("a".into(), "b".into()),
            ("c".into(), 2.0_f32.into()),
            (
                "d".into(),
                MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
            ),
        ]);
        let mapbox_value: MapboxProperties = value.clone().into();
        assert_eq!(
            mapbox_value,
            MapboxProperties::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),])
        );
    }

    #[test]
    fn from_mapbox() {
        let mapbox_value: MapboxProperties = MapboxProperties::from([("a".into(), "b".into())]);
        let value: MValue = mapbox_value.clone().into();
        assert_eq!(value, MValue::from([("a".into(), "b".into()),]));
    }

    #[test]
    fn to_json_obj() {
        let value: MValue = MValue::from([
            ("a".into(), "b".into()),
            ("c".into(), 2.0_f32.into()),
            (
                "d".into(),
                MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
            ),
            (
                "e".into(),
                Vec::<ValuePrimitiveType>::from(["a".into(), "b".into(), "c".into()]).into(),
            ),
        ]);
        let json_value: JSONProperties = value.clone().into();
        assert_eq!(
            json_value,
            JSONProperties::from([
                ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
                ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
                (
                    "d".into(),
                    JSONValue::Object(JSONProperties::from([
                        ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
                        ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
                    ]))
                ),
                (
                    "e".into(),
                    JSONValue::Array(Vec::from([
                        JSONValue::Primitive(PrimitiveValue::String("a".into())),
                        JSONValue::Primitive(PrimitiveValue::String("b".into())),
                        JSONValue::Primitive(PrimitiveValue::String("c".into())),
                    ]))
                ),
            ])
        );

        // get prim
        let prim_a = json_value.get("a").unwrap().to_prim().unwrap().to_string().unwrap();
        assert_eq!(prim_a, "b");
        let failed_to_prim = json_value.get("d").unwrap().to_prim();
        assert_eq!(failed_to_prim, None);

        // get array
        let array_e = json_value.get("e").unwrap().to_vec().unwrap();
        assert_eq!(
            *array_e,
            Vec::from([
                JSONValue::Primitive(PrimitiveValue::String("a".into())),
                JSONValue::Primitive(PrimitiveValue::String("b".into())),
                JSONValue::Primitive(PrimitiveValue::String("c".into())),
            ])
        );
        let array_fail = json_value.get("a").unwrap().to_vec();
        assert_eq!(array_fail, None);

        // get obj
        let obj_d = json_value.get("d").unwrap().to_nested().unwrap();
        assert_eq!(
            *obj_d,
            JSONProperties::from([
                ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
                ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
            ])
        );
        let obj_fail = json_value.get("a").unwrap().to_nested();
        assert_eq!(obj_fail, None);
    }

    #[test]
    fn from_json_obj() {
        let json_value = JSONProperties::from([
            ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
            ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
            (
                "d".into(),
                JSONValue::Object(JSONProperties::from([
                    ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
                    ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
                ])),
            ),
            (
                "e".into(),
                JSONValue::Array(Vec::from([
                    JSONValue::Primitive(PrimitiveValue::String("a".into())),
                    JSONValue::Primitive(PrimitiveValue::String("b".into())),
                    JSONValue::Primitive(PrimitiveValue::String("c".into())),
                ])),
            ),
        ]);
        let value: MValue = json_value.clone().into();
        assert_eq!(
            value,
            MValue::from([
                ("a".into(), "b".into()),
                ("c".into(), 2.0_f32.into()),
                (
                    "d".into(),
                    MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
                ),
                (
                    "e".into(),
                    Vec::<ValuePrimitiveType>::from(["a".into(), "b".into(), "c".into()]).into(),
                ),
            ])
        );
    }

    #[test]
    fn test_prim_to_json() {
        let json: JSONValue = (&PrimitiveValue::String("test".into())).into();
        assert_eq!(json, JSONValue::Primitive(PrimitiveValue::String("test".into())));

        let prim: PrimitiveValue = (&json).into();
        assert_eq!(prim, PrimitiveValue::String("test".into()));

        // to prim but json is not a prim
        let json = JSONValue::Array(Vec::new());
        let prim: PrimitiveValue = (&json).into();
        assert_eq!(prim, PrimitiveValue::Null);
    }

    #[test]
    fn test_value_prim_type_to_json() {
        let prim = ValuePrimitiveType::NestedPrimitive(Map::from([
            ("a".into(), "b".into()),
            ("c".into(), 2.0_f32.into()),
        ]));
        let json: JSONValue = (&prim).into();
        assert_eq!(
            json,
            JSONValue::Object(JSONProperties::from([
                ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
                ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
            ]))
        );

        let json = JSONValue::Object(JSONProperties::from([
            ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
            ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
        ]));

        let prim: ValuePrimitiveType = (&json).into();
        assert_eq!(
            prim,
            ValuePrimitiveType::NestedPrimitive(Map::from([
                ("2".into(), "3".into()),
                ("4".into(), 2.0_f32.into()),
            ]))
        );

        // Array Fails
        let json = JSONValue::Array(Vec::from([
            JSONValue::Primitive(PrimitiveValue::String("c".into())),
            JSONValue::Primitive(PrimitiveValue::String("d".into())),
        ]));

        let prim: ValuePrimitiveType = (&json).into();
        assert_eq!(prim, ValuePrimitiveType::Primitive(PrimitiveValue::Null));
    }
}