rustyfit 0.4.1

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
// 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 {
        22 | 23 => (state[num as usize >> 3] >> (num & 7)) & 1 == 1,
        _ => false,
    }
}

#[derive(Debug, Clone)]
/// Length is a Length message.
pub struct Length {
    pub message_index: typedef::MessageIndex,
    pub timestamp: typedef::DateTime,
    pub event: typedef::Event,
    pub event_type: typedef::EventType,
    pub start_time: typedef::DateTime,
    /// Scale: 1000; Units: s
    pub total_elapsed_time: u32,
    /// Scale: 1000; Units: s
    pub total_timer_time: u32,
    /// Units: strokes
    pub total_strokes: u16,
    /// Scale: 1000; Units: m/s
    pub avg_speed: u16,
    /// Units: swim_stroke
    pub swim_stroke: typedef::SwimStroke,
    /// Units: strokes/min
    pub avg_swimming_cadence: u8,
    pub event_group: u8,
    /// Units: kcal
    pub total_calories: u16,
    pub length_type: typedef::LengthType,
    pub player_score: u16,
    pub opponent_score: u16,
    /// Units: counts; stroke_type enum used as the index
    pub stroke_count: Vec<u16>,
    /// Units: counts; zone number used as the index
    pub zone_count: Vec<u16>,
    /// Scale: 100; Units: Breaths/min
    pub enhanced_avg_respiration_rate: u16,
    /// Scale: 100; Units: Breaths/min
    pub enhanced_max_respiration_rate: u16,
    pub avg_respiration_rate: u8,
    pub max_respiration_rate: u8,
    state: [u8; 3], // 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 Length {
    /// Value's type: `u16`
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `u32`
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u8`
    pub const EVENT: u8 = 0;
    /// Value's type: `u8`
    pub const EVENT_TYPE: u8 = 1;
    /// Value's type: `u32`
    pub const START_TIME: u8 = 2;
    /// Value's type: `u32`; Scale: `1000`; Units: `s`
    pub const TOTAL_ELAPSED_TIME: u8 = 3;
    /// Value's type: `u32`; Scale: `1000`; Units: `s`
    pub const TOTAL_TIMER_TIME: u8 = 4;
    /// Value's type: `u16`; Units: `strokes`
    pub const TOTAL_STROKES: u8 = 5;
    /// Value's type: `u16`; Scale: `1000`; Units: `m/s`
    pub const AVG_SPEED: u8 = 6;
    /// Value's type: `u8`; Units: `swim_stroke`
    pub const SWIM_STROKE: u8 = 7;
    /// Value's type: `u8`; Units: `strokes/min`
    pub const AVG_SWIMMING_CADENCE: u8 = 9;
    /// Value's type: `u8`
    pub const EVENT_GROUP: u8 = 10;
    /// Value's type: `u16`; Units: `kcal`
    pub const TOTAL_CALORIES: u8 = 11;
    /// Value's type: `u8`
    pub const LENGTH_TYPE: u8 = 12;
    /// Value's type: `u16`
    pub const PLAYER_SCORE: u8 = 18;
    /// Value's type: `u16`
    pub const OPPONENT_SCORE: u8 = 19;
    /// Value's type: `Vec<u16>`; Units: `counts`
    pub const STROKE_COUNT: u8 = 20;
    /// Value's type: `Vec<u16>`; Units: `counts`
    pub const ZONE_COUNT: u8 = 21;
    /// Value's type: `u16`; Scale: `100`; Units: `Breaths/min`
    pub const ENHANCED_AVG_RESPIRATION_RATE: u8 = 22;
    /// Value's type: `u16`; Scale: `100`; Units: `Breaths/min`
    pub const ENHANCED_MAX_RESPIRATION_RATE: u8 = 23;
    /// Value's type: `u8`
    pub const AVG_RESPIRATION_RATE: u8 = 24;
    /// Value's type: `u8`
    pub const MAX_RESPIRATION_RATE: u8 = 25;

    /// Create new Length with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            timestamp: typedef::DateTime(u32::MAX),
            event: typedef::Event(u8::MAX),
            event_type: typedef::EventType(u8::MAX),
            start_time: typedef::DateTime(u32::MAX),
            total_elapsed_time: u32::MAX,
            total_timer_time: u32::MAX,
            total_strokes: u16::MAX,
            avg_speed: u16::MAX,
            swim_stroke: typedef::SwimStroke(u8::MAX),
            avg_swimming_cadence: u8::MAX,
            event_group: u8::MAX,
            total_calories: u16::MAX,
            length_type: typedef::LengthType(u8::MAX),
            player_score: u16::MAX,
            opponent_score: u16::MAX,
            stroke_count: Vec::<u16>::new(),
            zone_count: Vec::<u16>::new(),
            enhanced_avg_respiration_rate: u16::MAX,
            enhanced_max_respiration_rate: u16::MAX,
            avg_respiration_rate: u8::MAX,
            max_respiration_rate: u8::MAX,
            state: [0u8; 3],
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

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

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

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

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

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

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

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

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

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

    /// Set `enhanced_max_respiration_rate` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_enhanced_max_respiration_rate_scaled(&mut self, v: f64) -> &mut Length {
        let unscaled = (v + 0.0) * 100.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
            self.enhanced_max_respiration_rate = u16::MAX;
            return self;
        }
        self.enhanced_max_respiration_rate = unscaled as u16;
        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 {
            22 | 23 => {
                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 Length {
    fn default() -> Self {
        Self::new()
    }
}

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

        const KNOWN_NUMS: [u64; 4] = [66854655, 0, 0, 6917529027641081856];
        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 < 24 {
                state[field.num as usize >> 3] |= 1 << (field.num & 7)
            }
            vals[field.num as usize] = &field.value;
        }

        Self {
            message_index: typedef::MessageIndex(vals[254].as_u16()),
            timestamp: typedef::DateTime(vals[253].as_u32()),
            event: typedef::Event(vals[0].as_u8()),
            event_type: typedef::EventType(vals[1].as_u8()),
            start_time: typedef::DateTime(vals[2].as_u32()),
            total_elapsed_time: vals[3].as_u32(),
            total_timer_time: vals[4].as_u32(),
            total_strokes: vals[5].as_u16(),
            avg_speed: vals[6].as_u16(),
            swim_stroke: typedef::SwimStroke(vals[7].as_u8()),
            avg_swimming_cadence: vals[9].as_u8(),
            event_group: vals[10].as_u8(),
            total_calories: vals[11].as_u16(),
            length_type: typedef::LengthType(vals[12].as_u8()),
            player_score: vals[18].as_u16(),
            opponent_score: vals[19].as_u16(),
            stroke_count: vals[20].as_vec_u16(),
            zone_count: vals[21].as_vec_u16(),
            enhanced_avg_respiration_rate: vals[22].as_u16(),
            enhanced_max_respiration_rate: vals[23].as_u16(),
            avg_respiration_rate: vals[24].as_u8(),
            max_respiration_rate: vals[25].as_u8(),
            state,
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

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

        if m.message_index != typedef::MessageIndex(u16::MAX) {
            arr[len] = Field {
                num: 254,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.message_index.0),
                is_expanded: false,
            };
            len += 1;
        }
        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.event != typedef::Event(u8::MAX) {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::EVENT,
                value: Value::Uint8(m.event.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.event_type != typedef::EventType(u8::MAX) {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::EVENT_TYPE,
                value: Value::Uint8(m.event_type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.start_time != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.start_time.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.total_elapsed_time != u32::MAX {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.total_elapsed_time),
                is_expanded: false,
            };
            len += 1;
        }
        if m.total_timer_time != u32::MAX {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.total_timer_time),
                is_expanded: false,
            };
            len += 1;
        }
        if m.total_strokes != u16::MAX {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.total_strokes),
                is_expanded: false,
            };
            len += 1;
        }
        if m.avg_speed != u16::MAX {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.avg_speed),
                is_expanded: false,
            };
            len += 1;
        }
        if m.swim_stroke != typedef::SwimStroke(u8::MAX) {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::SWIM_STROKE,
                value: Value::Uint8(m.swim_stroke.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.avg_swimming_cadence != u8::MAX {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.avg_swimming_cadence),
                is_expanded: false,
            };
            len += 1;
        }
        if m.event_group != u8::MAX {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.event_group),
                is_expanded: false,
            };
            len += 1;
        }
        if m.total_calories != u16::MAX {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.total_calories),
                is_expanded: false,
            };
            len += 1;
        }
        if m.length_type != typedef::LengthType(u8::MAX) {
            arr[len] = Field {
                num: 12,
                profile_type: ProfileType::LENGTH_TYPE,
                value: Value::Uint8(m.length_type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.player_score != u16::MAX {
            arr[len] = Field {
                num: 18,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.player_score),
                is_expanded: false,
            };
            len += 1;
        }
        if m.opponent_score != u16::MAX {
            arr[len] = Field {
                num: 19,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.opponent_score),
                is_expanded: false,
            };
            len += 1;
        }
        if m.stroke_count != Vec::<u16>::new() {
            arr[len] = Field {
                num: 20,
                profile_type: ProfileType::UINT16,
                value: Value::VecUint16(m.stroke_count),
                is_expanded: false,
            };
            len += 1;
        }
        if m.zone_count != Vec::<u16>::new() {
            arr[len] = Field {
                num: 21,
                profile_type: ProfileType::UINT16,
                value: Value::VecUint16(m.zone_count),
                is_expanded: false,
            };
            len += 1;
        }
        if m.enhanced_avg_respiration_rate != u16::MAX {
            arr[len] = Field {
                num: 22,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.enhanced_avg_respiration_rate),
                is_expanded: is_expanded(&state, 22),
            };
            len += 1;
        }
        if m.enhanced_max_respiration_rate != u16::MAX {
            arr[len] = Field {
                num: 23,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.enhanced_max_respiration_rate),
                is_expanded: is_expanded(&state, 23),
            };
            len += 1;
        }
        if m.avg_respiration_rate != u8::MAX {
            arr[len] = Field {
                num: 24,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.avg_respiration_rate),
                is_expanded: false,
            };
            len += 1;
        }
        if m.max_respiration_rate != u8::MAX {
            arr[len] = Field {
                num: 25,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.max_respiration_rate),
                is_expanded: false,
            };
            len += 1;
        }

        Message {
            header: 0,
            num: typedef::MesgNum::LENGTH,
            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,
        }
    }
}