s2json 1.8.3

This library supports the S2JSON 1.0 Specification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use alloc::{string::String, string::ToString, vec::Vec};

use crate::*;

// PrimitiveValue
impl PrimitiveValue {
    /// Returns true if the value is null
    pub fn is_null(&self) -> bool {
        matches!(self, PrimitiveValue::Null)
    }

    /// Converts a primitive value to a string
    pub fn to_string(&self) -> Option<String> {
        match self {
            PrimitiveValue::String(v) => Some(v.clone()),
            _ => None,
        }
    }

    /// Converts a primitive value to a u64
    pub fn to_u64(&self) -> Option<u64> {
        match self {
            PrimitiveValue::U64(v) => Some(*v),
            PrimitiveValue::I64(v) => Some(*v as u64),
            PrimitiveValue::F64(v) => Some(*v as u64),
            PrimitiveValue::F32(v) => Some(*v as u64),
            _ => None,
        }
    }

    /// Converts a primitive value to a i64
    pub fn to_i64(&self) -> Option<i64> {
        match self {
            PrimitiveValue::U64(v) => Some(*v as i64),
            PrimitiveValue::I64(v) => Some(*v),
            PrimitiveValue::F64(v) => Some(*v as i64),
            PrimitiveValue::F32(v) => Some(*v as i64),
            _ => None,
        }
    }

    /// Converts a primitive value to a f64
    pub fn to_f64(&self) -> Option<f64> {
        match self {
            PrimitiveValue::U64(v) => Some(*v as f64),
            PrimitiveValue::I64(v) => Some(*v as f64),
            PrimitiveValue::F64(v) => Some(*v),
            PrimitiveValue::F32(v) => Some(*v as f64),
            _ => None,
        }
    }

    /// Converts a primitive value to a f32
    pub fn to_f32(&self) -> Option<f32> {
        match self {
            PrimitiveValue::U64(v) => Some(*v as f32),
            PrimitiveValue::I64(v) => Some(*v as f32),
            PrimitiveValue::F64(v) => Some(*v as f32),
            PrimitiveValue::F32(v) => Some(*v),
            _ => None,
        }
    }

    /// Converts a primitive value to a bool
    pub fn to_bool(&self) -> Option<bool> {
        match self {
            PrimitiveValue::Bool(v) => Some(*v),
            _ => None,
        }
    }
}
impl From<&str> for PrimitiveValue {
    fn from(s: &str) -> Self {
        PrimitiveValue::String(s.to_string())
    }
}
impl From<String> for PrimitiveValue {
    fn from(s: String) -> Self {
        PrimitiveValue::String(s)
    }
}
impl From<u64> for PrimitiveValue {
    fn from(v: u64) -> Self {
        PrimitiveValue::U64(v)
    }
}
impl From<i64> for PrimitiveValue {
    fn from(v: i64) -> Self {
        PrimitiveValue::I64(v)
    }
}
impl From<f32> for PrimitiveValue {
    fn from(v: f32) -> Self {
        PrimitiveValue::F32(v)
    }
}
impl From<f64> for PrimitiveValue {
    fn from(v: f64) -> Self {
        PrimitiveValue::F64(v)
    }
}
impl From<bool> for PrimitiveValue {
    fn from(v: bool) -> Self {
        PrimitiveValue::Bool(v)
    }
}
impl From<()> for PrimitiveValue {
    fn from(_: ()) -> Self {
        PrimitiveValue::Null
    }
}
impl<T> From<Option<T>> for PrimitiveValue
where
    T: Into<PrimitiveValue>,
{
    fn from(v: Option<T>) -> Self {
        match v {
            Some(v) => v.into(),
            None => PrimitiveValue::Null,
        }
    }
}

// ValuePrimitiveType
impl ValuePrimitiveType {
    /// Returns the value as a primitive
    pub fn to_prim(&self) -> Option<&PrimitiveValue> {
        match self {
            ValuePrimitiveType::Primitive(v) => Some(v),
            _ => None,
        }
    }

