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
// 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};

/// Hsa Gyroscope Data message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct HsaGyroscopeData {
    /// Units: s
    pub timestamp: typedef::DateTime,
    /// Units: ms; Millisecond resolution of the timestamp
    pub timestamp_ms: u16,
    /// Units: 1/32768 s; Sampling Interval in 32 kHz timescale
    pub sampling_interval: u16,
    /// Scale: 28.57143; Units: deg/s; X-Axis Measurement
    pub gyro_x: Vec<i16>,
    /// Scale: 28.57143; Units: deg/s; Y-Axis Measurement
    pub gyro_y: Vec<i16>,
    /// Scale: 28.57143; Units: deg/s; Z-Axis Measurement
    pub gyro_z: Vec<i16>,
    /// Units: 1/32768 s; 32 kHz timestamp
    pub timestamp_32k: 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 HsaGyroscopeData {
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::DateTime; Units: `s`
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16; Units: `ms`
    pub const TIMESTAMP_MS: u8 = 0;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16; Units: `1/32768 s`
    pub const SAMPLING_INTERVAL: u8 = 1;
    /// Value's type: `Vec<i16>`; FitBaseType::SINT16; ProfileType::Sint16; Scale: `28.57143`; Units: `deg/s`
    pub const GYRO_X: u8 = 2;
    /// Value's type: `Vec<i16>`; FitBaseType::SINT16; ProfileType::Sint16; Scale: `28.57143`; Units: `deg/s`
    pub const GYRO_Y: u8 = 3;
    /// Value's type: `Vec<i16>`; FitBaseType::SINT16; ProfileType::Sint16; Scale: `28.57143`; Units: `deg/s`
    pub const GYRO_Z: u8 = 4;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::Uint32; Units: `1/32768 s`
    pub const TIMESTAMP_32K: u8 = 5;

    /// Create new HsaGyroscopeData with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            timestamp: typedef::DateTime(u32::MAX),
            timestamp_ms: u16::MAX,
            sampling_interval: u16::MAX,
            gyro_x: Vec::new(),
            gyro_y: Vec::new(),
            gyro_z: Vec::new(),
            timestamp_32k: u32::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

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

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

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

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

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

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

    fn count_valid_fields(&self) -> usize {
        (self.timestamp.0 != u32::MAX) as usize
            + (self.timestamp_ms != u16::MAX) as usize
            + (self.sampling_interval != u16::MAX) as usize
            + (!self.gyro_x.is_empty()) as usize
            + (!self.gyro_y.is_empty()) as usize
            + (!self.gyro_z.is_empty()) as usize
            + (self.timestamp_32k != u32::MAX) as usize
    }
}

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

impl From<&Message> for HsaGyroscopeData {
    /// from creates new HsaGyroscopeData struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [63, 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 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 {
                253 => v.timestamp = typedef::DateTime(field.value.as_u32()),
                0 => v.timestamp_ms = field.value.as_u16(),
                1 => v.sampling_interval = field.value.as_u16(),
                2 => v.gyro_x = field.value.to_vec_i16(),
                3 => v.gyro_y = field.value.to_vec_i16(),
                4 => v.gyro_z = field.value.to_vec_i16(),
                5 => v.timestamp_32k = field.value.as_u32(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if m.timestamp.0 != u32::MAX {
            fields.push(Field {
                num: 253,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.timestamp.0),
                is_expanded: false,
            });
        };
        if m.timestamp_ms != u16::MAX {
            fields.push(Field {
                num: 0,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.timestamp_ms),
                is_expanded: false,
            });
        };
        if m.sampling_interval != u16::MAX {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.sampling_interval),
                is_expanded: false,
            });
        };
        if !m.gyro_x.is_empty() {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::SINT16,
                value: Value::VecInt16(m.gyro_x),
                is_expanded: false,
            });
        };
        if !m.gyro_y.is_empty() {
            fields.push(Field {
                num: 3,
                base_type: FitBaseType::SINT16,
                value: Value::VecInt16(m.gyro_y),
                is_expanded: false,
            });
        };
        if !m.gyro_z.is_empty() {
            fields.push(Field {
                num: 4,
                base_type: FitBaseType::SINT16,
                value: Value::VecInt16(m.gyro_z),
                is_expanded: false,
            });
        };
        if m.timestamp_32k != u32::MAX {
            fields.push(Field {
                num: 5,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.timestamp_32k),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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

#[cfg(feature = "serde")]
impl Serialize for HsaGyroscopeData {
    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("HsaGyroscopeData", n)?;
        if let Some(v) = self.timestamp.unix_timestamp() {
            state.serialize_field("timestamp", &v)?;
        }
        if self.timestamp_ms != u16::MAX {
            state.serialize_field("timestamp_ms", &self.timestamp_ms)?;
        }
        if self.sampling_interval != u16::MAX {
            state.serialize_field("sampling_interval", &self.sampling_interval)?;
        }
        if let Some(v) = self.gyro_x_scaled() {
            state.serialize_field("gyro_x", &v)?;
        }
        if let Some(v) = self.gyro_y_scaled() {
            state.serialize_field("gyro_y", &v)?;
        }
        if let Some(v) = self.gyro_z_scaled() {
            state.serialize_field("gyro_z", &v)?;
        }
        if self.timestamp_32k != u32::MAX {
            state.serialize_field("timestamp_32k", &self.timestamp_32k)?;
        }
        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 {
    timestamp: Option<i64>,
    timestamp_ms: u16,
    sampling_interval: u16,
    gyro_x: Vec<f64>,
    gyro_y: Vec<f64>,
    gyro_z: Vec<f64>,
    timestamp_32k: u32,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for HsaGyroscopeData {
    fn from(m: De) -> Self {
        Self {
            timestamp: m.timestamp.map_or_else(
                || typedef::DateTime(u32::MAX),
                typedef::DateTime::from_unix_timestamp,
            ),
            timestamp_ms: m.timestamp_ms,
            sampling_interval: m.sampling_interval,
            gyro_x: {
                if m.gyro_x.is_empty() {
                    Vec::new()
                } else {
                    let mut vals = Vec::with_capacity(m.gyro_x.len());
                    for &x in m.gyro_x.iter() {
                        let unscaled = (x + 0.0) * 28.57143;
                        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64
                        {
                            vals.push(i16::MAX);
                            continue;
                        }
                        vals.push(unscaled as i16);
                    }
                    vals
                }
            },
            gyro_y: {
                if m.gyro_y.is_empty() {
                    Vec::new()
                } else {
                    let mut vals = Vec::with_capacity(m.gyro_y.len());
                    for &x in m.gyro_y.iter() {
                        let unscaled = (x + 0.0) * 28.57143;
                        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64
                        {
                            vals.push(i16::MAX);
                            continue;
                        }
                        vals.push(unscaled as i16);
                    }
                    vals
                }
            },
            gyro_z: {
                if m.gyro_z.is_empty() {
                    Vec::new()
                } else {
                    let mut vals = Vec::with_capacity(m.gyro_z.len());
                    for &x in m.gyro_z.iter() {
                        let unscaled = (x + 0.0) * 28.57143;
                        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64
                        {
                            vals.push(i16::MAX);
                            continue;
                        }
                        vals.push(unscaled as i16);
                    }
                    vals
                }
            },
            timestamp_32k: m.timestamp_32k,
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            timestamp: None,
            timestamp_ms: u16::MAX,
            sampling_interval: u16::MAX,
            gyro_x: Vec::new(),
            gyro_y: Vec::new(),
            gyro_z: Vec::new(),
            timestamp_32k: u32::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}