signalk 0.7.0

A library to parse signalk maritime data
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
use crate::definitions::V2NumberValue::Int;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
pub struct V1CommonValueFields {
    pub timestamp: String,
    #[serde(rename = "$source")]
    pub source: String,
    #[serde(rename = "_attr")]
    pub attr: Option<V1Attr>,
    pub meta: Option<V1Meta>,
    pub pgn: Option<i64>,
    pub sentence: Option<String>,
}

impl V1CommonValueFields {
    pub fn builder() -> V1CommonValueFieldsBuilder {
        V1CommonValueFieldsBuilder::default()
    }
}

#[derive(Default)]
pub struct V1CommonValueFieldsBuilder {
    timestamp: String,
    source: String,
    attr: Option<V1Attr>,
    meta: Option<V1Meta>,
    pgn: Option<i64>,
    sentence: Option<String>,
}

impl V1CommonValueFieldsBuilder {
    pub fn timestamp(mut self, value: String) -> V1CommonValueFieldsBuilder {
        self.timestamp = value;
        self
    }
    pub fn source(mut self, value: String) -> V1CommonValueFieldsBuilder {
        self.source = value;
        self
    }
    pub fn attr(mut self, value: V1Attr) -> V1CommonValueFieldsBuilder {
        self.attr = Some(value);
        self
    }
    pub fn meta(mut self, value: V1Meta) -> V1CommonValueFieldsBuilder {
        self.meta = Some(value);
        self
    }
    pub fn pgn(mut self, value: i64) -> V1CommonValueFieldsBuilder {
        self.pgn = Some(value);
        self
    }
    pub fn sentence(mut self, value: String) -> V1CommonValueFieldsBuilder {
        self.sentence = Some(value);
        self
    }
    pub fn build(self) -> V1CommonValueFields {
        V1CommonValueFields {
            timestamp: self.timestamp,
            source: self.source,
            attr: self.attr,
            meta: self.meta,
            pgn: self.pgn,
            sentence: self.sentence,
        }
    }
}

