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