rustyfit 0.5.0

This project hosts the Rust implementation for The Flexible and Interoperable Data Transfer (FIT) Protocol
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::comparison_to_empty, clippy::manual_range_patterns)]

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

#[derive(Debug, Clone)]
/// HsaAccelerometerData is a HsaAccelerometerData message.
pub struct HsaAccelerometerData {
    /// Units: s
    pub timestamp: typedef::DateTime,
    /// Units: ms; Millisecond resolution of the timestamp
    pub timestamp_ms: u16,
    /// Units: ms; Sampling Interval in Milliseconds
    pub sampling_interval: u16,
    /// Scale: 1.024; Units: mG; X-Axis Measurement
    pub accel_x: Vec<i16>,
    /// Scale: 1.024; Units: mG; Y-Axis Measurement
    pub accel_y: Vec<i16>,
    /// Scale: 1.024; Units: mG; Z-Axis Measurement
    pub accel_z: Vec<i16>,
    /// 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 HsaAccelerometerData {
    /// 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: `ms`
    pub const SAMPLING_INTERVAL: u8 = 1;
    /// Value's type: `Vec<i16>`; Scale: `1.024`; Units: `mG`
    pub const ACCEL_X: u8 = 2;
    /// Value's type: `Vec<i16>`; Scale: `1.024`; Units: `mG`
    pub const ACCEL_Y: u8 = 3;
    /// Value's type: `Vec<i16>`; Scale: `1.024`; Units: `mG`
    pub const ACCEL_Z: u8 = 4;
    /// Value's type: `u32`
    pub const TIMESTAMP_32K: u8 = 5;

    /// Create new HsaAccelerometerData 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,
            accel_x: Vec::<i16>::new(),
            accel_y: Vec::<i16>::new(),
            accel_z: Vec::<i16>::new(),
            timestamp_32k: u32::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `accel_x` in its scaled value. It returns invalid f64 when value is valid.
    pub fn accel_x_scaled(&self) -> Vec<f64> {
        if self.accel_x == Vec::<i16>::new() {
            return Vec::new();
        }
        let mut v = Vec::with_capacity(self.accel_x.len());
        for &x in &self.accel_x {
            v.push(x as f64 / 1.024 - 0.0)
        }
        v
    }

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

    /// Returns `accel_y` in its scaled value. It returns invalid f64 when value is valid.
    pub fn accel_y_scaled(&self) -> Vec<f64> {
        if self.accel_y == Vec::<i16>::new() {
            return Vec::new();
        }
        let mut v = Vec::with_capacity(self.accel_y.len());
        for &x in &self.accel_y {
            v.push(x as f64 / 1.024 - 0.0)
        }
        v
    }

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

    /// Returns `accel_z` in its scaled value. It returns invalid f64 when value is valid.
    pub fn accel_z_scaled(&self) -> Vec<f64> {
        if self.accel_z == Vec::<i16>::new() {
            return Vec::new();
        }
        let mut v = Vec::with_capacity(self.accel_z.len());
        for &x in &self.accel_z {
            v.push(x as f64 / 1.024 - 0.0)
        }
        v
    }

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

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

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

        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 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 {
            timestamp: typedef::DateTime(vals[253].as_u32()),
            timestamp_ms: vals[0].as_u16(),
            sampling_interval: vals[1].as_u16(),
            accel_x: vals[2].as_vec_i16(),
            accel_y: vals[3].as_vec_i16(),
            accel_z: vals[4].as_vec_i16(),
            timestamp_32k: vals[5].as_u32(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

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

        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.timestamp_ms != u16::MAX {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.timestamp_ms),
                is_expanded: false,
            };
            len += 1;
        }
        if m.sampling_interval != u16::MAX {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.sampling_interval),
                is_expanded: false,
            };
            len += 1;
        }
        if m.accel_x != Vec::<i16>::new() {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::SINT16,
                value: Value::VecInt16(m.accel_x),
                is_expanded: false,
            };
            len += 1;
        }
        if m.accel_y != Vec::<i16>::new() {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::SINT16,
                value: Value::VecInt16(m.accel_y),
                is_expanded: false,
            };
            len += 1;
        }
        if m.accel_z != Vec::<i16>::new() {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::SINT16,
                value: Value::VecInt16(m.accel_z),
                is_expanded: false,
            };
            len += 1;
        }
        if m.timestamp_32k != u32::MAX {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.timestamp_32k),
                is_expanded: false,
            };
            len += 1;
        }

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