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

/// Gyroscope Data message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct GyroscopeData {
    /// Units: s; Whole second part of the timestamp
    pub timestamp: typedef::DateTime,
    /// Units: ms; Millisecond part of the timestamp.
    pub timestamp_ms: u16,
    /// Units: ms; Each time in the array describes the time at which the gyro sample with the corresponding index was taken. Limited to 30 samples in each message. The samples may span across seconds. Array size must match the number of samples in gyro_x and gyro_y and gyro_z
    pub sample_time_offset: Vec<u16>,
    /// Units: counts; These are the raw ADC reading. Maximum number of samples is 30 in each message. The samples may span across seconds. A conversion will need to be done on this data once read.
    pub gyro_x: Vec<u16>,
    /// Units: counts; These are the raw ADC reading. Maximum number of samples is 30 in each message. The samples may span across seconds. A conversion will need to be done on this data once read.
    pub gyro_y: Vec<u16>,
    /// Units: counts; These are the raw ADC reading. Maximum number of samples is 30 in each message. The samples may span across seconds. A conversion will need to be done on this data once read.
    pub gyro_z: Vec<u16>,
    /// Units: deg/s; Calibrated gyro reading
    pub calibrated_gyro_x: Vec<f32>,
    /// Units: deg/s; Calibrated gyro reading
    pub calibrated_gyro_y: Vec<f32>,
    /// Units: deg/s; Calibrated gyro reading
    pub calibrated_gyro_z: Vec<f32>,
    /// 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 GyroscopeData {
    /// 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: `Vec<u16>`; FitBaseType::UINT16; ProfileType::Uint16; Units: `ms`
    pub const SAMPLE_TIME_OFFSET: u8 = 1;
    /// Value's type: `Vec<u16>`; FitBaseType::UINT16; ProfileType::Uint16; Units: `counts`
    pub const GYRO_X: u8 = 2;
    /// Value's type: `Vec<u16>`; FitBaseType::UINT16; ProfileType::Uint16; Units: `counts`
    pub const GYRO_Y: u8 = 3;
    /// Value's type: `Vec<u16>`; FitBaseType::UINT16; ProfileType::Uint16; Units: `counts`
    pub const GYRO_Z: u8 = 4;
    /// Value's type: `Vec<f32>`; FitBaseType::FLOAT32; ProfileType::Float32; Units: `deg/s`
    pub const CALIBRATED_GYRO_X: u8 = 5;
    /// Value's type: `Vec<f32>`; FitBaseType::FLOAT32; ProfileType::Float32; Units: `deg/s`
    pub const CALIBRATED_GYRO_Y: u8 = 6;
    /// Value's type: `Vec<f32>`; FitBaseType::FLOAT32; ProfileType::Float32; Units: `deg/s`
    pub const CALIBRATED_GYRO_Z: u8 = 7;

    /// Create new GyroscopeData 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,
            sample_time_offset: Vec::new(),
            gyro_x: Vec::new(),
            gyro_y: Vec::new(),
            gyro_z: Vec::new(),
            calibrated_gyro_x: Vec::new(),
            calibrated_gyro_y: Vec::new(),
            calibrated_gyro_z: Vec::new(),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

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

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

impl From<&Message> for GyroscopeData {
    /// from creates new GyroscopeData struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [255, 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.sample_time_offset = field.value.to_vec_u16(),
                2 => v.gyro_x = field.value.to_vec_u16(),
                3 => v.gyro_y = field.value.to_vec_u16(),
                4 => v.gyro_z = field.value.to_vec_u16(),
                5 => v.calibrated_gyro_x = field.value.to_vec_f32(),
                6 => v.calibrated_gyro_y = field.value.to_vec_f32(),
                7 => v.calibrated_gyro_z = field.value.to_vec_f32(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

impl From<GyroscopeData> for Message {
    fn from(m: GyroscopeData) -> 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.sample_time_offset.is_empty() {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::UINT16,
                value: Value::VecUint16(m.sample_time_offset),
                is_expanded: false,
            });
        };
        if !m.gyro_x.is_empty() {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::UINT16,
                value: Value::VecUint16(m.gyro_x),
                is_expanded: false,
            });
        };
        if !m.gyro_y.is_empty() {
            fields.push(Field {
                num: 3,
                base_type: FitBaseType::UINT16,
                value: Value::VecUint16(m.gyro_y),
                is_expanded: false,
            });
        };
        if !m.gyro_z.is_empty() {
            fields.push(Field {
                num: 4,
                base_type: FitBaseType::UINT16,
                value: Value::VecUint16(m.gyro_z),
                is_expanded: false,
            });
        };
        if !m.calibrated_gyro_x.is_empty() {
            fields.push(Field {
                num: 5,
                base_type: FitBaseType::FLOAT32,
                value: Value::VecFloat32(m.calibrated_gyro_x),
                is_expanded: false,
            });
        };
        if !m.calibrated_gyro_y.is_empty() {
            fields.push(Field {
                num: 6,
                base_type: FitBaseType::FLOAT32,
                value: Value::VecFloat32(m.calibrated_gyro_y),
                is_expanded: false,
            });
        };
        if !m.calibrated_gyro_z.is_empty() {
            fields.push(Field {
                num: 7,
                base_type: FitBaseType::FLOAT32,
                value: Value::VecFloat32(m.calibrated_gyro_z),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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

#[cfg(feature = "serde")]
impl Serialize for GyroscopeData {
    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("GyroscopeData", 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.sample_time_offset.is_empty() {
            state.serialize_field("sample_time_offset", &self.sample_time_offset)?;
        }
        if !self.gyro_x.is_empty() {
            state.serialize_field("gyro_x", &self.gyro_x)?;
        }
        if !self.gyro_y.is_empty() {
            state.serialize_field("gyro_y", &self.gyro_y)?;
        }
        if !self.gyro_z.is_empty() {
            state.serialize_field("gyro_z", &self.gyro_z)?;
        }
        if !self.calibrated_gyro_x.is_empty() {
            state.serialize_field("calibrated_gyro_x", &self.calibrated_gyro_x)?;
        }
        if !self.calibrated_gyro_y.is_empty() {
            state.serialize_field("calibrated_gyro_y", &self.calibrated_gyro_y)?;
        }
        if !self.calibrated_gyro_z.is_empty() {
            state.serialize_field("calibrated_gyro_z", &self.calibrated_gyro_z)?;
        }
        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,
    sample_time_offset: Vec<u16>,
    gyro_x: Vec<u16>,
    gyro_y: Vec<u16>,
    gyro_z: Vec<u16>,
    calibrated_gyro_x: Vec<f32>,
    calibrated_gyro_y: Vec<f32>,
    calibrated_gyro_z: Vec<f32>,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for GyroscopeData {
    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,
            sample_time_offset: m.sample_time_offset,
            gyro_x: m.gyro_x,
            gyro_y: m.gyro_y,
            gyro_z: m.gyro_z,
            calibrated_gyro_x: m.calibrated_gyro_x,
            calibrated_gyro_y: m.calibrated_gyro_y,
            calibrated_gyro_z: m.calibrated_gyro_z,
            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,
            sample_time_offset: Vec::new(),
            gyro_x: Vec::new(),
            gyro_y: Vec::new(),
            gyro_z: Vec::new(),
            calibrated_gyro_x: Vec::new(),
            calibrated_gyro_y: Vec::new(),
            calibrated_gyro_z: Vec::new(),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}