cinnamon 2.0.0-alpha.1

A type-safe, async Nightscout client for Rust: API v3 with v1/v2 fallback, JWT auth, incremental sync and realtime streams.
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
//! `GET /api/v2/properties`: the live state Nightscout's plugins compute (IOB, COB,
//! delta, device ages, pump and loop status, …).
//!
//! A property is only present when its plugin is enabled on the server (`ENABLE=iob cob …`).
//! Every property is decoded independently and tolerantly: a plugin changing its output
//! shape leaves that one property `None` (the raw JSON stays in [`Properties::extra`]).

use std::fmt;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::{Extra, Timestamp, de, time};

/// Names of the properties Nightscout can compute.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Property {
    /// Latest glucose and the 5-minute buckets it is computed from.
    BgNow,
    /// Glucose delta.
    Delta,
    /// 5-minute glucose buckets.
    Buckets,
    /// Trend arrow.
    Direction,
    /// Raw BG from unfiltered sensor data.
    RawBg,
    /// Uploader battery.
    Upbat,
    /// AR2 forecast.
    Ar2,
    /// Insulin on board.
    Iob,
    /// Carbs on board.
    Cob,
    /// Pump status.
    Pump,
    /// OpenAPS / AAPS status.
    OpenAps,
    /// Loop status.
    Loop,
    /// Bolus wizard preview.
    Bwp,
    /// Cannula age.
    Cage,
    /// Sensor age.
    Sage,
    /// Insulin (reservoir) age.
    Iage,
    /// Pump battery age.
    Bage,
    /// Current basal.
    Basal,
    /// Database size.
    DbSize,
    /// Server runtime state.
    RuntimeState,
    /// Any other plugin property.
    Custom(String),
}

impl Property {
    /// The property key used in URLs and responses.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::BgNow => "bgnow",
            Self::Delta => "delta",
            Self::Buckets => "buckets",
            Self::Direction => "direction",
            Self::RawBg => "rawbg",
            Self::Upbat => "upbat",
            Self::Ar2 => "ar2",
            Self::Iob => "iob",
            Self::Cob => "cob",
            Self::Pump => "pump",
            Self::OpenAps => "openaps",
            Self::Loop => "loop",
            Self::Bwp => "bwp",
            Self::Cage => "cage",
            Self::Sage => "sage",
            Self::Iage => "iage",
            Self::Bage => "bage",
            Self::Basal => "basal",
            Self::DbSize => "dbsize",
            Self::RuntimeState => "runtimestate",
            Self::Custom(s) => s,
        }
    }
}

impl fmt::Display for Property {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Everything `/api/v2/properties` returned.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Properties {
    /// Latest glucose.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub bgnow: Option<BgNow>,
    /// Glucose delta.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub delta: Option<Delta>,
    /// 5-minute buckets.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub buckets: Option<Vec<Bucket>>,
    /// Trend arrow.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub direction: Option<DirectionProperty>,
    /// Raw BG.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub rawbg: Option<RawBg>,
    /// Uploader battery.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub upbat: Option<Upbat>,
    /// AR2 forecast.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub ar2: Option<Ar2>,
    /// Insulin on board.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub iob: Option<Iob>,
    /// Carbs on board.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub cob: Option<Cob>,
    /// Pump status (the latest devicestatus with pump data, plus a display summary).
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub pump: Option<PumpProperty>,
    /// OpenAPS status.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub openaps: Option<LoopProperty>,
    /// Loop status.
    #[serde(
        rename = "loop",
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub loop_status: Option<LoopProperty>,
    /// Bolus wizard preview.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub bwp: Option<Bwp>,
    /// Cannula age.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub cage: Option<DeviceAge>,
    /// Sensor age (`Sensor Start` and `Sensor Change` tracked separately).
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub sage: Option<SensorAge>,
    /// Insulin age.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub iage: Option<DeviceAge>,
    /// Pump battery age.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub bage: Option<DeviceAge>,
    /// Current basal.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub basal: Option<Basal>,
    /// Database size.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub dbsize: Option<DbSize>,
    /// Runtime state.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "de::tolerant"
    )]
    pub runtimestate: Option<RuntimeState>,
    /// Any other property, and the raw JSON of properties that did not decode.
    #[serde(flatten)]
    pub extra: Extra,
}

