rustyfit 0.8.1

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.

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

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

#[derive(Debug, Clone)]
/// HsaGyroscopeData is a HsaGyroscopeData message.
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`; Units: `s`
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u16`; Units: `ms`
    pub const TIMESTAMP_MS: u8 = 0;
    /// Value's type: `u16`; Units: `1/32768 s`
    pub const SAMPLING_INTERVAL: u8 = 1;
    /// Value's type: `Vec<i16>`; Scale: `28.57143`; Units: `deg/s`
    pub const GYRO_X: u8 = 2;
    /// Value's type: `Vec<i16>`; Scale: `28.57143`; Units: `deg/s`
    pub const GYRO_Y: u8 = 3;
    /// Value's type: `Vec<i16>`; Scale: `28.57143`; Units: `deg/s`
    pub const GYRO_Z: u8 = 4;
    /// Value's type: `u32`; 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 invalid f64 when value is valid.
    pub fn gyro_x_scaled(&self) -> Vec<f64> {
        if self.gyro_x.is_empty() {
            return Vec::new();
        }
        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)
        }
        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: &Vec<f64>) -> &mut HsaGyroscopeData {
        if v.is_empty() {
            self.gyro_x = Vec::new();
            return self;
        }
        self.gyro_x = Vec::with_capacity(v.len());
        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 invalid f64 when value is valid.
    pub fn gyro_y_scaled(&self) -> Vec<f64> {
        if self.gyro_y.is_empty() {
            return Vec::new();
        }
        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)
        }
        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: &Vec<f64>) -> &mut HsaGyroscopeData {
        if v.is_empty() {
            self.gyro_y = Vec::new();
            return self;
        }
        self.gyro_y = Vec::with_capacity(v.len());
        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 invalid f64 when value is valid.
    pub fn gyro_z_scaled(&self) -> Vec<f64> {
        if self.gyro_z.is_empty() {
            return Vec::new();
        }
        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)
        }
        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: &Vec<f64>) -> &mut HsaGyroscopeData {
        if v.is_empty() {
            self.gyro_z = Vec::new();
            return self;
        }
        self.gyro_z = Vec::with_capacity(v.len());
        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 != typedef::DateTime(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 != typedef::DateTime(u32::MAX) {
            fields.push(Field {
                num: 253,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.timestamp.0),
                is_expanded: false,
            });
        };
        if m.timestamp_ms != u16::MAX {
            fields.push(Field {
                num: 0,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.timestamp_ms),
                is_expanded: false,
            });
        };
        if m.sampling_interval != u16::MAX {
            fields.push(Field {
                num: 1,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.sampling_interval),
                is_expanded: false,
            });
        };
        if !m.gyro_x.is_empty() {
            fields.push(Field {
                num: 2,
                profile_type: ProfileType::SINT16,
                value: Value::VecInt16(m.gyro_x),
                is_expanded: false,
            });
        };
        if !m.gyro_y.is_empty() {
            fields.push(Field {
                num: 3,
                profile_type: ProfileType::SINT16,
                value: Value::VecInt16(m.gyro_y),
                is_expanded: false,
            });
        };
        if !m.gyro_z.is_empty() {
            fields.push(Field {
                num: 4,
                profile_type: ProfileType::SINT16,
                value: Value::VecInt16(m.gyro_z),
                is_expanded: false,
            });
        };
        if m.timestamp_32k != u32::MAX {
            fields.push(Field {
                num: 5,
                profile_type: ProfileType::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,
        }
    }
}