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)]
/// OneDSensorCalibration is a OneDSensorCalibration message.
pub struct OneDSensorCalibration {
    /// Units: s; Whole second part of the timestamp
    pub timestamp: typedef::DateTime,
    /// Indicates which sensor the calibration is for
    pub sensor_type: typedef::SensorType,
    /// Calibration factor used to convert from raw ADC value to degrees, g, etc.
    pub calibration_factor: u32,
    /// Units: counts; Calibration factor divisor
    pub calibration_divisor: u32,
    /// Level shift value used to shift the ADC value back into range
    pub level_shift: u32,
    /// Internal Calibration factor
    pub offset_cal: i32,
    /// 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 OneDSensorCalibration {
    /// Value's type: `u32`; Units: `s`
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u8`
    pub const SENSOR_TYPE: u8 = 0;
    /// Value's type: `u32`
    pub const CALIBRATION_FACTOR: u8 = 1;
    /// Value's type: `u32`; Units: `counts`
    pub const CALIBRATION_DIVISOR: u8 = 2;
    /// Value's type: `u32`
    pub const LEVEL_SHIFT: u8 = 3;
    /// Value's type: `i32`
    pub const OFFSET_CAL: u8 = 4;

    /// Create new OneDSensorCalibration with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            timestamp: typedef::DateTime(u32::MAX),
            sensor_type: typedef::SensorType(u8::MAX),
            calibration_factor: u32::MAX,
            calibration_divisor: u32::MAX,
            level_shift: u32::MAX,
            offset_cal: i32::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (self.timestamp != typedef::DateTime(u32::MAX)) as usize
            + (self.sensor_type != typedef::SensorType(u8::MAX)) as usize
            + (self.calibration_factor != u32::MAX) as usize
            + (self.calibration_divisor != u32::MAX) as usize
            + (self.level_shift != u32::MAX) as usize
            + (self.offset_cal != i32::MAX) as usize
    }
}

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

impl From<&Message> for OneDSensorCalibration {
    /// from creates new OneDSensorCalibration struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [31, 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.sensor_type = typedef::SensorType(field.value.as_u8()),
                1 => v.calibration_factor = field.value.as_u32(),
                2 => v.calibration_divisor = field.value.as_u32(),
                3 => v.level_shift = field.value.as_u32(),
                4 => v.offset_cal = field.value.as_i32(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

impl From<OneDSensorCalibration> for Message {
    fn from(m: OneDSensorCalibration) -> 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.sensor_type != typedef::SensorType(u8::MAX) {
            fields.push(Field {
                num: 0,
                profile_type: ProfileType::SENSOR_TYPE,
                value: Value::Uint8(m.sensor_type.0),
                is_expanded: false,
            });
        };
        if m.calibration_factor != u32::MAX {
            fields.push(Field {
                num: 1,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.calibration_factor),
                is_expanded: false,
            });
        };
        if m.calibration_divisor != u32::MAX {
            fields.push(Field {
                num: 2,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.calibration_divisor),
                is_expanded: false,
            });
        };
        if m.level_shift != u32::MAX {
            fields.push(Field {
                num: 3,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.level_shift),
                is_expanded: false,
            });
        };
        if m.offset_cal != i32::MAX {
            fields.push(Field {
                num: 4,
                profile_type: ProfileType::SINT32,
                value: Value::Int32(m.offset_cal),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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