rustyfit 0.10.0

The #![no_std] Rust implementation of The Flexible and Interoperable Data Transfer (FIT) Protocol for decoding and encoding Garmin FIT files, supporting FIT Protocol V2.
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
// Code generated by fitgen/main.go. DO NOT EDIT.

// Copyright 2025 The RustyFIT Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

use crate::profile::typedef::{self, FitBaseType};
use crate::proto::*;
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};

/// Device Settings message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct DeviceSettings {
    /// Index into time zone arrays.
    pub active_time_zone: u8,
    /// Offset from system time. Required to convert timestamp from system time to UTC.
    pub utc_offset: u32,
    /// Units: s; Offset from system time.
    pub time_offset: Vec<u32>,
    /// Display mode for the time
    pub time_mode: Vec<typedef::TimeMode>,
    /// Scale: 4; Units: hr; timezone offset in 1/4 hour increments
    pub time_zone_offset: Vec<i8>,
    /// Mode for backlight
    pub backlight_mode: typedef::BacklightMode,
    /// Enabled state of the activity tracker functionality
    pub activity_tracker_enabled: typedef::Bool,
    /// UTC timestamp used to set the devices clock and date
    pub clock_time: typedef::DateTime,
    /// Bitfield to configure enabled screens for each supported loop
    pub pages_enabled: Vec<u16>,
    /// Enabled state of the move alert
    pub move_alert_enabled: typedef::Bool,
    /// Display mode for the date
    pub date_mode: typedef::DateMode,
    pub display_orientation: typedef::DisplayOrientation,
    pub mounting_side: typedef::Side,
    /// Bitfield to indicate one page as default for each supported loop
    pub default_page: Vec<u16>,
    /// Units: steps; Minimum steps before an autosync can occur
    pub autosync_min_steps: u16,
    /// Units: minutes; Minimum minutes before an autosync can occur
    pub autosync_min_time: u16,
    /// Enable auto-detect setting for the lactate threshold feature.
    pub lactate_threshold_autodetect_enabled: typedef::Bool,
    /// Automatically upload using BLE
    pub ble_auto_upload_enabled: typedef::Bool,
    /// Helps to conserve battery by changing modes
    pub auto_sync_frequency: typedef::AutoSyncFrequency,
    /// Allows setting specific activities auto-activity detect enabled/disabled settings
    pub auto_activity_detect: typedef::AutoActivityDetect,
    /// Number of screens configured to display
    pub number_of_screens: u8,
    /// Smart Notification display orientation
    pub smart_notification_display_orientation: typedef::DisplayOrientation,
    pub tap_interface: typedef::Switch,
    /// Used to hold the tap threshold setting
    pub tap_sensitivity: typedef::TapSensitivity,
    /// unknown_fields are fields that are exist but they are not defined in Profile.xlsx
    pub unknown_fields: Vec<Field>,
    /// developer_fields are custom data fields (Added since protocol version 2.0)
    pub developer_fields: Vec<DeveloperField>,
}