pub trait F64Compatible {
    fn as_f64(&self) -> Option<f64>;
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
#[serde(untagged)]
pub enum V2NumberValue {
    ExpandedFloat(V2NumberExpandedFloatValue),
    Float(f64),
    Int(i64),
}

impl V2NumberValue {
    pub fn from_value(value: &Value) -> Option<Self> {
        if value.is_null() {
            None
        } else {
            let ship_type_result: Result<Self, serde_json::Error> =
                serde_json::from_value(value.clone());
            if let Ok(ship_type_value) = ship_type_result {
                Some(ship_type_value)
            } else {
                None
            }
        }
    }
}
impl F64Compatible for V2NumberValue {
    fn as_f64(&self) -> Option<f64> {
        match self {
            V2NumberValue::ExpandedFloat(v) => v.value,
            V2NumberValue::Float(v) => Some(*v),
            Int(v) => Some(*v as f64),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
pub struct V2NumberExpandedFloatValue {
    pub value: Option<f64>,
    #[serde(flatten)]
    pub common_value_fields: Option<V1CommonValueFields>,
}

impl Default for V2NumberValue {
    fn default() -> Self {
        Int(i64::default())
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
pub struct V1NumberValue {
    pub value: Option<f64>,
    pub timestamp: String,
    #[serde(rename = "$source")]
    pub source: String,
    pub pgn: Option<f64>,
    pub sentence: Option<String>,
    // TODO: Add support for meta values
}

impl V1NumberValue {
    pub fn builder() -> V1NumberValueBuilder {
        V1NumberValueBuilder::default()
    }

    pub fn from_value(value: &Value) -> Option<V1NumberValue> {
        if value.is_null() {
            None
        } else {
            let type_result: Result<V1NumberValue, serde_json::Error> =
                serde_json::from_value(value.clone());
            if let Ok(type_value) = type_result {
                Some(type_value)
            } else {
                None
            }
        }
    }
}

impl F64Compatible for V1NumberValue {
    fn as_f64(&self) -> Option<f64> {
        self.value
    }
}

#[derive(Default)]
pub struct V1NumberValueBuilder {
    pub value: Option<f64>,
    pub timestamp: String,
    pub source: String,
    pub pgn: Option<f64>,
    pub sentence: Option<String>,
}

impl V1NumberValueBuilder {
    pub fn json_value(mut self, value: &serde_json::Value) -> V1NumberValueBuilder {
        if let Some(f64_value) = value.as_f64() {
            self.value = Some(f64_value);
        } else {
            self.value = None;
        }
        self
    }
    pub fn value(mut self, value: f64) -> V1NumberValueBuilder {
        self.value = Some(value);
        self
    }
    pub fn timestamp(mut self, timestamp: String) -> V1NumberValueBuilder {
        self.timestamp = timestamp;
        self
    }
    pub fn source(mut self, source: String) -> V1NumberValueBuilder {
        self.source = source;
        self
    }
    pub fn pgn(mut self, pgn: f64) -> V1NumberValueBuilder {
        self.pgn = Some(pgn);
        self
    }
    pub fn sentence(mut self, sentence: String) -> V1NumberValueBuilder {
        self.sentence = Some(sentence);
        self
    }
    pub fn build(self) -> V1NumberValue {
        V1NumberValue {
            value: self.value,
            timestamp: self.timestamp,
            source: self.source,
            pgn: self.pgn,
            sentence: self.sentence,
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct V1Meta {
    pub description: String,
    pub display_name: Option<String>,
    pub long_name: Option<String>,
    pub short_name: Option<String>,
    #[serde(rename = "enum")]
    pub enum_: Option<Vec<String>>,
    pub properties: Option<V1MetaProperties>,
    pub gauge_type: Option<String>,
    pub display_scale: Option<V1MetaDisplayScale>,
    pub units: Option<String>,
    pub timeout: Option<f64>,
    pub alert_method: Option<Vec<String>>,
    pub warn_method: Option<Vec<String>>,
    pub alarm_method: Option<Vec<String>>,
    pub emergency_method: Option<Vec<String>>,
    pub zones: Option<Vec<V1MetaZone>>,
}

impl V1Meta {
    pub fn builder() -> V1MetaBuilder {
        V1MetaBuilder::default()
    }
}

#[derive(Default)]
pub struct V1MetaBuilder {
    description: String,
    display_name: Option<String>,
    long_name: Option<String>,
    short_name: Option<String>,
    enum_: Option<Vec<String>>,
    properties: Option<V1MetaProperties>,
    gauge_type: Option<String>,
    display_scale: Option<V1MetaDisplayScale>,
    units: Option<String>,
    timeout: Option<f64>,
    alert_method: Option<Vec<String>>,
    warn_method: Option<Vec<String>>,
    alarm_method: Option<Vec<String>>,
    emergency_method: Option<Vec<String>>,
    zones: Option<Vec<V1MetaZone>>,
}

impl V1MetaBuilder {
    pub fn description(mut self, value: String) -> V1MetaBuilder {
        self.description = value;
        self
    }
    pub fn display_name(mut self, value: String) -> V1MetaBuilder {
        self.display_name = Some(value);
        self
    }
    pub fn long_name(mut self, value: String) -> V1MetaBuilder {
        self.long_name = Some(value);
        self
    }
    pub fn short_name(mut self, value: String) -> V1MetaBuilder {
        self.short_name = Some(value);
        self
    }
    pub fn enum_(mut self, value: String) -> V1MetaBuilder {
        if self.enum_.is_none() {
            self.enum_ = Some(Vec::new());
        }
        if let Some(ref mut x) = self.enum_ {
            x.push(value);
        }
        self
    }
    pub fn properties(mut self, value: V1MetaProperties) -> V1MetaBuilder {
        self.properties = Some(value);
        self
    }
    pub fn gauge_type(mut self, value: String) -> V1MetaBuilder {
        self.gauge_type = Some(value);
        self
    }
    pub fn display_scale(mut self, value: V1MetaDisplayScale) -> V1MetaBuilder {
        self.display_scale = Some(value);
        self
    }
    pub fn units(mut self, value: String) -> V1MetaBuilder {
        self.units = Some(value);
        self
    }
    pub fn timeout(mut self, value: f64) -> V1MetaBuilder {
        self.timeout = Some(value);
        self
    }
    pub fn alert_method(mut self, value: String) -> V1MetaBuilder {
        if self.alert_method.is_none() {
            self.alert_method = Some(Vec::new());
        }
        if let Some(ref mut x) = self.alert_method {
            x.push(value);
        }
        self
    }
    pub fn warn_method(mut self, value: String) -> V1MetaBuilder {
        if self.warn_method.is_none() {
            self.warn_method = Some(Vec::new());
        }
        if let Some(ref mut x) = self.warn_method {
            x.push(value);
        }
        self
    }
    pub fn alarm_method(mut self, value: String) -> V1MetaBuilder {
        if self.alarm_method.is_none() {
            self.alarm_method = Some(Vec::new());
        }
        if let Some(ref mut x) = self.alarm_method {
            x.push(value);
        }
        self
    }
    pub fn emergency_method(mut self, value: String) -> V1MetaBuilder {
        if self.emergency_method.is_none() {
            self.emergency_method = Some(Vec::new());
        }
        if let Some(ref mut x) = self.emergency_method {
            x.push(value);
        }
        self
    }
    pub fn zones(mut self, value: V1MetaZone) -> V1MetaBuilder {
        if self.zones.is_none() {
            self.zones = Some(Vec::new());
        }
        if let Some(ref mut x) = self.zones {
            x.push(value);
        }
        self
    }
    pub fn build(self) -> V1Meta {
        V1Meta {
            description: self.description,
            display_name: self.display_name,
            long_name: self.long_name,
            short_name: self.short_name,
            enum_: self.enum_,
            properties: self.properties,
            gauge_type: self.gauge_type,
            display_scale: self.display_scale,
            units: self.units,
            timeout: self.timeout,
            alert_method: self.alert_method,
            warn_method: self.warn_method,
            alarm_method: self.alarm_method,
            emergency_method: self.emergency_method,
            zones: self.zones,
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct V1MetaProperties {
    pub properties: Option<String>,
    pub title: Option<String>,
    pub description: Option<String>,
    pub units: Option<String>,
    pub example: Option<Value>,
}

impl V1MetaProperties {
    pub fn builder() -> V1MetaPropertiesBuilder {
        V1MetaPropertiesBuilder::default()
    }
}

#[derive(Default)]
pub struct V1MetaPropertiesBuilder {
    properties: Option<String>,
    title: Option<String>,
    description: Option<String>,
    units: Option<String>,
    example: Option<Value>,
}

impl V1MetaPropertiesBuilder {
    pub fn properties(mut self, value: String) -> V1MetaPropertiesBuilder {
        self.properties = Some(value);
        self
    }
    pub fn title(mut self, value: String) -> V1MetaPropertiesBuilder {
        self.title = Some(value);
        self
    }
    pub fn description(mut self, value: String) -> V1MetaPropertiesBuilder {
        self.description = Some(value);
        self
    }
    pub fn units(mut self, value: String) -> V1MetaPropertiesBuilder {
        self.units = Some(value);
        self
    }
    pub fn example(mut self, value: Value) -> V1MetaPropertiesBuilder {
        self.example = Some(value);
        self
    }

    pub fn build(self) -> V1MetaProperties {
        V1MetaProperties {
            properties: self.properties,
            title: self.title,
            description: self.description,
            units: self.units,
            example: self.example,
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct V1MetaDisplayScale {
    pub lower: f64,
    pub upper: f64,
    #[serde(rename = "type")]
    pub type_: Option<String>,
    pub power: Option<f64>,
}

impl V1MetaDisplayScale {
    pub fn builder() -> V1MetaDisplayScaleBuilder {
        V1MetaDisplayScaleBuilder::default()
    }
}

#[derive(Default)]
pub struct V1MetaDisplayScaleBuilder {
    lower: f64,
    upper: f64,
    type_: Option<String>,
    power: Option<f64>,
}

impl V1MetaDisplayScaleBuilder {
    pub fn lower(mut self, value: f64) -> V1MetaDisplayScaleBuilder {
        self.lower = value;
        self
    }
    pub fn upper(mut self, value: f64) -> V1MetaDisplayScaleBuilder {
        self.upper = value;
        self
    }
    pub fn type_(mut self, value: String) -> V1MetaDisplayScaleBuilder {
        self.type_ = Some(value);
        self
    }
    pub fn power(mut self, value: f64) -> V1MetaDisplayScaleBuilder {
        self.power = Some(value);
        self
    }

    pub fn build(self) -> V1MetaDisplayScale {
        V1MetaDisplayScale {
            lower: self.lower,
            upper: self.upper,
            type_: self.type_,
            power: self.power,
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct V1MetaZone {
    pub lower: Option<f64>,
    pub upper: Option<f64>,
    pub state: String,
    pub message: Option<String>,
}

impl V1MetaZone {
    pub fn builder() -> V1MetaZoneBuilder {
        V1MetaZoneBuilder::default()
    }
}

#[derive(Default)]
pub struct V1MetaZoneBuilder {
    lower: Option<f64>,
    upper: Option<f64>,
    state: String,
    message: Option<String>,
}

impl V1MetaZoneBuilder {
    pub fn lower(mut self, value: f64) -> V1MetaZoneBuilder {
        self.lower = Some(value);
        self
    }
    pub fn upper(mut self, value: f64) -> V1MetaZoneBuilder {
        self.upper = Some(value);
        self
    }
    pub fn state(mut self, value: String) -> V1MetaZoneBuilder {
        self.state = value;
        self
    }
    pub fn message(mut self, value: String) -> V1MetaZoneBuilder {
        self.message = Some(value);
        self
    }

    pub fn build(self) -> V1MetaZone {
        V1MetaZone {
            lower: self.lower,
            upper: self.upper,
            state: self.state,
            message: self.message,
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct V1Attr {
    #[serde(rename = "_mode")]
    pub mode: Option<i64>,
    #[serde(rename = "_owner")]
    pub owner: Option<String>,
    #[serde(rename = "_group")]
    pub group: Option<String>,
}

impl Default for V1Attr {
    fn default() -> Self {
        V1Attr {
            mode: Some(644),
            owner: Some("self".into()),
            group: Some("self".into()),
        }
    }
}

impl V1Attr {
    pub fn new(mode: i64, owner: String, group: String) -> V1Attr {
        V1Attr {
            mode: Some(mode),
            owner: Some(owner),
            group: Some(group),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct V1DefSource {
    pub label: String,
    #[serde(rename = "type")]
    pub type_: Option<String>,
    pub src: Option<String>,
    pub can_name: Option<String>,
    pub pgn: Option<i32>,
    pub instance: Option<String>,
    pub sentence: Option<String>,
    pub talker: Option<String>,
    pub ais_type: Option<i32>,
}

impl V1DefSource {
    pub fn builder() -> V1DefSourceBuilder {
        V1DefSourceBuilder::default()
    }
}

#[derive(Default)]
pub struct V1DefSourceBuilder {
    label: String,
    type_: Option<String>,
    src: Option<String>,
    can_name: Option<String>,
    pgn: Option<i32>,
    instance: Option<String>,
    sentence: Option<String>,
    talker: Option<String>,
    ais_type: Option<i32>,
}

impl V1DefSourceBuilder {
    pub fn label(mut self, value: String) -> V1DefSourceBuilder {
        self.label = value;
        self
    }
    pub fn type_(mut self, value: String) -> V1DefSourceBuilder {
        self.type_ = Some(value);
        self
    }
    pub fn src(mut self, value: String) -> V1DefSourceBuilder {
        self.src = Some(value);
        self
    }
    pub fn can_name(mut self, value: String) -> V1DefSourceBuilder {
        self.can_name = Some(value);
        self
    }
    pub fn pgn(mut self, value: i32) -> V1DefSourceBuilder {
        self.pgn = Some(value);
        self
    }
    pub fn instance(mut self, value: String) -> V1DefSourceBuilder {
        self.instance = Some(value);
        self
    }
    pub fn sentence(mut self, value: String) -> V1DefSourceBuilder {
        self.sentence = Some(value);
        self
    }
    pub fn talker(mut self, value: String) -> V1DefSourceBuilder {
        self.talker = Some(value);
        self
    }
    pub fn ais_type(mut self, value: i32) -> V1DefSourceBuilder {
        self.ais_type = Some(value);
        self
    }
    pub fn build(self) -> V1DefSource {
        V1DefSource {
            label: self.label,
            type_: self.type_,
            src: self.src,
            can_name: self.can_name,
            pgn: self.pgn,
            instance: self.instance,
            sentence: self.sentence,
            talker: self.talker,
            ais_type: self.ais_type,
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
#[serde(untagged)]
pub enum V1DateTime {
    #[default]
    None,
    String(String),
    Object(V1DateTimeValue),
}

impl V1DateTime {
    pub fn builder() -> V1DateTimeBuilder {
        V1DateTimeBuilder::default()
    }

    pub fn from_value(value: &Value) -> Option<V1DateTime> {
        if value.is_null() {
            None
        } else {
            let type_result: Result<V1DateTime, serde_json::Error> =
                serde_json::from_value(value.clone());
            if let Ok(type_value) = type_result {
                Some(type_value)
            } else {
                None
            }
        }
    }

    #[allow(clippy::manual_unwrap_or_default)]
    fn get_value(&self) -> &str {
        match self {
            V1DateTime::None => "",
            V1DateTime::String(val) => val,
            V1DateTime::Object(ref o) => match o.value {
                None => "",
                Some(ref value) => value,
            },
        }
    }
    pub fn get_hour(&self) -> u8 {
        let value = self.get_value();
        let time = OffsetDateTime::parse(value, &Rfc3339);
        if let Ok(time) = time {
            time.hour()
        } else {
            0
        }
    }
    pub fn get_minute(&self) -> u8 {
        let value = self.get_value();
        let time = OffsetDateTime::parse(value, &Rfc3339);
        if let Ok(time) = time {
            time.minute()
        } else {
            0
        }
    }
    pub fn get_second(&self) -> u8 {
        let value = self.get_value();
        let time = OffsetDateTime::parse(value, &Rfc3339);
        if let Ok(time) = time {
            time.second()
        } else {
            0
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
pub struct V1DateTimeValue {
    value: Option<String>,
    timestamp: Option<String>,
    pgn: Option<i64>,
    sentence: Option<String>,
}

#[derive(Default)]
pub struct V1DateTimeBuilder {
    value: Option<String>,
    _object: Option<V1DateTimeValue>,
}

impl V1DateTimeBuilder {
    pub fn timestamp(mut self, value: String) -> V1DateTimeBuilder {
        self.value = Some(value);
        self
    }

    pub fn build(self) -> V1DateTime {
        V1DateTime::None
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
pub struct V1Timestamp {
    #[serde(flatten)]
    pub value: Option<String>,
}

impl V1Timestamp {
    pub fn builder() -> V1TimestampBuilder {
        V1TimestampBuilder::default()
    }
}

#[derive(Default)]
pub struct V1TimestampBuilder {
    pub value: Option<String>,
}

impl V1TimestampBuilder {
    pub fn value(mut self, value: String) -> V1TimestampBuilder {
        self.value = Some(value);
        self
    }
    pub fn build(self) -> V1Timestamp {
        V1Timestamp { value: self.value }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
#[serde(untagged)]
pub enum V1StringValue {
    Expanded(V1StringExpandedValue),
    Value(String),
}

impl Default for V1StringValue {
    fn default() -> Self {
        V1StringValue::Value(String::default())
    }
}

impl V1StringValue {
    pub fn builder() -> V1StringValueBuilder {
        V1StringValueBuilder::default()
    }

    pub fn from_value(value: &Value) -> Option<V1StringValue> {
        if value.is_null() {
            None
        } else {
            let type_result: Result<V1StringValue, serde_json::Error> =
                serde_json::from_value(value.clone());
            if let Ok(type_value) = type_result {
                Some(type_value)
            } else {
                None
            }
        }
    }
}

#[derive(Default)]
pub struct V1StringValueBuilder {
    value: Option<String>,
    common_value_fields: Option<V1CommonValueFields>,
}
impl V1StringValueBuilder {
    pub fn value(mut self, value: String) -> V1StringValueBuilder {
        self.value = Some(value);
        self
    }
    pub fn common_value_fields(mut self, value: V1CommonValueFields) -> V1StringValueBuilder {
        self.common_value_fields = Some(value);
        self
    }

    pub fn build(self) -> V1StringValue {
        if let Some(ref _value) = self.common_value_fields {
            V1StringValue::Expanded(V1StringExpandedValue {
                value: self.value,
                common_value_fields: self.common_value_fields,
            })
        } else if let Some(value) = self.value {
            V1StringValue::Value(value.clone())
        } else {
            V1StringValue::Value("".to_string())
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
pub struct V1StringExpandedValue {
    pub value: Option<String>,
    #[serde(flatten)]
    pub common_value_fields: Option<V1CommonValueFields>,
}

#[cfg(test)]
mod tests {
    use crate::definitions::{V1DateTime, V1DateTimeValue};

    #[test]
    fn datetime_object_json() {
        let j = r#"
        {
          "value": "2015-12-05T13:11:59Z",
          "gnssTimeSource": "GPS",
          "timestamp": "2014-08-15T19:00:15.123456789Z",
          "$source": "foo.bar"
        }"#;
        let datetime: V1DateTime = serde_json::from_str(j).unwrap();
        assert_eq!(datetime.get_value(), "2015-12-05T13:11:59Z")
    }

    #[test]
    fn datetime_value_object_json() {
        let j = r#"
        {
          "value": "2015-12-05T13:11:59Z",
          "gnssTimeSource": "GPS",
          "timestamp": "2014-08-15T19:00:15.123456789Z",
          "$source": "foo.bar"
        }"#;
        let datetime: V1DateTimeValue = serde_json::from_str(j).unwrap();
        assert_eq!(datetime.value.unwrap(), "2015-12-05T13:11:59Z")
    }
    #[test]
    fn datetime_value_null_value_object_() {
        let j = r#"
        {
          "value": null,
          "gnssTimeSource": "GPS",
          "timestamp": "2014-08-15T19:00:15.123456789Z",
          "$source": "foo.bar"
        }"#;
        let datetime: V1DateTimeValue = serde_json::from_str(j).unwrap();
        assert_eq!(datetime.value, None)
    }

    #[test]
    fn datetime_string_json() {
        let j = r#""2014-08-15T19:05:29.57200Z""#;
        let datetime: V1DateTime = serde_json::from_str(j).unwrap();
        assert_eq!(datetime.get_value(), "2014-08-15T19:05:29.57200Z")
    }
    #[test]
    fn datetime_string_2022_json() {
        let j = r#""2022-04-22T05:02:56.484Z""#;
        let datetime: V1DateTime = serde_json::from_str(j).unwrap();
        assert_eq!(datetime.get_value(), "2022-04-22T05:02:56.484Z")
    }

    #[test]
    fn date_time_creation() {
        let date_time = V1DateTime::String("2015-12-05T13:11:59Z".into());
        println!("{:?}", date_time);
        assert!(matches!(date_time, V1DateTime::String(_)));
    }
}