/// A glucose sample inside properties (the server's in-memory `ddata` shape).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PropertySgv {
    /// Document id.
    #[serde(rename = "_id", default, with = "de::text")]
    pub id: Option<String>,
    /// Glucose in mg/dL.
    #[serde(default, with = "de::num")]
    pub mgdl: Option<f64>,
    /// Sample time.
    #[serde(default, with = "time::millis")]
    pub mills: Option<Timestamp>,
    /// Uploading device.
    #[serde(default, with = "de::text")]
    pub device: Option<String>,
    /// Trend arrow.
    #[serde(default, with = "de::text")]
    pub direction: Option<String>,
    /// Glucose in display units.
    #[serde(default)]
    pub scaled: Option<Value>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `bgnow`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct BgNow {
    /// Mean of the latest bucket (mg/dL).
    #[serde(default, with = "de::num")]
    pub mean: Option<f64>,
    /// Latest value (mg/dL).
    #[serde(default, with = "de::num")]
    pub last: Option<f64>,
    /// Time of the latest value.
    #[serde(default, with = "time::millis")]
    pub mills: Option<Timestamp>,
    /// Samples in the latest bucket.
    #[serde(default, deserialize_with = "de::null_as_empty")]
    pub sgvs: Vec<PropertySgv>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `delta`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Delta {
    /// Absolute change (mg/dL).
    #[serde(default, with = "de::num")]
    pub absolute: Option<f64>,
    /// Minutes between the compared buckets.
    #[serde(rename = "elapsedMins", default, with = "de::num")]
    pub elapsed_mins: Option<f64>,
    /// Whether the previous value was interpolated over a gap.
    #[serde(default, with = "de::flag")]
    pub interpolated: Option<bool>,
    /// Mean of the bucket 5 minutes earlier.
    #[serde(rename = "mean5MinsAgo", default, with = "de::num")]
    pub mean_5_mins_ago: Option<f64>,
    /// Delta normalized to 5 minutes, in mg/dL.
    #[serde(default, with = "de::num")]
    pub mgdl: Option<f64>,
    /// Delta in display units.
    #[serde(default, with = "de::num")]
    pub scaled: Option<f64>,
    /// Display string, e.g. `+3`.
    #[serde(default, with = "de::text")]
    pub display: Option<String>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// One 5-minute bucket.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Bucket {
    /// Bucket index (0 = newest).
    #[serde(default, with = "de::int")]
    pub index: Option<i64>,
    /// Bucket start.
    #[serde(rename = "fromMills", default, with = "time::millis")]
    pub from_mills: Option<Timestamp>,
    /// Bucket end.
    #[serde(rename = "toMills", default, with = "time::millis")]
    pub to_mills: Option<Timestamp>,
    /// Mean glucose in the bucket.
    #[serde(default, with = "de::num")]
    pub mean: Option<f64>,
    /// Newest glucose in the bucket.
    #[serde(default, with = "de::num")]
    pub last: Option<f64>,
    /// Newest sample time.
    #[serde(default, with = "time::millis")]
    pub mills: Option<Timestamp>,
    /// Samples.
    #[serde(default, deserialize_with = "de::null_as_empty")]
    pub sgvs: Vec<PropertySgv>,
    /// Whether the bucket has no samples.
    #[serde(rename = "isEmpty", default, with = "de::flag")]
    pub is_empty: Option<bool>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `direction`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DirectionProperty {
    /// Direction name, e.g. `Flat`.
    #[serde(default, with = "de::text")]
    pub value: Option<String>,
    /// Arrow character.
    #[serde(default, with = "de::text")]
    pub label: Option<String>,
    /// HTML entity of the arrow.
    #[serde(default, with = "de::text")]
    pub entity: Option<String>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `rawbg`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RawBg {
    /// Raw glucose in mg/dL.
    #[serde(default, with = "de::num")]
    pub mgdl: Option<f64>,
    /// Noise label (`Clean`, `Light`, …).
    #[serde(rename = "noiseLabel", default, with = "de::text")]
    pub noise_label: Option<String>,
    /// Display line.
    #[serde(rename = "displayLine", default, with = "de::text")]
    pub display_line: Option<String>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `upbat`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Upbat {
    /// Lowest battery level across devices.
    #[serde(default, with = "de::num")]
    pub level: Option<f64>,
    /// Display string, e.g. `58%`.
    #[serde(default, with = "de::text")]
    pub display: Option<String>,
    /// Alert status (`normal`, `warn`, `urgent`).
    #[serde(default, with = "de::text")]
    pub status: Option<String>,
    /// Per-device battery details.
    #[serde(default)]
    pub devices: Option<Value>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `ar2`: Nightscout's autoregressive forecast.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Ar2 {
    /// Forecast points and error.
    #[serde(default, deserialize_with = "de::tolerant")]
    pub forecast: Option<Ar2Forecast>,
    /// Alarm level raised by the forecast.
    #[serde(default, with = "de::int")]
    pub level: Option<i64>,
    /// Event name (`high`, `low`, …).
    #[serde(rename = "eventName", default, with = "de::text")]
    pub event_name: Option<String>,
    /// Display line.
    #[serde(rename = "displayLine", default, with = "de::text")]
    pub display_line: Option<String>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// AR2 forecast points.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Ar2Forecast {
    /// Predicted values.
    #[serde(default, deserialize_with = "de::null_as_empty")]
    pub predicted: Vec<ForecastPoint>,
    /// Average loss of the forecast (drives alarm levels).
    #[serde(rename = "avgLoss", default, with = "de::num")]
    pub avg_loss: Option<f64>,
}

/// A predicted glucose value.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ForecastPoint {
    /// Predicted time.
    #[serde(default, with = "time::millis")]
    pub mills: Option<Timestamp>,
    /// Predicted glucose in mg/dL.
    #[serde(default, with = "de::num")]
    pub mgdl: Option<f64>,
}

/// `iob`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Iob {
    /// Insulin on board in units.
    #[serde(default, with = "de::num")]
    pub iob: Option<f64>,
    /// IOB attributable to basal (loop sources).
    #[serde(default, with = "de::num")]
    pub basaliob: Option<f64>,
    /// Insulin activity.
    #[serde(default, with = "de::num")]
    pub activity: Option<f64>,
    /// Where the value comes from: `OpenAPS`, `Loop`, `MM Connect` or `Care Portal`.
    #[serde(default, with = "de::text")]
    pub source: Option<String>,
    /// Device that reported it.
    #[serde(default, with = "de::text")]
    pub device: Option<String>,
    /// When it was computed.
    #[serde(default, with = "time::millis")]
    pub mills: Option<Timestamp>,
    /// IOB computed from treatments, for comparison.
    #[serde(rename = "treatmentIob", default)]
    pub treatment_iob: Option<Value>,
    /// Last bolus (treatment source only).
    #[serde(rename = "lastBolus", default, deserialize_with = "de::tolerant")]
    pub last_bolus: Option<super::Treatment>,
    /// Display value.
    #[serde(default, with = "de::text")]
    pub display: Option<String>,
    /// Display line, e.g. `IOB: 1.25U`.
    #[serde(rename = "displayLine", default, with = "de::text")]
    pub display_line: Option<String>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `cob`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Cob {
    /// Carbs on board in grams.
    #[serde(default, with = "de::num")]
    pub cob: Option<f64>,
    /// Where the value comes from.
    #[serde(default, with = "de::text")]
    pub source: Option<String>,
    /// Device that reported it.
    #[serde(default, with = "de::text")]
    pub device: Option<String>,
    /// When it was computed.
    #[serde(default, with = "time::millis")]
    pub mills: Option<Timestamp>,
    /// Whether carbs are currently decaying (treatment source).
    #[serde(rename = "isDecaying", default, with = "de::flag")]
    pub is_decaying: Option<bool>,
    /// When the carbs will be absorbed (treatment source).
    #[serde(rename = "decayedBy", default, with = "time::iso")]
    pub decayed_by: Option<Timestamp>,
    /// Absorption rate in g/h (treatment source).
    #[serde(default, with = "de::num")]
    pub carbs_hr: Option<f64>,
    /// Display value.
    #[serde(default)]
    pub display: Option<Value>,
    /// Display line.
    #[serde(rename = "displayLine", default, with = "de::text")]
    pub display_line: Option<String>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `pump`: the newest devicestatus carrying pump data, with a display summary in `data`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PumpProperty {
    /// The pump object of the devicestatus.
    #[serde(default, deserialize_with = "de::tolerant")]
    pub pump: Option<super::devicestatus::Pump>,
    /// Pump clock.
    #[serde(rename = "clockMills", default, with = "time::millis")]
    pub clock_mills: Option<Timestamp>,
    /// Display summary (levels, labels, reservoir, battery, status).
    #[serde(default)]
    pub data: Option<Value>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `openaps` and `loop`: closed-loop status.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LoopProperty {
    /// Status display (`symbol`, `code`, `label`).
    #[serde(default, alias = "display")]
    pub status: Option<LoopStatusDisplay>,
    /// Last enacted action.
    #[serde(rename = "lastEnacted", default)]
    pub last_enacted: Option<Value>,
    /// Last suggestion (OpenAPS).
    #[serde(rename = "lastSuggested", default)]
    pub last_suggested: Option<Value>,
    /// Last prediction (Loop: `lastPredicted`; OpenAPS: `lastPredBGs`).
    #[serde(rename = "lastPredicted", alias = "lastPredBGs", default)]
    pub last_predicted: Option<Value>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// Closed-loop status display.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LoopStatusDisplay {
    /// Symbol shown in the UI.
    #[serde(default, with = "de::text")]
    pub symbol: Option<String>,
    /// Status code: `enacted`, `looping`, `recommendation`, `warning`, `error`, …
    #[serde(default, with = "de::text")]
    pub code: Option<String>,
    /// Status label.
    #[serde(default, with = "de::text")]
    pub label: Option<String>,
}

/// `bwp`: bolus wizard preview.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Bwp {
    /// Suggested bolus in units.
    #[serde(rename = "bolusEstimate", default, with = "de::num")]
    pub bolus_estimate: Option<f64>,
    /// Current IOB considered.
    #[serde(default, with = "de::num")]
    pub iob: Option<f64>,
    /// Expected glucose outcome.
    #[serde(default, with = "de::num")]
    pub outcome: Option<f64>,
    /// Display line.
    #[serde(rename = "displayLine", default, with = "de::text")]
    pub display_line: Option<String>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `cage`, `iage`, `bage`: time since the last device change.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DeviceAge {
    /// Whether a change event was found.
    #[serde(default, with = "de::flag")]
    pub found: Option<bool>,
    /// Age in hours.
    #[serde(default, with = "de::num")]
    pub age: Option<f64>,
    /// Whole days of the age.
    #[serde(default, with = "de::num")]
    pub days: Option<f64>,
    /// Remaining hours of the age.
    #[serde(default, with = "de::num")]
    pub hours: Option<f64>,
    /// When the change happened.
    #[serde(rename = "treatmentDate", default, with = "time::millis")]
    pub treatment_date: Option<Timestamp>,
    /// Notes on the change treatment.
    #[serde(default, with = "de::text")]
    pub notes: Option<String>,
    /// Alarm level.
    #[serde(default, with = "de::int")]
    pub level: Option<i64>,
    /// Display string, e.g. `2d3h`.
    #[serde(default, with = "de::text")]
    pub display: Option<String>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `sage`: sensor age, tracked from both `Sensor Start` and `Sensor Change`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SensorAge {
    /// Age since the last `Sensor Start`.
    #[serde(rename = "Sensor Start", default, deserialize_with = "de::tolerant")]
    pub sensor_start: Option<DeviceAge>,
    /// Age since the last `Sensor Change`.
    #[serde(rename = "Sensor Change", default, deserialize_with = "de::tolerant")]
    pub sensor_change: Option<DeviceAge>,
    /// Which of the two is the most recent.
    #[serde(default, with = "de::text")]
    pub min: Option<String>,
}

impl SensorAge {
    /// The age from the most recent of the two events.
    #[must_use]
    pub fn current(&self) -> Option<&DeviceAge> {
        match self.min.as_deref() {
            Some("Sensor Change") => self.sensor_change.as_ref(),
            _ => self.sensor_start.as_ref().or(self.sensor_change.as_ref()),
        }
    }
}

/// `basal`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Basal {
    /// Display string, e.g. `T: 0.850U`.
    #[serde(default, with = "de::text")]
    pub display: Option<String>,
    /// Current rates.
    #[serde(default, deserialize_with = "de::tolerant")]
    pub current: Option<BasalCurrent>,
}

/// Current basal rates.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct BasalCurrent {
    /// Scheduled profile basal (U/h).
    #[serde(default, with = "de::num")]
    pub basal: Option<f64>,
    /// Active temp basal (U/h).
    #[serde(default, with = "de::num")]
    pub tempbasal: Option<f64>,
    /// Extended bolus rate (U/h).
    #[serde(default, with = "de::num")]
    pub combobolusbasal: Option<f64>,
    /// Total delivered basal (U/h).
    #[serde(default, with = "de::num")]
    pub totalbasal: Option<f64>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `dbsize`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DbSize {
    /// Display string.
    #[serde(default, with = "de::text")]
    pub display: Option<String>,
    /// Alert status.
    #[serde(default, with = "de::text")]
    pub status: Option<String>,
    /// Stored data in MiB.
    #[serde(rename = "totalDataSize", default, with = "de::num")]
    pub total_data_size: Option<f64>,
    /// Percent of the configured maximum.
    #[serde(rename = "dataPercentage", default, with = "de::num")]
    pub data_percentage: Option<f64>,
    /// Fields cinnamon does not model.
    #[serde(flatten)]
    pub extra: Extra,
}

/// `runtimestate`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RuntimeState {
    /// `booting`, `booted` or `loaded`.
    #[serde(default, with = "de::text")]
    pub state: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn one_odd_property_does_not_fail_the_rest() {
        let props: Properties = serde_json::from_value(serde_json::json!({
            "iob": {"iob": 1.25, "source": "Loop", "display": "1.25", "displayLine": "IOB: 1.25U"},
            "cob": {"cob": 30, "isDecaying": 1, "decayedBy": "2024-05-01T13:30:00.000Z", "display": 30},
            "delta": "this plugin changed its shape",
            "sage": {"Sensor Start": {"found": true, "age": 50, "display": "2d2h"}, "min": "Sensor Start"},
            "future_plugin": {"x": 1}
        }))
        .unwrap_or_else(|e| panic!("{e}"));
        assert_eq!(props.iob.and_then(|i| i.iob), Some(1.25));
        assert_eq!(props.cob.as_ref().and_then(|c| c.is_decaying), Some(true));
        assert!(props.delta.is_none());
        assert_eq!(
            props.sage.and_then(|s| s.current().and_then(|a| a.age)),
            Some(50.0)
        );
        assert!(props.extra.contains_key("future_plugin"));
    }
}