impl DeviceSettings {
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::Uint8
    pub const ACTIVE_TIME_ZONE: u8 = 0;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::Uint32
    pub const UTC_OFFSET: u8 = 1;
    /// Value's type: `Vec<u32>`; FitBaseType::UINT32; ProfileType::Uint32; Units: `s`
    pub const TIME_OFFSET: u8 = 2;
    /// Value's type: `Vec<u8>`; FitBaseType::ENUM; ProfileType::TimeMode
    pub const TIME_MODE: u8 = 4;
    /// Value's type: `Vec<i8>`; FitBaseType::SINT8; ProfileType::Sint8; Scale: `4`; Units: `hr`
    pub const TIME_ZONE_OFFSET: u8 = 5;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::BacklightMode
    pub const BACKLIGHT_MODE: u8 = 12;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::Bool
    pub const ACTIVITY_TRACKER_ENABLED: u8 = 36;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::DateTime
    pub const CLOCK_TIME: u8 = 39;
    /// Value's type: `Vec<u16>`; FitBaseType::UINT16; ProfileType::Uint16
    pub const PAGES_ENABLED: u8 = 40;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::Bool
    pub const MOVE_ALERT_ENABLED: u8 = 46;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::DateMode
    pub const DATE_MODE: u8 = 47;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::DisplayOrientation
    pub const DISPLAY_ORIENTATION: u8 = 55;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::Side
    pub const MOUNTING_SIDE: u8 = 56;
    /// Value's type: `Vec<u16>`; FitBaseType::UINT16; ProfileType::Uint16
    pub const DEFAULT_PAGE: u8 = 57;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16; Units: `steps`
    pub const AUTOSYNC_MIN_STEPS: u8 = 58;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16; Units: `minutes`
    pub const AUTOSYNC_MIN_TIME: u8 = 59;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::Bool
    pub const LACTATE_THRESHOLD_AUTODETECT_ENABLED: u8 = 80;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::Bool
    pub const BLE_AUTO_UPLOAD_ENABLED: u8 = 86;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::AutoSyncFrequency
    pub const AUTO_SYNC_FREQUENCY: u8 = 89;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::AutoActivityDetect
    pub const AUTO_ACTIVITY_DETECT: u8 = 90;
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::Uint8
    pub const NUMBER_OF_SCREENS: u8 = 94;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::DisplayOrientation
    pub const SMART_NOTIFICATION_DISPLAY_ORIENTATION: u8 = 95;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::Switch
    pub const TAP_INTERFACE: u8 = 134;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::TapSensitivity
    pub const TAP_SENSITIVITY: u8 = 174;

    /// Create new DeviceSettings with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            active_time_zone: u8::MAX,
            utc_offset: u32::MAX,
            time_offset: Vec::new(),
            time_mode: Vec::new(),
            time_zone_offset: Vec::new(),
            backlight_mode: typedef::BacklightMode(u8::MAX),
            activity_tracker_enabled: typedef::Bool(u8::MAX),
            clock_time: typedef::DateTime(u32::MAX),
            pages_enabled: Vec::new(),
            move_alert_enabled: typedef::Bool(u8::MAX),
            date_mode: typedef::DateMode(u8::MAX),
            display_orientation: typedef::DisplayOrientation(u8::MAX),
            mounting_side: typedef::Side(u8::MAX),
            default_page: Vec::new(),
            autosync_min_steps: u16::MAX,
            autosync_min_time: u16::MAX,
            lactate_threshold_autodetect_enabled: typedef::Bool(u8::MAX),
            ble_auto_upload_enabled: typedef::Bool(u8::MAX),
            auto_sync_frequency: typedef::AutoSyncFrequency(u8::MAX),
            auto_activity_detect: typedef::AutoActivityDetect(u32::MAX),
            number_of_screens: u8::MAX,
            smart_notification_display_orientation: typedef::DisplayOrientation(u8::MAX),
            tap_interface: typedef::Switch(u8::MAX),
            tap_sensitivity: typedef::TapSensitivity(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `time_zone_offset` in its scaled value. It returns `None` when value is invalid.
    ///
    /// Units: hr
    pub fn time_zone_offset_scaled(&self) -> Option<Vec<f64>> {
        if self.time_zone_offset.is_empty() {
            return None;
        }
        let mut v = Vec::with_capacity(self.time_zone_offset.len());
        for &x in &self.time_zone_offset {
            v.push(x as f64 / 4.0 - 0.0)
        }
        Some(v)
    }

    /// Set `time_zone_offset` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_time_zone_offset_scaled(&mut self, v: &[f64]) -> &mut Self {
        self.time_zone_offset = Vec::with_capacity(v.len());
        if v.is_empty() {
            return self;
        }
        for &x in v {
            let unscaled = (x + 0.0) * 4.0;
            if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i8::MAX as f64 {
                self.time_zone_offset.push(i8::MAX);
                continue;
            }
            self.time_zone_offset.push(unscaled as i8);
        }
        self
    }

