rustyfit 0.5.0

This project hosts the Rust implementation for The Flexible and Interoperable Data Transfer (FIT) Protocol
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
// 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.

#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]

use crate::profile::{ProfileType, typedef};
use crate::proto::*;

fn is_expanded(state: &[u8], num: u8) -> bool {
    match num {
        5 | 28 => (state[num as usize >> 3] >> (num & 7)) & 1 == 1,
        _ => false,
    }
}

#[derive(Debug, Clone)]
/// Monitoring is a Monitoring message.
pub struct Monitoring {
    /// Units: s; Must align to logging interval, for example, time must be 00:00:00 for daily log.
    pub timestamp: typedef::DateTime,
    /// Associates this data to device_info message. Not required for file with single device (sensor).
    pub device_index: typedef::DeviceIndex,
    /// Units: kcal; Accumulated total calories. Maintained by MonitoringReader for each activity_type. See SDK documentation
    pub calories: u16,
    /// Scale: 100; Units: m; Accumulated distance. Maintained by MonitoringReader for each activity_type. See SDK documentation.
    pub distance: u32,
    /// Scale: 2; Units: cycles; Accumulated cycles. Maintained by MonitoringReader for each activity_type. See SDK documentation.
    pub cycles: u32,
    /// Scale: 1000; Units: s
    pub active_time: u32,
    pub activity_type: typedef::ActivityType,
    pub activity_subtype: typedef::ActivitySubtype,
    pub activity_level: typedef::ActivityLevel,
    /// Units: 100 * m
    pub distance_16: u16,
    /// Units: 2 * cycles (steps)
    pub cycles_16: u16,
    /// Units: s
    pub active_time_16: u16,
    /// Must align to logging interval, for example, time must be 00:00:00 for daily log.
    pub local_timestamp: typedef::LocalDateTime,
    /// Scale: 100; Units: C; Avg temperature during the logging interval ended at timestamp
    pub temperature: i16,
    /// Scale: 100; Units: C; Min temperature during the logging interval ended at timestamp
    pub temperature_min: i16,
    /// Scale: 100; Units: C; Max temperature during the logging interval ended at timestamp
    pub temperature_max: i16,
    /// Array: [8]; Units: minutes; Indexed using minute_activity_level enum
    pub activity_time: [u16; 8],
    /// Units: kcal
    pub active_calories: u16,
    /// Indicates single type / intensity for duration since last monitoring message.
    pub current_activity_type_intensity: u8,
    /// Units: min
    pub timestamp_min_8: u8,
    /// Units: s
    pub timestamp_16: u16,
    /// Units: bpm
    pub heart_rate: u8,
    /// Scale: 10
    pub intensity: u8,
    /// Units: min
    pub duration_min: u16,
    /// Units: s
    pub duration: u32,
    /// Scale: 1000; Units: m
    pub ascent: u32,
    /// Scale: 1000; Units: m
    pub descent: u32,
    /// Units: minutes
    pub moderate_activity_minutes: u16,
    /// Units: minutes
    pub vigorous_activity_minutes: u16,
    state: [u8; 4], // Used for tracking expanded fields.
    /// 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 Monitoring {
    /// Value's type: `u32`; Units: `s`
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u8`
    pub const DEVICE_INDEX: u8 = 0;
    /// Value's type: `u16`; Units: `kcal`
    pub const CALORIES: u8 = 1;
    /// Value's type: `u32`; Scale: `100`; Units: `m`
    pub const DISTANCE: u8 = 2;
    /// Value's type: `u32`; Scale: `2`; Units: `cycles`
    pub const CYCLES: u8 = 3;
    /// Value's type: `u32`; Scale: `1000`; Units: `s`
    pub const ACTIVE_TIME: u8 = 4;
    /// Value's type: `u8`
    pub const ACTIVITY_TYPE: u8 = 5;
    /// Value's type: `u8`
    pub const ACTIVITY_SUBTYPE: u8 = 6;
    /// Value's type: `u8`
    pub const ACTIVITY_LEVEL: u8 = 7;
    /// Value's type: `u16`; Units: `100 * m`
    pub const DISTANCE_16: u8 = 8;
    /// Value's type: `u16`; Units: `2 * cycles (steps)`
    pub const CYCLES_16: u8 = 9;
    /// Value's type: `u16`; Units: `s`
    pub const ACTIVE_TIME_16: u8 = 10;
    /// Value's type: `u32`
    pub const LOCAL_TIMESTAMP: u8 = 11;
    /// Value's type: `i16`; Scale: `100`; Units: `C`
    pub const TEMPERATURE: u8 = 12;
    /// Value's type: `i16`; Scale: `100`; Units: `C`
    pub const TEMPERATURE_MIN: u8 = 14;
    /// Value's type: `i16`; Scale: `100`; Units: `C`
    pub const TEMPERATURE_MAX: u8 = 15;
    /// Value's type: `[u16; 8]`; Units: `minutes`
    pub const ACTIVITY_TIME: u8 = 16;
    /// Value's type: `u16`; Units: `kcal`
    pub const ACTIVE_CALORIES: u8 = 19;
    /// Value's type: `u8`
    pub const CURRENT_ACTIVITY_TYPE_INTENSITY: u8 = 24;
    /// Value's type: `u8`; Units: `min`
    pub const TIMESTAMP_MIN_8: u8 = 25;
    /// Value's type: `u16`; Units: `s`
    pub const TIMESTAMP_16: u8 = 26;
    /// Value's type: `u8`; Units: `bpm`
    pub const HEART_RATE: u8 = 27;
    /// Value's type: `u8`; Scale: `10`
    pub const INTENSITY: u8 = 28;
    /// Value's type: `u16`; Units: `min`
    pub const DURATION_MIN: u8 = 29;
    /// Value's type: `u32`; Units: `s`
    pub const DURATION: u8 = 30;
    /// Value's type: `u32`; Scale: `1000`; Units: `m`
    pub const ASCENT: u8 = 31;
    /// Value's type: `u32`; Scale: `1000`; Units: `m`
    pub const DESCENT: u8 = 32;
    /// Value's type: `u16`; Units: `minutes`
    pub const MODERATE_ACTIVITY_MINUTES: u8 = 33;
    /// Value's type: `u16`; Units: `minutes`
    pub const VIGOROUS_ACTIVITY_MINUTES: u8 = 34;

    /// Create new Monitoring with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            timestamp: typedef::DateTime(u32::MAX),
            device_index: typedef::DeviceIndex(u8::MAX),
            calories: u16::MAX,
            distance: u32::MAX,
            cycles: u32::MAX,
            active_time: u32::MAX,
            activity_type: typedef::ActivityType(u8::MAX),
            activity_subtype: typedef::ActivitySubtype(u8::MAX),
            activity_level: typedef::ActivityLevel(u8::MAX),
            distance_16: u16::MAX,
            cycles_16: u16::MAX,
            active_time_16: u16::MAX,
            local_timestamp: typedef::LocalDateTime(u32::MAX),
            temperature: i16::MAX,
            temperature_min: i16::MAX,
            temperature_max: i16::MAX,
            activity_time: [u16::MAX; 8],
            active_calories: u16::MAX,
            current_activity_type_intensity: u8::MAX,
            timestamp_min_8: u8::MAX,
            timestamp_16: u16::MAX,
            heart_rate: u8::MAX,
            intensity: u8::MAX,
            duration_min: u16::MAX,
            duration: u32::MAX,
            ascent: u32::MAX,
            descent: u32::MAX,
            moderate_activity_minutes: u16::MAX,
            vigorous_activity_minutes: u16::MAX,
            state: [0u8; 4],
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `distance` in its scaled value. It returns invalid f64 when value is valid.
    pub fn distance_scaled(&self) -> f64 {
        if self.distance == u32::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.distance as f64 / 100.0 - 0.0
    }

    /// Set `distance` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_distance_scaled(&mut self, v: f64) -> &mut Monitoring {
        let unscaled = (v + 0.0) * 100.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
            self.distance = u32::MAX;
            return self;
        }
        self.distance = unscaled as u32;
        self
    }

    /// Returns `cycles` in its scaled value. It returns invalid f64 when value is valid.
    pub fn cycles_scaled(&self) -> f64 {
        if self.cycles == u32::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.cycles as f64 / 2.0 - 0.0
    }

    /// Set `cycles` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_cycles_scaled(&mut self, v: f64) -> &mut Monitoring {
        let unscaled = (v + 0.0) * 2.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
            self.cycles = u32::MAX;
            return self;
        }
        self.cycles = unscaled as u32;
        self
    }

    /// Returns `active_time` in its scaled value. It returns invalid f64 when value is valid.
    pub fn active_time_scaled(&self) -> f64 {
        if self.active_time == u32::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.active_time as f64 / 1000.0 - 0.0
    }

    /// Set `active_time` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_active_time_scaled(&mut self, v: f64) -> &mut Monitoring {
        let unscaled = (v + 0.0) * 1000.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
            self.active_time = u32::MAX;
            return self;
        }
        self.active_time = unscaled as u32;
        self
    }

    /// Returns `temperature` in its scaled value. It returns invalid f64 when value is valid.
    pub fn temperature_scaled(&self) -> f64 {
        if self.temperature == i16::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.temperature as f64 / 100.0 - 0.0
    }

    /// Set `temperature` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_temperature_scaled(&mut self, v: f64) -> &mut Monitoring {
        let unscaled = (v + 0.0) * 100.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64 {
            self.temperature = i16::MAX;
            return self;
        }
        self.temperature = unscaled as i16;
        self
    }

    /// Returns `temperature_min` in its scaled value. It returns invalid f64 when value is valid.
    pub fn temperature_min_scaled(&self) -> f64 {
        if self.temperature_min == i16::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.temperature_min as f64 / 100.0 - 0.0
    }

    /// Set `temperature_min` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_temperature_min_scaled(&mut self, v: f64) -> &mut Monitoring {
        let unscaled = (v + 0.0) * 100.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64 {
            self.temperature_min = i16::MAX;
            return self;
        }
        self.temperature_min = unscaled as i16;
        self
    }

    /// Returns `temperature_max` in its scaled value. It returns invalid f64 when value is valid.
    pub fn temperature_max_scaled(&self) -> f64 {
        if self.temperature_max == i16::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.temperature_max as f64 / 100.0 - 0.0
    }

    /// Set `temperature_max` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_temperature_max_scaled(&mut self, v: f64) -> &mut Monitoring {
        let unscaled = (v + 0.0) * 100.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64 {
            self.temperature_max = i16::MAX;
            return self;
        }
        self.temperature_max = unscaled as i16;
        self
    }

    /// Returns `intensity` in its scaled value. It returns invalid f64 when value is valid.
    pub fn intensity_scaled(&self) -> f64 {
        if self.intensity == u8::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.intensity as f64 / 10.0 - 0.0
    }

    /// Set `intensity` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_intensity_scaled(&mut self, v: f64) -> &mut Monitoring {
        let unscaled = (v + 0.0) * 10.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u8::MAX as f64 {
            self.intensity = u8::MAX;
            return self;
        }
        self.intensity = unscaled as u8;
        self
    }

    /// Returns `ascent` in its scaled value. It returns invalid f64 when value is valid.
    pub fn ascent_scaled(&self) -> f64 {
        if self.ascent == u32::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.ascent as f64 / 1000.0 - 0.0
    }

    /// Set `ascent` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_ascent_scaled(&mut self, v: f64) -> &mut Monitoring {
        let unscaled = (v + 0.0) * 1000.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
            self.ascent = u32::MAX;
            return self;
        }
        self.ascent = unscaled as u32;
        self
    }

    /// Returns `descent` in its scaled value. It returns invalid f64 when value is valid.
    pub fn descent_scaled(&self) -> f64 {
        if self.descent == u32::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.descent as f64 / 1000.0 - 0.0
    }

    /// Set `descent` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_descent_scaled(&mut self, v: f64) -> &mut Monitoring {
        let unscaled = (v + 0.0) * 1000.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
            self.descent = u32::MAX;
            return self;
        }
        self.descent = unscaled as u32;
        self
    }

    /// Marks whether given field's num is an expanded field (field that being generated through a component expansion).
    pub fn mark_as_expanded(&mut self, num: u8, flag: bool) -> bool {
        match num {
            5 | 28 => {
                if flag {
                    self.state[num as usize >> 3] |= 1 << (num & 7)
                } else {
                    self.state[num as usize >> 3] &= !(1 << (num & 7))
                }
                true
            }
            _ => false,
        }
    }

    /// checks whether given field's num is a field generated through a component expansion.
    pub fn is_expanded(&self, num: u8) -> bool {
        is_expanded(&self.state, num)
    }
}

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

impl From<&Message> for Monitoring {
    /// from creates new Monitoring struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        let mut vals: [&Value; 254] = [const { &Value::Invalid }; 254];
        let mut state = [0u8; 4];

        const KNOWN_NUMS: [u64; 4] = [34343608319, 0, 0, 2305843009213693952];
        let mut n = 0u64;
        for field in &mesg.fields {
            n += (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 ^ 1
        }
        let mut unknown_fields: Vec<Field> = Vec::with_capacity(n as usize);

        for field in &mesg.fields {
            if (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 == 0 {
                unknown_fields.push(field.clone());
                continue;
            }
            if field.is_expanded && field.num < 29 {
                state[field.num as usize >> 3] |= 1 << (field.num & 7)
            }
            vals[field.num as usize] = &field.value;
        }

        Self {
            timestamp: typedef::DateTime(vals[253].as_u32()),
            device_index: typedef::DeviceIndex(vals[0].as_u8()),
            calories: vals[1].as_u16(),
            distance: vals[2].as_u32(),
            cycles: vals[3].as_u32(),
            active_time: vals[4].as_u32(),
            activity_type: typedef::ActivityType(vals[5].as_u8()),
            activity_subtype: typedef::ActivitySubtype(vals[6].as_u8()),
            activity_level: typedef::ActivityLevel(vals[7].as_u8()),
            distance_16: vals[8].as_u16(),
            cycles_16: vals[9].as_u16(),
            active_time_16: vals[10].as_u16(),
            local_timestamp: typedef::LocalDateTime(vals[11].as_u32()),
            temperature: vals[12].as_i16(),
            temperature_min: vals[14].as_i16(),
            temperature_max: vals[15].as_i16(),
            activity_time: match &vals[16] {
                Value::VecUint16(v) => {
                    let mut arr: [u16; 8] = [u16::MAX; 8];
                    for (i, x) in v.iter().enumerate() {
                        arr[i] = *x;
                    }
                    arr
                }
                _ => [u16::MAX; 8],
            },
            active_calories: vals[19].as_u16(),
            current_activity_type_intensity: vals[24].as_u8(),
            timestamp_min_8: vals[25].as_u8(),
            timestamp_16: vals[26].as_u16(),
            heart_rate: vals[27].as_u8(),
            intensity: vals[28].as_u8(),
            duration_min: vals[29].as_u16(),
            duration: vals[30].as_u32(),
            ascent: vals[31].as_u32(),
            descent: vals[32].as_u32(),
            moderate_activity_minutes: vals[33].as_u16(),
            vigorous_activity_minutes: vals[34].as_u16(),
            state,
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<Monitoring> for Message {
    fn from(m: Monitoring) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 29];
        let mut len = 0usize;
        let state = m.state;

        if m.timestamp != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 253,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.timestamp.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.device_index != typedef::DeviceIndex(u8::MAX) {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::DEVICE_INDEX,
                value: Value::Uint8(m.device_index.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.calories != u16::MAX {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.calories),
                is_expanded: false,
            };
            len += 1;
        }
        if m.distance != u32::MAX {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.distance),
                is_expanded: false,
            };
            len += 1;
        }
        if m.cycles != u32::MAX {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.cycles),
                is_expanded: false,
            };
            len += 1;
        }
        if m.active_time != u32::MAX {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.active_time),
                is_expanded: false,
            };
            len += 1;
        }
        if m.activity_type != typedef::ActivityType(u8::MAX) {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::ACTIVITY_TYPE,
                value: Value::Uint8(m.activity_type.0),
                is_expanded: is_expanded(&state, 5),
            };
            len += 1;
        }
        if m.activity_subtype != typedef::ActivitySubtype(u8::MAX) {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::ACTIVITY_SUBTYPE,
                value: Value::Uint8(m.activity_subtype.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.activity_level != typedef::ActivityLevel(u8::MAX) {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::ACTIVITY_LEVEL,
                value: Value::Uint8(m.activity_level.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.distance_16 != u16::MAX {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.distance_16),
                is_expanded: false,
            };
            len += 1;
        }
        if m.cycles_16 != u16::MAX {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.cycles_16),
                is_expanded: false,
            };
            len += 1;
        }
        if m.active_time_16 != u16::MAX {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.active_time_16),
                is_expanded: false,
            };
            len += 1;
        }
        if m.local_timestamp != typedef::LocalDateTime(u32::MAX) {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::LOCAL_DATE_TIME,
                value: Value::Uint32(m.local_timestamp.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.temperature != i16::MAX {
            arr[len] = Field {
                num: 12,
                profile_type: ProfileType::SINT16,
                value: Value::Int16(m.temperature),
                is_expanded: false,
            };
            len += 1;
        }
        if m.temperature_min != i16::MAX {
            arr[len] = Field {
                num: 14,
                profile_type: ProfileType::SINT16,
                value: Value::Int16(m.temperature_min),
                is_expanded: false,
            };
            len += 1;
        }
        if m.temperature_max != i16::MAX {
            arr[len] = Field {
                num: 15,
                profile_type: ProfileType::SINT16,
                value: Value::Int16(m.temperature_max),
                is_expanded: false,
            };
            len += 1;
        }
        if m.activity_time != [u16::MAX; 8] {
            arr[len] = Field {
                num: 16,
                profile_type: ProfileType::UINT16,
                value: Value::VecUint16(Vec::from(&m.activity_time)),
                is_expanded: false,
            };
            len += 1;
        }
        if m.active_calories != u16::MAX {
            arr[len] = Field {
                num: 19,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.active_calories),
                is_expanded: false,
            };
            len += 1;
        }
        if m.current_activity_type_intensity != u8::MAX {
            arr[len] = Field {
                num: 24,
                profile_type: ProfileType::BYTE,
                value: Value::Uint8(m.current_activity_type_intensity),
                is_expanded: false,
            };
            len += 1;
        }
        if m.timestamp_min_8 != u8::MAX {
            arr[len] = Field {
                num: 25,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.timestamp_min_8),
                is_expanded: false,
            };
            len += 1;
        }
        if m.timestamp_16 != u16::MAX {
            arr[len] = Field {
                num: 26,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.timestamp_16),
                is_expanded: false,
            };
            len += 1;
        }
        if m.heart_rate != u8::MAX {
            arr[len] = Field {
                num: 27,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.heart_rate),
                is_expanded: false,
            };
            len += 1;
        }
        if m.intensity != u8::MAX {
            arr[len] = Field {
                num: 28,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.intensity),
                is_expanded: is_expanded(&state, 28),
            };
            len += 1;
        }
        if m.duration_min != u16::MAX {
            arr[len] = Field {
                num: 29,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.duration_min),
                is_expanded: false,
            };
            len += 1;
        }
        if m.duration != u32::MAX {
            arr[len] = Field {
                num: 30,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.duration),
                is_expanded: false,
            };
            len += 1;
        }
        if m.ascent != u32::MAX {
            arr[len] = Field {
                num: 31,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.ascent),
                is_expanded: false,
            };
            len += 1;
        }
        if m.descent != u32::MAX {
            arr[len] = Field {
                num: 32,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.descent),
                is_expanded: false,
            };
            len += 1;
        }
        if m.moderate_activity_minutes != u16::MAX {
            arr[len] = Field {
                num: 33,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.moderate_activity_minutes),
                is_expanded: false,
            };
            len += 1;
        }
        if m.vigorous_activity_minutes != u16::MAX {
            arr[len] = Field {
                num: 34,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.vigorous_activity_minutes),
                is_expanded: false,
            };
            len += 1;
        }

        Message {
            header: 0,
            num: typedef::MesgNum::MONITORING,
            fields: {
                let mut fields: Vec<Field> = Vec::with_capacity(len + m.unknown_fields.len());
                fields.extend_from_slice(&arr[..len]);
                fields.extend_from_slice(&m.unknown_fields);
                fields
            },
            developer_fields: m.developer_fields,
        }
    }
}