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
// 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::*;

#[derive(Debug, Clone)]
/// SplitSummary is a SplitSummary message.
pub struct SplitSummary {
    pub message_index: typedef::MessageIndex,
    pub split_type: typedef::SplitType,
    pub num_splits: u16,
    /// Scale: 1000; Units: s
    pub total_timer_time: u32,
    /// Scale: 100; Units: m
    pub total_distance: u32,
    /// Scale: 1000; Units: m/s
    pub avg_speed: u32,
    /// Scale: 1000; Units: m/s
    pub max_speed: u32,
    /// Units: m
    pub total_ascent: u16,
    /// Units: m
    pub total_descent: u16,
    /// Units: bpm
    pub avg_heart_rate: u8,
    /// Units: bpm
    pub max_heart_rate: u8,
    /// Scale: 1000; Units: m/s
    pub avg_vert_speed: i32,
    /// Units: kcal
    pub total_calories: u32,
    /// Scale: 1000; Units: s
    pub total_moving_time: u32,
    /// 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 SplitSummary {
    /// Value's type: `u16`
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `u8`
    pub const SPLIT_TYPE: u8 = 0;
    /// Value's type: `u16`
    pub const NUM_SPLITS: u8 = 3;
    /// Value's type: `u32`; Scale: `1000`; Units: `s`
    pub const TOTAL_TIMER_TIME: u8 = 4;
    /// Value's type: `u32`; Scale: `100`; Units: `m`
    pub const TOTAL_DISTANCE: u8 = 5;
    /// Value's type: `u32`; Scale: `1000`; Units: `m/s`
    pub const AVG_SPEED: u8 = 6;
    /// Value's type: `u32`; Scale: `1000`; Units: `m/s`
    pub const MAX_SPEED: u8 = 7;
    /// Value's type: `u16`; Units: `m`
    pub const TOTAL_ASCENT: u8 = 8;
    /// Value's type: `u16`; Units: `m`
    pub const TOTAL_DESCENT: u8 = 9;
    /// Value's type: `u8`; Units: `bpm`
    pub const AVG_HEART_RATE: u8 = 10;
    /// Value's type: `u8`; Units: `bpm`
    pub const MAX_HEART_RATE: u8 = 11;
    /// Value's type: `i32`; Scale: `1000`; Units: `m/s`
    pub const AVG_VERT_SPEED: u8 = 12;
    /// Value's type: `u32`; Units: `kcal`
    pub const TOTAL_CALORIES: u8 = 13;
    /// Value's type: `u32`; Scale: `1000`; Units: `s`
    pub const TOTAL_MOVING_TIME: u8 = 77;

    /// Create new SplitSummary with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            split_type: typedef::SplitType(u8::MAX),
            num_splits: u16::MAX,
            total_timer_time: u32::MAX,
            total_distance: u32::MAX,
            avg_speed: u32::MAX,
            max_speed: u32::MAX,
            total_ascent: u16::MAX,
            total_descent: u16::MAX,
            avg_heart_rate: u8::MAX,
            max_heart_rate: u8::MAX,
            avg_vert_speed: i32::MAX,
            total_calories: u32::MAX,
            total_moving_time: u32::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// 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 SplitSummary {
        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 `total_distance` in its scaled value. It returns invalid f64 when value is valid.
    pub fn total_distance_scaled(&self) -> f64 {
        if self.total_distance == u32::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.total_distance as f64 / 100.0 - 0.0
    }

    /// Set `total_distance` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_total_distance_scaled(&mut self, v: f64) -> &mut SplitSummary {
        let unscaled = (v + 0.0) * 100.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
            self.total_distance = u32::MAX;
            return self;
        }
        self.total_distance = 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 == u32::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 SplitSummary {
        let unscaled = (v + 0.0) * 1000.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
            self.avg_speed = u32::MAX;
            return self;
        }
        self.avg_speed = unscaled as u32;
        self
    }

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

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

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

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

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

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

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

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

        const KNOWN_NUMS: [u64; 4] = [16377, 8192, 0, 4611686018427387904];
        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;
            }
            vals[field.num as usize] = &field.value;
        }

        Self {
            message_index: typedef::MessageIndex(vals[254].as_u16()),
            split_type: typedef::SplitType(vals[0].as_u8()),
            num_splits: vals[3].as_u16(),
            total_timer_time: vals[4].as_u32(),
            total_distance: vals[5].as_u32(),
            avg_speed: vals[6].as_u32(),
            max_speed: vals[7].as_u32(),
            total_ascent: vals[8].as_u16(),
            total_descent: vals[9].as_u16(),
            avg_heart_rate: vals[10].as_u8(),
            max_heart_rate: vals[11].as_u8(),
            avg_vert_speed: vals[12].as_i32(),
            total_calories: vals[13].as_u32(),
            total_moving_time: vals[77].as_u32(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

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

        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.split_type != typedef::SplitType(u8::MAX) {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::SPLIT_TYPE,
                value: Value::Uint8(m.split_type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.num_splits != u16::MAX {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.num_splits),
                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_distance != u32::MAX {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.total_distance),
                is_expanded: false,
            };
            len += 1;
        }
        if m.avg_speed != u32::MAX {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.avg_speed),
                is_expanded: false,
            };
            len += 1;
        }
        if m.max_speed != u32::MAX {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.max_speed),
                is_expanded: false,
            };
            len += 1;
        }
        if m.total_ascent != u16::MAX {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.total_ascent),
                is_expanded: false,
            };
            len += 1;
        }
        if m.total_descent != u16::MAX {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.total_descent),
                is_expanded: false,
            };
            len += 1;
        }
        if m.avg_heart_rate != u8::MAX {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.avg_heart_rate),
                is_expanded: false,
            };
            len += 1;
        }
        if m.max_heart_rate != u8::MAX {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.max_heart_rate),
                is_expanded: false,
            };
            len += 1;
        }
        if m.avg_vert_speed != i32::MAX {
            arr[len] = Field {
                num: 12,
                profile_type: ProfileType::SINT32,
                value: Value::Int32(m.avg_vert_speed),
                is_expanded: false,
            };
            len += 1;
        }
        if m.total_calories != u32::MAX {
            arr[len] = Field {
                num: 13,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.total_calories),
                is_expanded: false,
            };
            len += 1;
        }
        if m.total_moving_time != u32::MAX {
            arr[len] = Field {
                num: 77,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.total_moving_time),
                is_expanded: false,
            };
            len += 1;
        }

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