    fn count_valid_fields(&self) -> usize {
        (self.active_time_zone != u8::MAX) as usize
            + (self.utc_offset != u32::MAX) as usize
            + (!self.time_offset.is_empty()) as usize
            + (!self.time_mode.is_empty()) as usize
            + (!self.time_zone_offset.is_empty()) as usize
            + (self.backlight_mode.0 != u8::MAX) as usize
            + (self.activity_tracker_enabled.0 != u8::MAX) as usize
            + (self.clock_time.0 != u32::MAX) as usize
            + (!self.pages_enabled.is_empty()) as usize
            + (self.move_alert_enabled.0 != u8::MAX) as usize
            + (self.date_mode.0 != u8::MAX) as usize
            + (self.display_orientation.0 != u8::MAX) as usize
            + (self.mounting_side.0 != u8::MAX) as usize
            + (!self.default_page.is_empty()) as usize
            + (self.autosync_min_steps != u16::MAX) as usize
            + (self.autosync_min_time != u16::MAX) as usize
            + (self.lactate_threshold_autodetect_enabled.0 != u8::MAX) as usize
            + (self.ble_auto_upload_enabled.0 != u8::MAX) as usize
            + (self.auto_sync_frequency.0 != u8::MAX) as usize
            + (self.auto_activity_detect.0 != u32::MAX) as usize
            + (self.number_of_screens != u8::MAX) as usize
            + (self.smart_notification_display_orientation.0 != u8::MAX) as usize
            + (self.tap_interface.0 != u8::MAX) as usize
            + (self.tap_sensitivity.0 != u8::MAX) as usize
    }
}

impl Default for DeviceSettings {
    fn default() -> Self {
        Self::new()
    }
}