    /// Returns the value as a nested object
    pub fn to_nested(&self) -> Option<&ValuePrimitive> {
        match self {
            ValuePrimitiveType::NestedPrimitive(v) => Some(v),
            _ => None,
        }
    }
}
impl From<&str> for ValuePrimitiveType {
    fn from(s: &str) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::String(s.to_string()))
    }
}
impl From<String> for ValuePrimitiveType {
    fn from(s: String) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::String(s))
    }
}
impl From<u64> for ValuePrimitiveType {
    fn from(v: u64) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::U64(v))
    }
}
impl From<i64> for ValuePrimitiveType {
    fn from(v: i64) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::I64(v))
    }
}
impl From<f32> for ValuePrimitiveType {
    fn from(v: f32) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::F32(v))
    }
}
impl From<f64> for ValuePrimitiveType {
    fn from(v: f64) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::F64(v))
    }
}
impl From<bool> for ValuePrimitiveType {
    fn from(v: bool) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::Bool(v))
    }
}
impl From<()> for ValuePrimitiveType {
    fn from(_: ()) -> Self {
        ValuePrimitiveType::Primitive(PrimitiveValue::Null)
    }
}
impl From<PrimitiveValue> for ValuePrimitiveType {
    fn from(v: PrimitiveValue) -> Self {
        ValuePrimitiveType::Primitive(v)
    }
}
impl From<ValuePrimitive> for ValuePrimitiveType {
    fn from(v: ValuePrimitive) -> Self {
        ValuePrimitiveType::NestedPrimitive(v)
    }
}
impl<T> From<Option<T>> for ValuePrimitiveType
where
    T: Into<ValuePrimitiveType>,
{
    fn from(v: Option<T>) -> Self {
        match v {
            Some(v) => v.into(),
            None => ValuePrimitiveType::Primitive(PrimitiveValue::Null),
        }
    }
}

// ValueType
impl Default for ValueType {
    fn default() -> Self {
        ValueType::Primitive(PrimitiveValue::Null)
    }
}
impl ValueType {
    /// Returns the value as a primitive
    pub fn to_prim(&self) -> Option<&PrimitiveValue> {
        match self {
            ValueType::Primitive(v) => Some(v),
            _ => None,
        }
    }

    /// Returns the value as a vector
    pub fn to_vec(&self) -> Option<&Vec<ValuePrimitiveType>> {
        match self {
            ValueType::Array(v) => Some(v),
            _ => None,
        }
    }

    /// Returns the value as a nested object
    pub fn to_nested(&self) -> Option<&Value> {
        match self {
            ValueType::Nested(v) => Some(v),
            _ => None,
        }
    }
}
impl From<&str> for ValueType {
    fn from(s: &str) -> Self {
        ValueType::Primitive(PrimitiveValue::String(s.to_string()))
    }
}
impl From<String> for ValueType {
    fn from(s: String) -> Self {
        ValueType::Primitive(PrimitiveValue::String(s))
    }
}
impl From<u64> for ValueType {
    fn from(v: u64) -> Self {
        ValueType::Primitive(PrimitiveValue::U64(v))
    }
}
impl From<i64> for ValueType {
    fn from(v: i64) -> Self {
        ValueType::Primitive(PrimitiveValue::I64(v))
    }
}
impl From<f32> for ValueType {
    fn from(v: f32) -> Self {
        ValueType::Primitive(PrimitiveValue::F32(v))
    }
}
impl From<f64> for ValueType {
    fn from(v: f64) -> Self {
        ValueType::Primitive(PrimitiveValue::F64(v))
    }
}
impl From<bool> for ValueType {
    fn from(v: bool) -> Self {
        ValueType::Primitive(PrimitiveValue::Bool(v))
    }
}
impl From<()> for ValueType {
    fn from(_: ()) -> Self {
        ValueType::Primitive(PrimitiveValue::Null)
    }
}
impl<T> From<Vec<T>> for ValueType
where
    T: Into<ValuePrimitiveType>,
{
    fn from(v: Vec<T>) -> Self {
        ValueType::Array(v.into_iter().map(Into::into).collect())
    }
}
impl From<Value> for ValueType {
    fn from(v: Value) -> Self {
        ValueType::Nested(v)
    }
}
impl<T> From<Option<T>> for ValueType
where
    T: Into<ValueType>,
{
    fn from(v: Option<T>) -> Self {
        match v {
            Some(v) => v.into(),
            None => ValueType::Primitive(PrimitiveValue::Null),
        }
    }
}

#[cfg(test)]
mod tests {
    use alloc::vec;

    use crate::{MValue, MValueCompatible, VectorPoint};

    use super::*;

    #[test]
    fn value_default() {
        let default = ValueType::default();
        assert_eq!(default, ValueType::Primitive(PrimitiveValue::Null));
    }

