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