impl From<&Message> for DeviceSettings {
    /// from creates new DeviceSettings struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [1117105531807338551, 3326148608, 70368744177728, 0];
        let mut n = 0u64;
        for field in &mesg.fields {
            n += (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 ^ 1
        }

        let mut v = Self::new();
        v.unknown_fields = Vec::<Field>::with_capacity(n as usize);
        v.developer_fields = mesg.developer_fields.clone();

        for field in &mesg.fields {
            match field.num {
                0 => v.active_time_zone = field.value.as_u8(),
                1 => v.utc_offset = field.value.as_u32(),
                2 => v.time_offset = field.value.to_vec_u32(),
                4 => {
                    v.time_mode = match &field.value {
                        Value::VecUint8(v) => {
                            let mut vs = Vec::with_capacity(v.len());
                            vs.extend(v.iter().map(|&x| typedef::TimeMode(x)));
                            vs
                        }
                        _ => Vec::new(),
                    }
                }
                5 => v.time_zone_offset = field.value.to_vec_i8(),
                12 => v.backlight_mode = typedef::BacklightMode(field.value.as_u8()),
                36 => v.activity_tracker_enabled = typedef::Bool(field.value.as_u8()),
                39 => v.clock_time = typedef::DateTime(field.value.as_u32()),
                40 => v.pages_enabled = field.value.to_vec_u16(),
                46 => v.move_alert_enabled = typedef::Bool(field.value.as_u8()),
                47 => v.date_mode = typedef::DateMode(field.value.as_u8()),
                55 => v.display_orientation = typedef::DisplayOrientation(field.value.as_u8()),
                56 => v.mounting_side = typedef::Side(field.value.as_u8()),
                57 => v.default_page = field.value.to_vec_u16(),
                58 => v.autosync_min_steps = field.value.as_u16(),
                59 => v.autosync_min_time = field.value.as_u16(),
                80 => v.lactate_threshold_autodetect_enabled = typedef::Bool(field.value.as_u8()),
                86 => v.ble_auto_upload_enabled = typedef::Bool(field.value.as_u8()),
                89 => v.auto_sync_frequency = typedef::AutoSyncFrequency(field.value.as_u8()),
                90 => v.auto_activity_detect = typedef::AutoActivityDetect(field.value.as_u32()),
                94 => v.number_of_screens = field.value.as_u8(),
                95 => {
                    v.smart_notification_display_orientation =
                        typedef::DisplayOrientation(field.value.as_u8())
                }
                134 => v.tap_interface = typedef::Switch(field.value.as_u8()),
                174 => v.tap_sensitivity = typedef::TapSensitivity(field.value.as_u8()),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

impl From<DeviceSettings> for Message {
    fn from(m: DeviceSettings) -> Self {
        let mut fields =
            Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());

        if m.active_time_zone != u8::MAX {
            fields.push(Field {
                num: 0,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.active_time_zone),
                is_expanded: false,
            });
        };
        if m.utc_offset != u32::MAX {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.utc_offset),
                is_expanded: false,
            });
        };
        if !m.time_offset.is_empty() {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::UINT32,
                value: Value::VecUint32(m.time_offset),
                is_expanded: false,
            });
        };
        if !m.time_mode.is_empty() {
            fields.push(Field {
                num: 4,
                base_type: FitBaseType::ENUM,
                value: Value::VecUint8({
                    let (ptr, len, capacity) = m.time_mode.into_raw_parts();
                    unsafe { Vec::from_raw_parts(ptr.cast::<u8>(), len, capacity) }
                }),
                is_expanded: false,
            });
        };
        if !m.time_zone_offset.is_empty() {
            fields.push(Field {
                num: 5,
                base_type: FitBaseType::SINT8,
                value: Value::VecInt8(m.time_zone_offset),
                is_expanded: false,
            });
        };
        if m.backlight_mode.0 != u8::MAX {
            fields.push(Field {
                num: 12,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.backlight_mode.0),
                is_expanded: false,
            });
        };
        if m.activity_tracker_enabled.0 != u8::MAX {
            fields.push(Field {
                num: 36,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.activity_tracker_enabled.0),
                is_expanded: false,
            });
        };
        if m.clock_time.0 != u32::MAX {
            fields.push(Field {
                num: 39,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.clock_time.0),
                is_expanded: false,
            });
        };
        if !m.pages_enabled.is_empty() {
            fields.push(Field {
                num: 40,
                base_type: FitBaseType::UINT16,
                value: Value::VecUint16(m.pages_enabled),
                is_expanded: false,
            });
        };
        if m.move_alert_enabled.0 != u8::MAX {
            fields.push(Field {
                num: 46,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.move_alert_enabled.0),
                is_expanded: false,
            });
        };
        if m.date_mode.0 != u8::MAX {
            fields.push(Field {
                num: 47,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.date_mode.0),
                is_expanded: false,
            });
        };
        if m.display_orientation.0 != u8::MAX {
            fields.push(Field {
                num: 55,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.display_orientation.0),
                is_expanded: false,
            });
        };
        if m.mounting_side.0 != u8::MAX {
            fields.push(Field {
                num: 56,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.mounting_side.0),
                is_expanded: false,
            });
        };
        if !m.default_page.is_empty() {
            fields.push(Field {
                num: 57,
                base_type: FitBaseType::UINT16,
                value: Value::VecUint16(m.default_page),
                is_expanded: false,
            });
        };
        if m.autosync_min_steps != u16::MAX {
            fields.push(Field {
                num: 58,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.autosync_min_steps),
                is_expanded: false,
            });
        };
        if m.autosync_min_time != u16::MAX {
            fields.push(Field {
                num: 59,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.autosync_min_time),
                is_expanded: false,
            });
        };
        if m.lactate_threshold_autodetect_enabled.0 != u8::MAX {
            fields.push(Field {
                num: 80,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.lactate_threshold_autodetect_enabled.0),
                is_expanded: false,
            });
        };
        if m.ble_auto_upload_enabled.0 != u8::MAX {
            fields.push(Field {
                num: 86,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.ble_auto_upload_enabled.0),
                is_expanded: false,
            });
        };
        if m.auto_sync_frequency.0 != u8::MAX {
            fields.push(Field {
                num: 89,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.auto_sync_frequency.0),
                is_expanded: false,
            });
        };
        if m.auto_activity_detect.0 != u32::MAX {
            fields.push(Field {
                num: 90,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.auto_activity_detect.0),
                is_expanded: false,
            });
        };
        if m.number_of_screens != u8::MAX {
            fields.push(Field {
                num: 94,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.number_of_screens),
                is_expanded: false,
            });
        };
        if m.smart_notification_display_orientation.0 != u8::MAX {
            fields.push(Field {
                num: 95,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.smart_notification_display_orientation.0),
                is_expanded: false,
            });
        };
        if m.tap_interface.0 != u8::MAX {
            fields.push(Field {
                num: 134,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.tap_interface.0),
                is_expanded: false,
            });
        };
        if m.tap_sensitivity.0 != u8::MAX {
            fields.push(Field {
                num: 174,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.tap_sensitivity.0),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

        Self {
            header: 0,
            num: typedef::MesgNum::DEVICE_SETTINGS,
            fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Serialize for DeviceSettings {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let n = self.count_valid_fields() + 2;
        let mut state = serializer.serialize_struct("DeviceSettings", n)?;
        if self.active_time_zone != u8::MAX {
            state.serialize_field("active_time_zone", &self.active_time_zone)?;
        }
        if self.utc_offset != u32::MAX {
            state.serialize_field("utc_offset", &self.utc_offset)?;
        }
        if !self.time_offset.is_empty() {
            state.serialize_field("time_offset", &self.time_offset)?;
        }
        if !self.time_mode.is_empty() {
            state.serialize_field("time_mode", &self.time_mode)?;
        }
        if let Some(v) = self.time_zone_offset_scaled() {
            state.serialize_field("time_zone_offset", &v)?;
        }
        if self.backlight_mode.0 != u8::MAX {
            state.serialize_field("backlight_mode", &self.backlight_mode)?;
        }
        if self.activity_tracker_enabled.0 != u8::MAX {
            state.serialize_field("activity_tracker_enabled", &self.activity_tracker_enabled)?;
        }
        if let Some(v) = self.clock_time.unix_timestamp() {
            state.serialize_field("clock_time", &v)?;
        }
        if !self.pages_enabled.is_empty() {
            state.serialize_field("pages_enabled", &self.pages_enabled)?;
        }
        if self.move_alert_enabled.0 != u8::MAX {
            state.serialize_field("move_alert_enabled", &self.move_alert_enabled)?;
        }
        if self.date_mode.0 != u8::MAX {
            state.serialize_field("date_mode", &self.date_mode)?;
        }
        if self.display_orientation.0 != u8::MAX {
            state.serialize_field("display_orientation", &self.display_orientation)?;
        }
        if self.mounting_side.0 != u8::MAX {
            state.serialize_field("mounting_side", &self.mounting_side)?;
        }
        if !self.default_page.is_empty() {
            state.serialize_field("default_page", &self.default_page)?;
        }
        if self.autosync_min_steps != u16::MAX {
            state.serialize_field("autosync_min_steps", &self.autosync_min_steps)?;
        }
        if self.autosync_min_time != u16::MAX {
            state.serialize_field("autosync_min_time", &self.autosync_min_time)?;
        }
        if self.lactate_threshold_autodetect_enabled.0 != u8::MAX {
            state.serialize_field(
                "lactate_threshold_autodetect_enabled",
                &self.lactate_threshold_autodetect_enabled,
            )?;
        }
        if self.ble_auto_upload_enabled.0 != u8::MAX {
            state.serialize_field("ble_auto_upload_enabled", &self.ble_auto_upload_enabled)?;
        }
        if self.auto_sync_frequency.0 != u8::MAX {
            state.serialize_field("auto_sync_frequency", &self.auto_sync_frequency)?;
        }
        if self.auto_activity_detect.0 != u32::MAX {
            state.serialize_field("auto_activity_detect", &self.auto_activity_detect)?;
        }
        if self.number_of_screens != u8::MAX {
            state.serialize_field("number_of_screens", &self.number_of_screens)?;
        }
        if self.smart_notification_display_orientation.0 != u8::MAX {
            state.serialize_field(
                "smart_notification_display_orientation",
                &self.smart_notification_display_orientation,
            )?;
        }
        if self.tap_interface.0 != u8::MAX {
            state.serialize_field("tap_interface", &self.tap_interface)?;
        }
        if self.tap_sensitivity.0 != u8::MAX {
            state.serialize_field("tap_sensitivity", &self.tap_sensitivity)?;
        }
        if !self.unknown_fields.is_empty() {
            state.serialize_field("unknown_fields", &self.unknown_fields)?;
        }
        if !self.developer_fields.is_empty() {
            state.serialize_field("developer_fields", &self.developer_fields)?;
        }
        state.end()
    }
}

#[cfg(feature = "serde")]
#[cfg_attr(feature = "serde", derive(Deserialize), serde(default))]
struct De {
    active_time_zone: u8,
    utc_offset: u32,
    time_offset: Vec<u32>,
    time_mode: Vec<typedef::TimeMode>,
    time_zone_offset: Vec<f64>,
    backlight_mode: typedef::BacklightMode,
    activity_tracker_enabled: typedef::Bool,
    clock_time: Option<i64>,
    pages_enabled: Vec<u16>,
    move_alert_enabled: typedef::Bool,
    date_mode: typedef::DateMode,
    display_orientation: typedef::DisplayOrientation,
    mounting_side: typedef::Side,
    default_page: Vec<u16>,
    autosync_min_steps: u16,
    autosync_min_time: u16,
    lactate_threshold_autodetect_enabled: typedef::Bool,
    ble_auto_upload_enabled: typedef::Bool,
    auto_sync_frequency: typedef::AutoSyncFrequency,
    auto_activity_detect: typedef::AutoActivityDetect,
    number_of_screens: u8,
    smart_notification_display_orientation: typedef::DisplayOrientation,
    tap_interface: typedef::Switch,
    tap_sensitivity: typedef::TapSensitivity,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for DeviceSettings {
    fn from(m: De) -> Self {
        Self {
            active_time_zone: m.active_time_zone,
            utc_offset: m.utc_offset,
            time_offset: m.time_offset,
            time_mode: m.time_mode,
            time_zone_offset: {
                if m.time_zone_offset.is_empty() {
                    Vec::new()
                } else {
                    let mut vals = Vec::with_capacity(m.time_zone_offset.len());
                    for &x in m.time_zone_offset.iter() {
                        let unscaled = (x + 0.0) * 4.0;
                        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i8::MAX as f64
                        {
                            vals.push(i8::MAX);
                            continue;
                        }
                        vals.push(unscaled as i8);
                    }
                    vals
                }
            },
            backlight_mode: m.backlight_mode,
            activity_tracker_enabled: m.activity_tracker_enabled,
            clock_time: m.clock_time.map_or_else(
                || typedef::DateTime(u32::MAX),
                typedef::DateTime::from_unix_timestamp,
            ),
            pages_enabled: m.pages_enabled,
            move_alert_enabled: m.move_alert_enabled,
            date_mode: m.date_mode,
            display_orientation: m.display_orientation,
            mounting_side: m.mounting_side,
            default_page: m.default_page,
            autosync_min_steps: m.autosync_min_steps,
            autosync_min_time: m.autosync_min_time,
            lactate_threshold_autodetect_enabled: m.lactate_threshold_autodetect_enabled,
            ble_auto_upload_enabled: m.ble_auto_upload_enabled,
            auto_sync_frequency: m.auto_sync_frequency,
            auto_activity_detect: m.auto_activity_detect,
            number_of_screens: m.number_of_screens,
            smart_notification_display_orientation: m.smart_notification_display_orientation,
            tap_interface: m.tap_interface,
            tap_sensitivity: m.tap_sensitivity,
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            active_time_zone: u8::MAX,
            utc_offset: u32::MAX,
            time_offset: Vec::new(),
            time_mode: Vec::new(),
            time_zone_offset: Vec::new(),
            backlight_mode: typedef::BacklightMode(u8::MAX),
            activity_tracker_enabled: typedef::Bool(u8::MAX),
            clock_time: None,
            pages_enabled: Vec::new(),
            move_alert_enabled: typedef::Bool(u8::MAX),
            date_mode: typedef::DateMode(u8::MAX),
            display_orientation: typedef::DisplayOrientation(u8::MAX),
            mounting_side: typedef::Side(u8::MAX),
            default_page: Vec::new(),
            autosync_min_steps: u16::MAX,
            autosync_min_time: u16::MAX,
            lactate_threshold_autodetect_enabled: typedef::Bool(u8::MAX),
            ble_auto_upload_enabled: typedef::Bool(u8::MAX),
            auto_sync_frequency: typedef::AutoSyncFrequency(u8::MAX),
            auto_activity_detect: typedef::AutoActivityDetect(u32::MAX),
            number_of_screens: u8::MAX,
            smart_notification_display_orientation: typedef::DisplayOrientation(u8::MAX),
            tap_interface: typedef::Switch(u8::MAX),
            tap_sensitivity: typedef::TapSensitivity(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}