1use crate::*;
2use alloc::{
3 string::{String, ToString},
4 vec::Vec,
5};
6use libm::round;
7
8impl PrimitiveValue {
10 pub fn is_null(&self) -> bool {
12 matches!(self, PrimitiveValue::Null)
13 }
14
15 pub fn to_string(&self) -> Option<String> {
17 match self {
18 PrimitiveValue::String(v) => Some(v.clone()),
19 _ => None,
20 }
21 }
22
23 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 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 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 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 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 _ => PrimitiveValue::Null,
137 }
138 }
139}
140
141impl ValuePrimitiveType {
143 pub fn to_prim(&self) -> Option<&PrimitiveValue> {
145 match self {
146 ValuePrimitiveType::Primitive(v) => Some(v),
147 _ => None,
148 }
149 }
150
151 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<u64> for ValuePrimitiveType {
170 fn from(v: u64) -> Self {
171 ValuePrimitiveType::Primitive(PrimitiveValue::U64(v))
172 }
173}
174impl From<i64> for ValuePrimitiveType {
175 fn from(v: i64) -> Self {
176 ValuePrimitiveType::Primitive(PrimitiveValue::I64(v))
177 }
178}
179impl From<f32> for ValuePrimitiveType {
180 fn from(v: f32) -> Self {
181 ValuePrimitiveType::Primitive(PrimitiveValue::F32(v))
182 }
183}
184impl From<f64> for ValuePrimitiveType {
185 fn from(v: f64) -> Self {
186 ValuePrimitiveType::Primitive(PrimitiveValue::F64(v))
187 }
188}
189impl From<bool> for ValuePrimitiveType {
190 fn from(v: bool) -> Self {
191 ValuePrimitiveType::Primitive(PrimitiveValue::Bool(v))
192 }
193}
194impl From<()> for ValuePrimitiveType {
195 fn from(_: ()) -> Self {
196 ValuePrimitiveType::Primitive(PrimitiveValue::Null)
197 }
198}
199impl From<PrimitiveValue> for ValuePrimitiveType {
200 fn from(v: PrimitiveValue) -> Self {
201 ValuePrimitiveType::Primitive(v)
202 }
203}
204impl From<ValuePrimitive> for ValuePrimitiveType {
205 fn from(v: ValuePrimitive) -> Self {
206 ValuePrimitiveType::NestedPrimitive(v)
207 }
208}
209impl<T> From<Option<T>> for ValuePrimitiveType
210where
211 T: Into<ValuePrimitiveType>,
212{
213 fn from(v: Option<T>) -> Self {
214 match v {
215 Some(v) => v.into(),
216 None => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
217 }
218 }
219}
220impl From<&ValuePrimitiveType> for JSONValue {
221 fn from(v: &ValuePrimitiveType) -> Self {
222 match v {
223 ValuePrimitiveType::Primitive(v) => JSONValue::Primitive(v.clone()),
224 ValuePrimitiveType::NestedPrimitive(v) => {
225 let mut map = Map::<String, JSONValue>::new();
226 for (k, v) in v.iter() {
227 map.insert(k.clone(), v.into());
228 }
229 JSONValue::Object(map)
230 }
231 }
232 }
233}
234impl From<&JSONValue> for ValuePrimitiveType {
235 fn from(v: &JSONValue) -> Self {
236 match v {
237 JSONValue::Primitive(v) => ValuePrimitiveType::Primitive(v.clone()),
238 JSONValue::Object(v) => {
239 let mut map = ValuePrimitive::new();
240 for (k, v) in v.iter() {
241 map.insert(k.clone(), v.into());
242 }
243 ValuePrimitiveType::NestedPrimitive(map)
244 }
245 _ => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
247 }
248 }
249}
250
251impl Default for ValueType {
253 fn default() -> Self {
254 ValueType::Primitive(PrimitiveValue::Null)
255 }
256}
257impl ValueType {
258 pub fn to_prim(&self) -> Option<&PrimitiveValue> {
260 match self {
261 ValueType::Primitive(v) => Some(v),
262 _ => None,
263 }
264 }
265
266 pub fn to_vec(&self) -> Option<&Vec<ValuePrimitiveType>> {
268 match self {
269 ValueType::Array(v) => Some(v),
270 _ => None,
271 }
272 }
273
274 pub fn to_nested(&self) -> Option<&Value> {
276 match self {
277 ValueType::Nested(v) => Some(v),
278 _ => None,
279 }
280 }
281}
282impl From<&str> for ValueType {
283 fn from(s: &str) -> Self {
284 ValueType::Primitive(PrimitiveValue::String(s.to_string()))
285 }
286}
287impl AsRef<str> for ValueType {
288 fn as_ref(&self) -> &str {
289 match self {
290 ValueType::Primitive(PrimitiveValue::String(s)) => s.as_str(),
291 _ => "",
292 }
293 }
294}
295impl From<String> for ValueType {
296 fn from(s: String) -> Self {
297 ValueType::Primitive(PrimitiveValue::String(s))
298 }
299}
300impl From<ValueType> for String {
301 fn from(v: ValueType) -> Self {
302 match v {
303 ValueType::Primitive(PrimitiveValue::String(s)) => s,
304 _ => "".to_string(),
305 }
306 }
307}
308
309macro_rules! impl_from_int {
311 ($($t:ty),*) => {
312 $(
313 impl From<$t> for ValueType {
314 fn from(v: $t) -> Self {
315 ValueType::Primitive(PrimitiveValue::U64(v as u64))
316 }
317 }
318
319 impl From<ValueType> for $t {
320 fn from(v: ValueType) -> Self {
321 match v {
322 ValueType::Primitive(PrimitiveValue::U64(v)) => v as $t,
323 _ => 0,
324 }
325 }
326 }
327 )*
328 };
329}
330impl_from_int!(u8, u16, u32, u64, usize);
331macro_rules! impl_from_int {
333 ($($t:ty),*) => {
334 $(
335 impl From<$t> for ValueType {
336 fn from(v: $t) -> Self {
337 ValueType::Primitive(PrimitiveValue::I64(v as i64))
338 }
339 }
340
341 impl From<ValueType> for $t {
342 fn from(v: ValueType) -> Self {
343 match v {
344 ValueType::Primitive(PrimitiveValue::I64(v)) => v as $t,
345 _ => 0,
346 }
347 }
348 }
349 )*
350 };
351}
352impl_from_int!(i8, i16, i32, i64, isize);
353impl From<f32> for ValueType {
354 fn from(v: f32) -> Self {
355 ValueType::Primitive(PrimitiveValue::F32(v))
356 }
357}
358impl From<ValueType> for f32 {
359 fn from(v: ValueType) -> Self {
360 match v {
361 ValueType::Primitive(PrimitiveValue::F32(v)) => v,
362 _ => 0.0,
363 }
364 }
365}
366impl From<f64> for ValueType {
367 fn from(v: f64) -> Self {
368 ValueType::Primitive(PrimitiveValue::F64(v))
369 }
370}
371impl From<ValueType> for f64 {
372 fn from(v: ValueType) -> Self {
373 match v {
374 ValueType::Primitive(PrimitiveValue::F64(v)) => v,
375 _ => 0.0,
376 }
377 }
378}
379impl From<bool> for ValueType {
380 fn from(v: bool) -> Self {
381 ValueType::Primitive(PrimitiveValue::Bool(v))
382 }
383}
384impl From<ValueType> for bool {
385 fn from(v: ValueType) -> Self {
386 match v {
387 ValueType::Primitive(PrimitiveValue::Bool(v)) => v,
388 _ => false,
389 }
390 }
391}
392impl From<()> for ValueType {
393 fn from(_: ()) -> Self {
394 ValueType::Primitive(PrimitiveValue::Null)
395 }
396}
397impl From<ValueType> for () {
398 fn from(_: ValueType) -> Self {}
399}
400impl<T> From<Vec<T>> for ValueType
401where
402 T: Into<ValuePrimitiveType>,
403{
404 fn from(v: Vec<T>) -> Self {
405 ValueType::Array(v.into_iter().map(Into::into).collect())
406 }
407}
408impl<T> From<ValueType> for Vec<T>
409where
410 T: From<ValuePrimitiveType>,
411{
412 fn from(v: ValueType) -> Self {
413 match v {
414 ValueType::Array(v) => v.into_iter().map(Into::into).collect(),
415 _ => Vec::new(),
416 }
417 }
418}
419impl From<Value> for ValueType {
420 fn from(v: Value) -> Self {
421 ValueType::Nested(v)
422 }
423}
424impl From<ValueType> for Value {
425 fn from(v: ValueType) -> Self {
426 match v {
427 ValueType::Nested(v) => v,
428 _ => Value::default(),
429 }
430 }
431}
432impl<T> From<Option<T>> for ValueType
433where
434 T: Into<ValueType>,
435{
436 fn from(v: Option<T>) -> Self {
437 match v {
438 Some(v) => v.into(),
439 None => ValueType::Primitive(PrimitiveValue::Null),
440 }
441 }
442}
443pub trait NotValueType {}
446macro_rules! impl_not_value_type {
448 ( $( $t:ty ),* ) => {
449 $(
450 impl NotValueType for $t {}
451 )*
452 };
453}
454impl_not_value_type!(
455 u8,
456 u16,
457 u32,
458 u64,
459 usize,
460 i8,
461 i16,
462 i32,
463 i64,
464 isize,
465 f32,
466 f64,
467 String,
468 &str,
469 bool,
470 ()
471);
472impl<T> From<ValueType> for Option<T>
473where
474 T: From<ValueType> + NotValueType, {
476 fn from(v: ValueType) -> Self {
477 match v {
478 ValueType::Primitive(PrimitiveValue::Null) => None,
479 _ => Some(v.into()),
480 }
481 }
482}
483impl From<&JSONValue> for ValueType {
496 fn from(v: &JSONValue) -> Self {
497 match v {
498 JSONValue::Primitive(v) => ValueType::Primitive(v.clone()),
499 JSONValue::Array(v) => ValueType::Array(v.iter().map(Into::into).collect()),
500 JSONValue::Object(v) => {
501 let mut res = Value::new();
502 for (k, v) in v.iter() {
503 res.insert(k.clone(), v.into());
504 }
505 ValueType::Nested(res)
506 }
507 }
508 }
509}
510impl From<&ValueType> for JSONValue {
511 fn from(v: &ValueType) -> Self {
512 match v {
513 ValueType::Primitive(v) => JSONValue::Primitive(v.clone()),
514 ValueType::Array(v) => JSONValue::Array(v.iter().map(Into::into).collect()),
515 ValueType::Nested(v) => {
516 let mut res = Map::<String, JSONValue>::new();
517 for (k, v) in v.iter() {
518 res.insert(k.clone(), v.into());
519 }
520 JSONValue::Object(res)
521 }
522 }
523 }
524}
525
526impl Default for JSONValue {
527 fn default() -> Self {
528 JSONValue::Primitive(PrimitiveValue::Null)
529 }
530}
531impl JSONValue {
532 pub fn to_prim(&self) -> Option<&PrimitiveValue> {
534 match self {
535 JSONValue::Primitive(v) => Some(v),
536 _ => None,
537 }
538 }
539
540 pub fn to_vec(&self) -> Option<&Vec<JSONValue>> {
542 match self {
543 JSONValue::Array(v) => Some(v),
544 _ => None,
545 }
546 }
547
548 pub fn to_nested(&self) -> Option<&Map<String, JSONValue>> {
550 match self {
551 JSONValue::Object(v) => Some(v),
552 _ => None,
553 }
554 }
555}
556
557impl MValueCompatible for JSONProperties {}
558impl From<JSONProperties> for MValue {
559 fn from(json: JSONProperties) -> MValue {
560 let mut res = MValue::new();
561 for (k, v) in json.iter() {
562 res.insert(k.clone(), v.into());
563 }
564 res
565 }
566}
567impl From<MValue> for JSONProperties {
568 fn from(v: MValue) -> JSONProperties {
569 let mut res = JSONProperties::new();
570 for (k, v) in v.iter() {
571 res.insert(k.clone(), v.into());
572 }
573 res
574 }
575}
576
577impl MValueCompatible for MapboxProperties {}
578impl From<MapboxProperties> for MValue {
579 fn from(json: MapboxProperties) -> MValue {
580 let mut res = MValue::new();
581 for (k, v) in json.iter() {
582 res.insert(k.clone(), ValueType::Primitive(v.clone()));
583 }
584 res
585 }
586}
587impl From<MValue> for MapboxProperties {
588 fn from(v: MValue) -> MapboxProperties {
589 let mut res = MapboxProperties::new();
590 for (k, v) in v.iter() {
592 let value = v.clone();
593 if let Some(p) = value.to_prim() {
594 res.insert(k.clone(), p.clone());
595 }
596 }
597 res
598 }
599}
600
601#[cfg(test)]
602mod tests {
603 use alloc::vec;
604
605 use crate::{MValue, MValueCompatible, VectorPoint};
606
607 use super::*;
608
609 #[test]
610 fn value_default() {
611 let default = ValueType::default();
612 assert_eq!(default, ValueType::Primitive(PrimitiveValue::Null));
613 }
614
615 #[test]
616 fn primitive_value_funcs() {
617 let prim_value: PrimitiveValue = "test".into();
619 assert_eq!(PrimitiveValue::String("test".into()), prim_value);
620 assert_eq!(prim_value.to_u64(), None);
621 assert_eq!(prim_value.to_i64(), None);
622 assert_eq!(prim_value.to_f32(), None);
623 assert_eq!(prim_value.to_f64(), None);
624 assert_eq!(prim_value.to_bool(), None);
625 assert!(!prim_value.is_null());
626 let prim_value_str: String = "test".into();
628 let prim_value: PrimitiveValue = prim_value_str.clone().into();
629 assert_eq!(PrimitiveValue::String("test".into()), prim_value);
630 assert_eq!(prim_value.to_string(), Some("test".into()));
631 let prim_value: PrimitiveValue = 1_u64.into();
633 assert_eq!(PrimitiveValue::U64(1), prim_value);
634 assert_eq!(prim_value.to_string(), None);
635 assert_eq!(prim_value.to_u64(), Some(1));
636 assert_eq!(prim_value.to_i64(), Some(1));
637 assert_eq!(prim_value.to_f32(), Some(1.0));
638 assert_eq!(prim_value.to_f64(), Some(1.0));
639 let prim_value: PrimitiveValue = (-1_i64).into();
641 assert_eq!(PrimitiveValue::I64(-1), prim_value);
642 assert_eq!(prim_value.to_u64(), Some(18446744073709551615));
643 assert_eq!(prim_value.to_i64(), Some(-1));
644 assert_eq!(prim_value.to_f32(), Some(-1.0));
645 assert_eq!(prim_value.to_f64(), Some(-1.0));
646 let prim_value: PrimitiveValue = (1.0_f32).into();
648 assert_eq!(PrimitiveValue::F32(1.0), prim_value);
649 assert_eq!(prim_value.to_u64(), Some(1));
650 assert_eq!(prim_value.to_i64(), Some(1));
651 assert_eq!(prim_value.to_f32(), Some(1.0));
652 assert_eq!(prim_value.to_f64(), Some(1.0));
653 let prim_value: PrimitiveValue = (1.0_f64).into();
655 assert_eq!(PrimitiveValue::F64(1.0), prim_value);
656 assert_eq!(prim_value.to_u64(), Some(1));
657 assert_eq!(prim_value.to_i64(), Some(1));
658 assert_eq!(prim_value.to_f32(), Some(1.0));
659 assert_eq!(prim_value.to_f64(), Some(1.0));
660 let prim_value: PrimitiveValue = true.into();
662 assert_eq!(PrimitiveValue::Bool(true), prim_value);
663 assert_eq!(prim_value.to_bool(), Some(true));
664 let prim_value: PrimitiveValue = ().into();
666 assert_eq!(PrimitiveValue::Null, prim_value);
667 assert!(prim_value.is_null());
668 let prim_value: PrimitiveValue = Some(true).into();
670 assert_eq!(PrimitiveValue::Bool(true), prim_value);
671 assert_eq!(prim_value.to_bool(), Some(true));
672 let prim_value: PrimitiveValue = None::<bool>.into();
673 assert_eq!(PrimitiveValue::Null, prim_value);
674 assert!(prim_value.is_null());
675 }
676
677 #[test]
678 fn value_prim_type_funcs() {
679 let prim_value: ValuePrimitiveType = "test".into();
681 assert_eq!(
682 ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
683 prim_value
684 );
685 assert_eq!(prim_value.to_prim(), Some(PrimitiveValue::String("test".into())).as_ref());
686 assert_eq!(prim_value.to_nested(), None);
687 let prim_value_str: String = "test".into();
689 let prim_value: ValuePrimitiveType = prim_value_str.clone().into();
690 assert_eq!(
691 ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
692 prim_value
693 );
694 let prim_value: ValuePrimitiveType = 1_u64.into();
696 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::U64(1)), prim_value);
697 let prim_value: ValuePrimitiveType = (-1_i64).into();
699 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::I64(-1)), prim_value);
700 let prim_value: ValuePrimitiveType = (1.0_f32).into();
702 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::F32(1.0)), prim_value);
703 let prim_value: ValuePrimitiveType = (1.0_f64).into();
705 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::F64(1.0)), prim_value);
706 let prim_value: ValuePrimitiveType = true.into();
708 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Bool(true)), prim_value);
709 let prim_value: ValuePrimitiveType = ().into();
711 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Null), prim_value);
712
713 let nested: ValuePrimitiveType = PrimitiveValue::Bool(true).into();
715 assert_eq!(nested.to_prim().unwrap().to_bool(), Some(true));
716
717 let nested: ValuePrimitiveType =
719 ValuePrimitive::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into())]).into();
720 assert_eq!(nested.to_prim(), None);
721 assert_eq!(
722 nested.to_nested(),
723 Some(ValuePrimitive::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),]))
724 .as_ref()
725 );
726
727 let prim_value: ValuePrimitiveType = Some(true).into();
729 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Bool(true)), prim_value);
730 let prim_value: ValuePrimitiveType = None::<bool>.into();
731 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Null), prim_value);
732 }
733
734 #[test]
735 fn value_funcs() {
736 let prim_value: ValueType = "test".into();
738 assert_eq!(ValueType::Primitive(PrimitiveValue::String("test".into())), prim_value);
739 let prim = prim_value.to_prim().unwrap();
740 assert_eq!(*prim, PrimitiveValue::String("test".into()));
741 assert_eq!(prim_value.to_nested(), None);
742 assert_eq!(prim_value.to_vec(), None);
743 let prim_value_str: String = "test".into();
745 let prim_value: ValueType = prim_value_str.into();
746 assert_eq!(ValueType::Primitive(PrimitiveValue::String("test".into())), prim_value);
747 let prim_value: ValueType = 1_u64.into();
749 assert_eq!(ValueType::Primitive(PrimitiveValue::U64(1)), prim_value);
750 let prim_value: ValueType = (-1_i64).into();
752 assert_eq!(ValueType::Primitive(PrimitiveValue::I64(-1)), prim_value);
753 let prim_value: ValueType = (1.0_f32).into();
755 assert_eq!(ValueType::Primitive(PrimitiveValue::F32(1.0)), prim_value);
756 let prim_value: ValueType = (1.0_f64).into();
758 assert_eq!(ValueType::Primitive(PrimitiveValue::F64(1.0)), prim_value);
759 let prim_value: ValueType = true.into();
761 assert_eq!(ValueType::Primitive(PrimitiveValue::Bool(true)), prim_value);
762 let prim_value: ValueType = ().into();
764 assert_eq!(ValueType::Primitive(PrimitiveValue::Null), prim_value);
765
766 let prim_value: ValueType = vec!["test", "test2"].into();
768 assert_eq!(prim_value.to_prim(), None);
769 assert_eq!(
770 ValueType::Array(vec![
771 ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
772 ValuePrimitiveType::Primitive(PrimitiveValue::String("test2".into())),
773 ]),
774 prim_value
775 );
776 let back_to_vec: Vec<String> =
777 prim_value.to_vec().unwrap().iter().filter_map(|v| v.to_prim()?.to_string()).collect();
778 assert_eq!(back_to_vec, vec!["test", "test2"]);
779
780 let nested: ValueType =
782 Value::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into())]).into();
783 assert_eq!(nested.to_vec(), None);
784 assert_eq!(
785 nested.to_nested(),
786 Some(Value::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),])).as_ref()
787 );
788
789 let prim_value: ValueType = Some(true).into();
791 assert_eq!(ValueType::Primitive(PrimitiveValue::Bool(true)), prim_value);
792 let prim_value: ValueType = None::<bool>.into();
793 assert_eq!(ValueType::Primitive(PrimitiveValue::Null), prim_value);
794 }
795
796 #[test]
797 fn test_rgba_struct() {
798 #[derive(Debug, Clone, Copy, PartialEq, Default)]
799 pub struct Rgba {
800 pub r: f64,
802 pub g: f64,
804 pub b: f64,
806 pub a: f64,
808 }
809 impl Rgba {
810 pub fn new(r: f64, g: f64, b: f64, a: f64) -> Self {
812 Self { r, g, b, a }
813 }
814 }
815 impl MValueCompatible for Rgba {}
816 impl From<Rgba> for MValue {
817 fn from(rgba: Rgba) -> MValue {
818 MValue::from([
819 ("r".into(), (rgba.r).into()),
820 ("g".into(), (rgba.g).into()),
821 ("b".into(), (rgba.b).into()),
822 ("a".into(), (rgba.a).into()),
823 ])
824 }
825 }
826 impl From<MValue> for Rgba {
827 fn from(mvalue: MValue) -> Self {
828 let r: f64 = mvalue.get("r").unwrap().to_prim().unwrap().to_f64().unwrap();
829 let g = mvalue.get("g").unwrap().to_prim().unwrap().to_f64().unwrap();
830 let b = mvalue.get("b").unwrap().to_prim().unwrap().to_f64().unwrap();
831 let a = mvalue.get("a").unwrap().to_prim().unwrap().to_f64().unwrap();
832 Rgba::new(r, g, b, a)
833 }
834 }
835
836 let rgba = Rgba::new(0.1, 0.2, 0.3, 0.4);
837 let rgba_mvalue: MValue = rgba.into();
838 assert_eq!(
839 rgba_mvalue,
840 MValue::from([
841 ("r".into(), ValueType::Primitive(PrimitiveValue::F64(0.1))),
842 ("g".into(), ValueType::Primitive(PrimitiveValue::F64(0.2))),
843 ("b".into(), ValueType::Primitive(PrimitiveValue::F64(0.3))),
844 ("a".into(), ValueType::Primitive(PrimitiveValue::F64(0.4))),
845 ])
846 );
847 let back_to_rgba: Rgba = rgba_mvalue.clone().into();
848 assert_eq!(rgba, back_to_rgba);
849
850 let vp: VectorPoint<Rgba> = VectorPoint { x: 1.0, y: 2.0, z: None, m: Some(rgba), t: None };
851 let vp_mvalue: MValue = vp.m.unwrap().into();
852 assert_eq!(vp_mvalue, rgba_mvalue);
853
854 let a: VectorPoint<Rgba> = VectorPoint { x: 1.0, y: 2.0, z: None, m: Some(rgba), t: None };
856 let b: VectorPoint = VectorPoint::new(3.0, 4.0, None, None);
857 let dist = a.distance(&b);
858 assert_eq!(dist, 2.8284271247461903);
859 }
860
861 #[test]
862 fn to_mapbox() {
863 let value: MValue = MValue::from([
864 ("a".into(), "b".into()),
865 ("c".into(), 2.0_f32.into()),
866 (
867 "d".into(),
868 MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
869 ),
870 ]);
871 let mapbox_value: MapboxProperties = value.clone().into();
872 assert_eq!(
873 mapbox_value,
874 MapboxProperties::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),])
875 );
876 }
877
878 #[test]
879 fn from_mapbox() {
880 let mapbox_value: MapboxProperties = MapboxProperties::from([("a".into(), "b".into())]);
881 let value: MValue = mapbox_value.clone().into();
882 assert_eq!(value, MValue::from([("a".into(), "b".into()),]));
883 }
884
885 #[test]
886 fn to_json_obj() {
887 let value: MValue = MValue::from([
888 ("a".into(), "b".into()),
889 ("c".into(), 2.0_f32.into()),
890 (
891 "d".into(),
892 MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
893 ),
894 (
895 "e".into(),
896 Vec::<ValuePrimitiveType>::from(["a".into(), "b".into(), "c".into()]).into(),
897 ),
898 ]);
899 let json_value: JSONProperties = value.clone().into();
900 assert_eq!(
901 json_value,
902 JSONProperties::from([
903 ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
904 ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
905 (
906 "d".into(),
907 JSONValue::Object(JSONProperties::from([
908 ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
909 ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
910 ]))
911 ),
912 (
913 "e".into(),
914 JSONValue::Array(Vec::from([
915 JSONValue::Primitive(PrimitiveValue::String("a".into())),
916 JSONValue::Primitive(PrimitiveValue::String("b".into())),
917 JSONValue::Primitive(PrimitiveValue::String("c".into())),
918 ]))
919 ),
920 ])
921 );
922
923 let prim_a = json_value.get("a").unwrap().to_prim().unwrap().to_string().unwrap();
925 assert_eq!(prim_a, "b");
926 let failed_to_prim = json_value.get("d").unwrap().to_prim();
927 assert_eq!(failed_to_prim, None);
928
929 let array_e = json_value.get("e").unwrap().to_vec().unwrap();
931 assert_eq!(
932 *array_e,
933 Vec::from([
934 JSONValue::Primitive(PrimitiveValue::String("a".into())),
935 JSONValue::Primitive(PrimitiveValue::String("b".into())),
936 JSONValue::Primitive(PrimitiveValue::String("c".into())),
937 ])
938 );
939 let array_fail = json_value.get("a").unwrap().to_vec();
940 assert_eq!(array_fail, None);
941
942 let obj_d = json_value.get("d").unwrap().to_nested().unwrap();
944 assert_eq!(
945 *obj_d,
946 JSONProperties::from([
947 ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
948 ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
949 ])
950 );
951 let obj_fail = json_value.get("a").unwrap().to_nested();
952 assert_eq!(obj_fail, None);
953 }
954
955 #[test]
956 fn from_json_obj() {
957 let json_value = JSONProperties::from([
958 ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
959 ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
960 (
961 "d".into(),
962 JSONValue::Object(JSONProperties::from([
963 ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
964 ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
965 ])),
966 ),
967 (
968 "e".into(),
969 JSONValue::Array(Vec::from([
970 JSONValue::Primitive(PrimitiveValue::String("a".into())),
971 JSONValue::Primitive(PrimitiveValue::String("b".into())),
972 JSONValue::Primitive(PrimitiveValue::String("c".into())),
973 ])),
974 ),
975 ]);
976 let value: MValue = json_value.clone().into();
977 assert_eq!(
978 value,
979 MValue::from([
980 ("a".into(), "b".into()),
981 ("c".into(), 2.0_f32.into()),
982 (
983 "d".into(),
984 MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
985 ),
986 (
987 "e".into(),
988 Vec::<ValuePrimitiveType>::from(["a".into(), "b".into(), "c".into()]).into(),
989 ),
990 ])
991 );
992 }
993
994 #[test]
995 fn test_prim_to_json() {
996 let json: JSONValue = (&PrimitiveValue::String("test".into())).into();
997 assert_eq!(json, JSONValue::Primitive(PrimitiveValue::String("test".into())));
998
999 let prim: PrimitiveValue = (&json).into();
1000 assert_eq!(prim, PrimitiveValue::String("test".into()));
1001
1002 let json = JSONValue::Array(Vec::new());
1004 let prim: PrimitiveValue = (&json).into();
1005 assert_eq!(prim, PrimitiveValue::Null);
1006 }
1007
1008 #[test]
1009 fn test_value_prim_type_to_json() {
1010 let prim = ValuePrimitiveType::NestedPrimitive(Map::from([
1011 ("a".into(), "b".into()),
1012 ("c".into(), 2.0_f32.into()),
1013 ]));
1014 let json: JSONValue = (&prim).into();
1015 assert_eq!(
1016 json,
1017 JSONValue::Object(JSONProperties::from([
1018 ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
1019 ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1020 ]))
1021 );
1022
1023 let json = JSONValue::Object(JSONProperties::from([
1024 ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
1025 ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
1026 ]));
1027
1028 let prim: ValuePrimitiveType = (&json).into();
1029 assert_eq!(
1030 prim,
1031 ValuePrimitiveType::NestedPrimitive(Map::from([
1032 ("2".into(), "3".into()),
1033 ("4".into(), 2.0_f32.into()),
1034 ]))
1035 );
1036
1037 let json = JSONValue::Array(Vec::from([
1039 JSONValue::Primitive(PrimitiveValue::String("c".into())),
1040 JSONValue::Primitive(PrimitiveValue::String("d".into())),
1041 ]));
1042
1043 let prim: ValuePrimitiveType = (&json).into();
1044 assert_eq!(prim, ValuePrimitiveType::Primitive(PrimitiveValue::Null));
1045 }
1046}