    #[test]
    fn primitive_value_funcs() {
        // &str
        let prim_value: PrimitiveValue = "test".into();
        assert_eq!(PrimitiveValue::String("test".into()), prim_value);
        assert_eq!(prim_value.to_u64(), None);
        assert_eq!(prim_value.to_i64(), None);
        assert_eq!(prim_value.to_f32(), None);
        assert_eq!(prim_value.to_f64(), None);
        assert_eq!(prim_value.to_bool(), None);
        assert!(!prim_value.is_null());
        // String
        let prim_value_str: String = "test".into();
        let prim_value: PrimitiveValue = prim_value_str.clone().into();
        assert_eq!(PrimitiveValue::String("test".into()), prim_value);
        assert_eq!(prim_value.to_string(), Some("test".into()));
        // u64
        let prim_value: PrimitiveValue = 1_u64.into();
        assert_eq!(PrimitiveValue::U64(1), prim_value);
        assert_eq!(prim_value.to_string(), None);
        assert_eq!(prim_value.to_u64(), Some(1));
        assert_eq!(prim_value.to_i64(), Some(1));
        assert_eq!(prim_value.to_f32(), Some(1.0));
        assert_eq!(prim_value.to_f64(), Some(1.0));
        // i64
        let prim_value: PrimitiveValue = (-1_i64).into();
        assert_eq!(PrimitiveValue::I64(-1), prim_value);
        assert_eq!(prim_value.to_u64(), Some(18446744073709551615));
        assert_eq!(prim_value.to_i64(), Some(-1));
        assert_eq!(prim_value.to_f32(), Some(-1.0));
        assert_eq!(prim_value.to_f64(), Some(-1.0));
        // f32
        let prim_value: PrimitiveValue = (1.0_f32).into();
        assert_eq!(PrimitiveValue::F32(1.0), prim_value);
        assert_eq!(prim_value.to_u64(), Some(1));
        assert_eq!(prim_value.to_i64(), Some(1));
        assert_eq!(prim_value.to_f32(), Some(1.0));
        assert_eq!(prim_value.to_f64(), Some(1.0));
        // f64
        let prim_value: PrimitiveValue = (1.0_f64).into();
        assert_eq!(PrimitiveValue::F64(1.0), prim_value);
        assert_eq!(prim_value.to_u64(), Some(1));
        assert_eq!(prim_value.to_i64(), Some(1));
        assert_eq!(prim_value.to_f32(), Some(1.0));
        assert_eq!(prim_value.to_f64(), Some(1.0));
        // bool
        let prim_value: PrimitiveValue = true.into();
        assert_eq!(PrimitiveValue::Bool(true), prim_value);
        assert_eq!(prim_value.to_bool(), Some(true));
        // ()
        let prim_value: PrimitiveValue = ().into();
        assert_eq!(PrimitiveValue::Null, prim_value);
        assert!(prim_value.is_null());
        // Option
        let prim_value: PrimitiveValue = Some(true).into();
        assert_eq!(PrimitiveValue::Bool(true), prim_value);
        assert_eq!(prim_value.to_bool(), Some(true));
        let prim_value: PrimitiveValue = None::<bool>.into();
        assert_eq!(PrimitiveValue::Null, prim_value);
        assert!(prim_value.is_null());
    }

