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