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}
443impl From<&JSONValue> for ValueType {
444 fn from(v: &JSONValue) -> Self {
445 match v {
446 JSONValue::Primitive(v) => ValueType::Primitive(v.clone()),
447 JSONValue::Array(v) => ValueType::Array(v.iter().map(Into::into).collect()),
448 JSONValue::Object(v) => {
449 let mut res = Value::new();
450 for (k, v) in v.iter() {
451 res.insert(k.clone(), v.into());
452 }
453 ValueType::Nested(res)
454 }
455 }
456 }
457}
458impl From<&ValueType> for JSONValue {
459 fn from(v: &ValueType) -> Self {
460 match v {
461 ValueType::Primitive(v) => JSONValue::Primitive(v.clone()),
462 ValueType::Array(v) => JSONValue::Array(v.iter().map(Into::into).collect()),
463 ValueType::Nested(v) => {
464 let mut res = Map::<String, JSONValue>::new();
465 for (k, v) in v.iter() {
466 res.insert(k.clone(), v.into());
467 }
468 JSONValue::Object(res)
469 }
470 }
471 }
472}
473
474impl Default for JSONValue {
475 fn default() -> Self {
476 JSONValue::Primitive(PrimitiveValue::Null)
477 }
478}
479impl JSONValue {
480 pub fn to_prim(&self) -> Option<&PrimitiveValue> {
482 match self {
483 JSONValue::Primitive(v) => Some(v),
484 _ => None,
485 }
486 }
487
488 pub fn to_vec(&self) -> Option<&Vec<JSONValue>> {
490 match self {
491 JSONValue::Array(v) => Some(v),
492 _ => None,
493 }
494 }
495
496 pub fn to_nested(&self) -> Option<&Map<String, JSONValue>> {
498 match self {
499 JSONValue::Object(v) => Some(v),
500 _ => None,
501 }
502 }
503}
504
505impl MValueCompatible for JSONProperties {}
506impl From<JSONProperties> for MValue {
507 fn from(json: JSONProperties) -> MValue {
508 let mut res = MValue::new();
509 for (k, v) in json.iter() {
510 res.insert(k.clone(), v.into());
511 }
512 res
513 }
514}
515impl From<MValue> for JSONProperties {
516 fn from(v: MValue) -> JSONProperties {
517 let mut res = JSONProperties::new();
518 for (k, v) in v.iter() {
519 res.insert(k.clone(), v.into());
520 }
521 res
522 }
523}
524
525impl MValueCompatible for MapboxProperties {}
526impl From<MapboxProperties> for MValue {
527 fn from(json: MapboxProperties) -> MValue {
528 let mut res = MValue::new();
529 for (k, v) in json.iter() {
530 res.insert(k.clone(), ValueType::Primitive(v.clone()));
531 }
532 res
533 }
534}
535impl From<MValue> for MapboxProperties {
536 fn from(v: MValue) -> MapboxProperties {
537 let mut res = MapboxProperties::new();
538 for (k, v) in v.iter() {
540 let value = v.clone();
541 if let Some(p) = value.to_prim() {
542 res.insert(k.clone(), p.clone());
543 }
544 }
545 res
546 }
547}
548
549#[cfg(test)]
550mod tests {
551 use alloc::vec;
552
553 use crate::{MValue, MValueCompatible, VectorPoint};
554
555 use super::*;
556
557 #[test]
558 fn value_default() {
559 let default = ValueType::default();
560 assert_eq!(default, ValueType::Primitive(PrimitiveValue::Null));
561 }
562
563 #[test]
564 fn primitive_value_funcs() {
565 let prim_value: PrimitiveValue = "test".into();
567 assert_eq!(PrimitiveValue::String("test".into()), prim_value);
568 assert_eq!(prim_value.to_u64(), None);
569 assert_eq!(prim_value.to_i64(), None);
570 assert_eq!(prim_value.to_f32(), None);
571 assert_eq!(prim_value.to_f64(), None);
572 assert_eq!(prim_value.to_bool(), None);
573 assert!(!prim_value.is_null());
574 let prim_value_str: String = "test".into();
576 let prim_value: PrimitiveValue = prim_value_str.clone().into();
577 assert_eq!(PrimitiveValue::String("test".into()), prim_value);
578 assert_eq!(prim_value.to_string(), Some("test".into()));
579 let prim_value: PrimitiveValue = 1_u64.into();
581 assert_eq!(PrimitiveValue::U64(1), prim_value);
582 assert_eq!(prim_value.to_string(), None);
583 assert_eq!(prim_value.to_u64(), Some(1));
584 assert_eq!(prim_value.to_i64(), Some(1));
585 assert_eq!(prim_value.to_f32(), Some(1.0));
586 assert_eq!(prim_value.to_f64(), Some(1.0));
587 let prim_value: PrimitiveValue = (-1_i64).into();
589 assert_eq!(PrimitiveValue::I64(-1), prim_value);
590 assert_eq!(prim_value.to_u64(), Some(18446744073709551615));
591 assert_eq!(prim_value.to_i64(), Some(-1));
592 assert_eq!(prim_value.to_f32(), Some(-1.0));
593 assert_eq!(prim_value.to_f64(), Some(-1.0));
594 let prim_value: PrimitiveValue = (1.0_f32).into();
596 assert_eq!(PrimitiveValue::F32(1.0), prim_value);
597 assert_eq!(prim_value.to_u64(), Some(1));
598 assert_eq!(prim_value.to_i64(), Some(1));
599 assert_eq!(prim_value.to_f32(), Some(1.0));
600 assert_eq!(prim_value.to_f64(), Some(1.0));
601 let prim_value: PrimitiveValue = (1.0_f64).into();
603 assert_eq!(PrimitiveValue::F64(1.0), prim_value);
604 assert_eq!(prim_value.to_u64(), Some(1));
605 assert_eq!(prim_value.to_i64(), Some(1));
606 assert_eq!(prim_value.to_f32(), Some(1.0));
607 assert_eq!(prim_value.to_f64(), Some(1.0));
608 let prim_value: PrimitiveValue = true.into();
610 assert_eq!(PrimitiveValue::Bool(true), prim_value);
611 assert_eq!(prim_value.to_bool(), Some(true));
612 let prim_value: PrimitiveValue = ().into();
614 assert_eq!(PrimitiveValue::Null, prim_value);
615 assert!(prim_value.is_null());
616 let prim_value: PrimitiveValue = Some(true).into();
618 assert_eq!(PrimitiveValue::Bool(true), prim_value);
619 assert_eq!(prim_value.to_bool(), Some(true));
620 let prim_value: PrimitiveValue = None::<bool>.into();
621 assert_eq!(PrimitiveValue::Null, prim_value);
622 assert!(prim_value.is_null());
623 }
624
625 #[test]
626 fn value_prim_type_funcs() {
627 let prim_value: ValuePrimitiveType = "test".into();
629 assert_eq!(
630 ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
631 prim_value
632 );
633 assert_eq!(prim_value.to_prim(), Some(PrimitiveValue::String("test".into())).as_ref());
634 assert_eq!(prim_value.to_nested(), None);
635 let prim_value_str: String = "test".into();
637 let prim_value: ValuePrimitiveType = prim_value_str.clone().into();
638 assert_eq!(
639 ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
640 prim_value
641 );
642 let prim_value: ValuePrimitiveType = 1_u64.into();
644 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::U64(1)), prim_value);
645 let prim_value: ValuePrimitiveType = (-1_i64).into();
647 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::I64(-1)), prim_value);
648 let prim_value: ValuePrimitiveType = (1.0_f32).into();
650 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::F32(1.0)), prim_value);
651 let prim_value: ValuePrimitiveType = (1.0_f64).into();
653 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::F64(1.0)), prim_value);
654 let prim_value: ValuePrimitiveType = true.into();
656 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Bool(true)), prim_value);
657 let prim_value: ValuePrimitiveType = ().into();
659 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Null), prim_value);
660
661 let nested: ValuePrimitiveType = PrimitiveValue::Bool(true).into();
663 assert_eq!(nested.to_prim().unwrap().to_bool(), Some(true));
664
665 let nested: ValuePrimitiveType =
667 ValuePrimitive::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into())]).into();
668 assert_eq!(nested.to_prim(), None);
669 assert_eq!(
670 nested.to_nested(),
671 Some(ValuePrimitive::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),]))
672 .as_ref()
673 );
674
675 let prim_value: ValuePrimitiveType = Some(true).into();
677 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Bool(true)), prim_value);
678 let prim_value: ValuePrimitiveType = None::<bool>.into();
679 assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Null), prim_value);
680 }
681
682 #[test]
683 fn value_funcs() {
684 let prim_value: ValueType = "test".into();
686 assert_eq!(ValueType::Primitive(PrimitiveValue::String("test".into())), prim_value);
687 let prim = prim_value.to_prim().unwrap();
688 assert_eq!(*prim, PrimitiveValue::String("test".into()));
689 assert_eq!(prim_value.to_nested(), None);
690 assert_eq!(prim_value.to_vec(), None);
691 let prim_value_str: String = "test".into();
693 let prim_value: ValueType = prim_value_str.into();
694 assert_eq!(ValueType::Primitive(PrimitiveValue::String("test".into())), prim_value);
695 let prim_value: ValueType = 1_u64.into();
697 assert_eq!(ValueType::Primitive(PrimitiveValue::U64(1)), prim_value);
698 let prim_value: ValueType = (-1_i64).into();
700 assert_eq!(ValueType::Primitive(PrimitiveValue::I64(-1)), prim_value);
701 let prim_value: ValueType = (1.0_f32).into();
703 assert_eq!(ValueType::Primitive(PrimitiveValue::F32(1.0)), prim_value);
704 let prim_value: ValueType = (1.0_f64).into();
706 assert_eq!(ValueType::Primitive(PrimitiveValue::F64(1.0)), prim_value);
707 let prim_value: ValueType = true.into();
709 assert_eq!(ValueType::Primitive(PrimitiveValue::Bool(true)), prim_value);
710 let prim_value: ValueType = ().into();
712 assert_eq!(ValueType::Primitive(PrimitiveValue::Null), prim_value);
713
714 let prim_value: ValueType = vec!["test", "test2"].into();
716 assert_eq!(prim_value.to_prim(), None);
717 assert_eq!(
718 ValueType::Array(vec![
719 ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
720 ValuePrimitiveType::Primitive(PrimitiveValue::String("test2".into())),
721 ]),
722 prim_value
723 );
724 let back_to_vec: Vec<String> =
725 prim_value.to_vec().unwrap().iter().filter_map(|v| v.to_prim()?.to_string()).collect();
726 assert_eq!(back_to_vec, vec!["test", "test2"]);
727
728 let nested: ValueType =
730 Value::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into())]).into();
731 assert_eq!(nested.to_vec(), None);
732 assert_eq!(
733 nested.to_nested(),
734 Some(Value::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),])).as_ref()
735 );
736
737 let prim_value: ValueType = Some(true).into();
739 assert_eq!(ValueType::Primitive(PrimitiveValue::Bool(true)), prim_value);
740 let prim_value: ValueType = None::<bool>.into();
741 assert_eq!(ValueType::Primitive(PrimitiveValue::Null), prim_value);
742 }
743
744 #[test]
745 fn test_rgba_struct() {
746 #[derive(Debug, Clone, Copy, PartialEq, Default)]
747 pub struct Rgba {
748 pub r: f64,
750 pub g: f64,
752 pub b: f64,
754 pub a: f64,
756 }
757 impl Rgba {
758 pub fn new(r: f64, g: f64, b: f64, a: f64) -> Self {
760 Self { r, g, b, a }
761 }
762 }
763 impl MValueCompatible for Rgba {}
764 impl From<Rgba> for MValue {
765 fn from(rgba: Rgba) -> MValue {
766 MValue::from([
767 ("r".into(), (rgba.r).into()),
768 ("g".into(), (rgba.g).into()),
769 ("b".into(), (rgba.b).into()),
770 ("a".into(), (rgba.a).into()),
771 ])
772 }
773 }
774 impl From<MValue> for Rgba {
775 fn from(mvalue: MValue) -> Self {
776 let r: f64 = mvalue.get("r").unwrap().to_prim().unwrap().to_f64().unwrap();
777 let g = mvalue.get("g").unwrap().to_prim().unwrap().to_f64().unwrap();
778 let b = mvalue.get("b").unwrap().to_prim().unwrap().to_f64().unwrap();
779 let a = mvalue.get("a").unwrap().to_prim().unwrap().to_f64().unwrap();
780 Rgba::new(r, g, b, a)
781 }
782 }
783
784 let rgba = Rgba::new(0.1, 0.2, 0.3, 0.4);
785 let rgba_mvalue: MValue = rgba.into();
786 assert_eq!(
787 rgba_mvalue,
788 MValue::from([
789 ("r".into(), ValueType::Primitive(PrimitiveValue::F64(0.1))),
790 ("g".into(), ValueType::Primitive(PrimitiveValue::F64(0.2))),
791 ("b".into(), ValueType::Primitive(PrimitiveValue::F64(0.3))),
792 ("a".into(), ValueType::Primitive(PrimitiveValue::F64(0.4))),
793 ])
794 );
795 let back_to_rgba: Rgba = rgba_mvalue.clone().into();
796 assert_eq!(rgba, back_to_rgba);
797
798 let vp: VectorPoint<Rgba> = VectorPoint { x: 1.0, y: 2.0, z: None, m: Some(rgba), t: None };
799 let vp_mvalue: MValue = vp.m.unwrap().into();
800 assert_eq!(vp_mvalue, rgba_mvalue);
801
802 let a: VectorPoint<Rgba> = VectorPoint { x: 1.0, y: 2.0, z: None, m: Some(rgba), t: None };
804 let b: VectorPoint = VectorPoint::new(3.0, 4.0, None, None);
805 let dist = a.distance(&b);
806 assert_eq!(dist, 2.8284271247461903);
807 }
808
809 #[test]
810 fn to_mapbox() {
811 let value: MValue = MValue::from([
812 ("a".into(), "b".into()),
813 ("c".into(), 2.0_f32.into()),
814 (
815 "d".into(),
816 MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
817 ),
818 ]);
819 let mapbox_value: MapboxProperties = value.clone().into();
820 assert_eq!(
821 mapbox_value,
822 MapboxProperties::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),])
823 );
824 }
825
826 #[test]
827 fn from_mapbox() {
828 let mapbox_value: MapboxProperties = MapboxProperties::from([("a".into(), "b".into())]);
829 let value: MValue = mapbox_value.clone().into();
830 assert_eq!(value, MValue::from([("a".into(), "b".into()),]));
831 }
832
833 #[test]
834 fn to_json_obj() {
835 let value: MValue = MValue::from([
836 ("a".into(), "b".into()),
837 ("c".into(), 2.0_f32.into()),
838 (
839 "d".into(),
840 MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
841 ),
842 (
843 "e".into(),
844 Vec::<ValuePrimitiveType>::from(["a".into(), "b".into(), "c".into()]).into(),
845 ),
846 ]);
847 let json_value: JSONProperties = value.clone().into();
848 assert_eq!(
849 json_value,
850 JSONProperties::from([
851 ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
852 ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
853 (
854 "d".into(),
855 JSONValue::Object(JSONProperties::from([
856 ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
857 ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
858 ]))
859 ),
860 (
861 "e".into(),
862 JSONValue::Array(Vec::from([
863 JSONValue::Primitive(PrimitiveValue::String("a".into())),
864 JSONValue::Primitive(PrimitiveValue::String("b".into())),
865 JSONValue::Primitive(PrimitiveValue::String("c".into())),
866 ]))
867 ),
868 ])
869 );
870
871 let prim_a = json_value.get("a").unwrap().to_prim().unwrap().to_string().unwrap();
873 assert_eq!(prim_a, "b");
874 let failed_to_prim = json_value.get("d").unwrap().to_prim();
875 assert_eq!(failed_to_prim, None);
876
877 let array_e = json_value.get("e").unwrap().to_vec().unwrap();
879 assert_eq!(
880 *array_e,
881 Vec::from([
882 JSONValue::Primitive(PrimitiveValue::String("a".into())),
883 JSONValue::Primitive(PrimitiveValue::String("b".into())),
884 JSONValue::Primitive(PrimitiveValue::String("c".into())),
885 ])
886 );
887 let array_fail = json_value.get("a").unwrap().to_vec();
888 assert_eq!(array_fail, None);
889
890 let obj_d = json_value.get("d").unwrap().to_nested().unwrap();
892 assert_eq!(
893 *obj_d,
894 JSONProperties::from([
895 ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
896 ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
897 ])
898 );
899 let obj_fail = json_value.get("a").unwrap().to_nested();
900 assert_eq!(obj_fail, None);
901 }
902
903 #[test]
904 fn from_json_obj() {
905 let json_value = JSONProperties::from([
906 ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
907 ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
908 (
909 "d".into(),
910 JSONValue::Object(JSONProperties::from([
911 ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
912 ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
913 ])),
914 ),
915 (
916 "e".into(),
917 JSONValue::Array(Vec::from([
918 JSONValue::Primitive(PrimitiveValue::String("a".into())),
919 JSONValue::Primitive(PrimitiveValue::String("b".into())),
920 JSONValue::Primitive(PrimitiveValue::String("c".into())),
921 ])),
922 ),
923 ]);
924 let value: MValue = json_value.clone().into();
925 assert_eq!(
926 value,
927 MValue::from([
928 ("a".into(), "b".into()),
929 ("c".into(), 2.0_f32.into()),
930 (
931 "d".into(),
932 MValue::from([("2".into(), "3".into()), ("4".into(), 2.0_f32.into())]).into(),
933 ),
934 (
935 "e".into(),
936 Vec::<ValuePrimitiveType>::from(["a".into(), "b".into(), "c".into()]).into(),
937 ),
938 ])
939 );
940 }
941
942 #[test]
943 fn test_prim_to_json() {
944 let json: JSONValue = (&PrimitiveValue::String("test".into())).into();
945 assert_eq!(json, JSONValue::Primitive(PrimitiveValue::String("test".into())));
946
947 let prim: PrimitiveValue = (&json).into();
948 assert_eq!(prim, PrimitiveValue::String("test".into()));
949
950 let json = JSONValue::Array(Vec::new());
952 let prim: PrimitiveValue = (&json).into();
953 assert_eq!(prim, PrimitiveValue::Null);
954 }
955
956 #[test]
957 fn test_value_prim_type_to_json() {
958 let prim = ValuePrimitiveType::NestedPrimitive(Map::from([
959 ("a".into(), "b".into()),
960 ("c".into(), 2.0_f32.into()),
961 ]));
962 let json: JSONValue = (&prim).into();
963 assert_eq!(
964 json,
965 JSONValue::Object(JSONProperties::from([
966 ("a".into(), JSONValue::Primitive(PrimitiveValue::String("b".into()))),
967 ("c".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
968 ]))
969 );
970
971 let json = JSONValue::Object(JSONProperties::from([
972 ("2".into(), JSONValue::Primitive(PrimitiveValue::String("3".into()))),
973 ("4".into(), JSONValue::Primitive(PrimitiveValue::F32(2.0))),
974 ]));
975
976 let prim: ValuePrimitiveType = (&json).into();
977 assert_eq!(
978 prim,
979 ValuePrimitiveType::NestedPrimitive(Map::from([
980 ("2".into(), "3".into()),
981 ("4".into(), 2.0_f32.into()),
982 ]))
983 );
984
985 let json = JSONValue::Array(Vec::from([
987 JSONValue::Primitive(PrimitiveValue::String("c".into())),
988 JSONValue::Primitive(PrimitiveValue::String("d".into())),
989 ]));
990
991 let prim: ValuePrimitiveType = (&json).into();
992 assert_eq!(prim, ValuePrimitiveType::Primitive(PrimitiveValue::Null));
993 }
994}