    #[test]
    fn value_prim_type_funcs() {
        // &str
        let prim_value: ValuePrimitiveType = "test".into();
        assert_eq!(
            ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
            prim_value
        );
        assert_eq!(prim_value.to_prim(), Some(PrimitiveValue::String("test".into())).as_ref());
        assert_eq!(prim_value.to_nested(), None);
        // String
        let prim_value_str: String = "test".into();
        let prim_value: ValuePrimitiveType = prim_value_str.clone().into();
        assert_eq!(
            ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
            prim_value
        );
        // u64
        let prim_value: ValuePrimitiveType = 1_u64.into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::U64(1)), prim_value);
        // i64
        let prim_value: ValuePrimitiveType = (-1_i64).into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::I64(-1)), prim_value);
        // f32
        let prim_value: ValuePrimitiveType = (1.0_f32).into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::F32(1.0)), prim_value);
        // f64
        let prim_value: ValuePrimitiveType = (1.0_f64).into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::F64(1.0)), prim_value);
        // bool
        let prim_value: ValuePrimitiveType = true.into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Bool(true)), prim_value);
        // ()
        let prim_value: ValuePrimitiveType = ().into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Null), prim_value);

        // from prim
        let nested: ValuePrimitiveType = PrimitiveValue::Bool(true).into();
        assert_eq!(nested.to_prim().unwrap().to_bool(), Some(true));

        // nested
        let nested: ValuePrimitiveType =
            ValuePrimitive::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into())]).into();
        assert_eq!(nested.to_prim(), None);
        assert_eq!(
            nested.to_nested(),
            Some(ValuePrimitive::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),]))
                .as_ref()
        );

        // option
        let prim_value: ValuePrimitiveType = Some(true).into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Bool(true)), prim_value);
        let prim_value: ValuePrimitiveType = None::<bool>.into();
        assert_eq!(ValuePrimitiveType::Primitive(PrimitiveValue::Null), prim_value);
    }

    #[test]
    fn value_funcs() {
        // &str
        let prim_value: ValueType = "test".into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::String("test".into())), prim_value);
        let prim = prim_value.to_prim().unwrap();
        assert_eq!(*prim, PrimitiveValue::String("test".into()));
        assert_eq!(prim_value.to_nested(), None);
        assert_eq!(prim_value.to_vec(), None);
        // String
        let prim_value_str: String = "test".into();
        let prim_value: ValueType = prim_value_str.into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::String("test".into())), prim_value);
        // u64
        let prim_value: ValueType = 1_u64.into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::U64(1)), prim_value);
        // i64
        let prim_value: ValueType = (-1_i64).into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::I64(-1)), prim_value);
        // f32
        let prim_value: ValueType = (1.0_f32).into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::F32(1.0)), prim_value);
        // f64
        let prim_value: ValueType = (1.0_f64).into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::F64(1.0)), prim_value);
        // bool
        let prim_value: ValueType = true.into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::Bool(true)), prim_value);
        // ()
        let prim_value: ValueType = ().into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::Null), prim_value);

        // vec
        let prim_value: ValueType = vec!["test", "test2"].into();
        assert_eq!(prim_value.to_prim(), None);
        assert_eq!(
            ValueType::Array(vec![
                ValuePrimitiveType::Primitive(PrimitiveValue::String("test".into())),
                ValuePrimitiveType::Primitive(PrimitiveValue::String("test2".into())),
            ]),
            prim_value
        );
        let back_to_vec: Vec<String> =
            prim_value.to_vec().unwrap().iter().filter_map(|v| v.to_prim()?.to_string()).collect();
        assert_eq!(back_to_vec, vec!["test", "test2"]);

        // nested
        let nested: ValueType =
            Value::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into())]).into();
        assert_eq!(nested.to_vec(), None);
        assert_eq!(
            nested.to_nested(),
            Some(Value::from([("a".into(), "b".into()), ("c".into(), 2.0_f32.into()),])).as_ref()
        );

        // option
        let prim_value: ValueType = Some(true).into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::Bool(true)), prim_value);
        let prim_value: ValueType = None::<bool>.into();
        assert_eq!(ValueType::Primitive(PrimitiveValue::Null), prim_value);
    }

    #[test]
    fn test_rgba_struct() {
        #[derive(Debug, Clone, Copy, PartialEq)]
        pub struct Rgba {
            /// Gamma corrected Red between 0 and 1
            pub r: f64,
            /// Gamma corrected Green between 0 and 1
            pub g: f64,
            /// Gamma corrected Blue between 0 and 1
            pub b: f64,
            /// Opacity between 0 and 1 (not gamma corrected as opacity is linear)
            pub a: f64,
        }
        impl Rgba {
            /// Create a new RGBA value
            pub fn new(r: f64, g: f64, b: f64, a: f64) -> Self {
                Self { r, g, b, a }
            }
        }
        impl MValueCompatible for Rgba {}
        impl From<Rgba> for MValue {
            fn from(rgba: Rgba) -> MValue {
                MValue::from([
                    ("r".into(), (rgba.r).into()),
                    ("g".into(), (rgba.g).into()),
                    ("b".into(), (rgba.b).into()),
                    ("a".into(), (rgba.a).into()),
                ])
            }
        }
        impl From<MValue> for Rgba {
            fn from(mvalue: MValue) -> Self {
                let r: f64 = mvalue.get("r").unwrap().to_prim().unwrap().to_f64().unwrap();
                let g = mvalue.get("g").unwrap().to_prim().unwrap().to_f64().unwrap();
                let b = mvalue.get("b").unwrap().to_prim().unwrap().to_f64().unwrap();
                let a = mvalue.get("a").unwrap().to_prim().unwrap().to_f64().unwrap();
                Rgba::new(r, g, b, a)
            }
        }

        let rgba = Rgba::new(0.1, 0.2, 0.3, 0.4);
        let rgba_mvalue: MValue = rgba.into();
        assert_eq!(
            rgba_mvalue,
            MValue::from([
                ("r".into(), ValueType::Primitive(PrimitiveValue::F64(0.1))),
                ("g".into(), ValueType::Primitive(PrimitiveValue::F64(0.2))),
                ("b".into(), ValueType::Primitive(PrimitiveValue::F64(0.3))),
                ("a".into(), ValueType::Primitive(PrimitiveValue::F64(0.4))),
            ])
        );
        let back_to_rgba: Rgba = rgba_mvalue.clone().into();
        assert_eq!(rgba, back_to_rgba);

        let vp: VectorPoint<Rgba> =
            VectorPoint { x: 1.0, y: 2.0, z: Some(3.0), m: Some(rgba), t: None };
        let vp_mvalue: MValue = vp.m.unwrap().into();
        assert_eq!(vp_mvalue, rgba_mvalue);
    }
}