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)]
/// MonitoringInfo is a MonitoringInfo message.
pub struct MonitoringInfo {
    /// Units: s
    pub timestamp: typedef::DateTime,
    /// Units: s; Use to convert activity timestamps to local time if device does not support time zone and daylight savings time correction.
    pub local_timestamp: typedef::LocalDateTime,
    pub activity_type: Vec<typedef::ActivityType>,
    /// Scale: 5000; Units: m/cycle; Indexed by activity_type
    pub cycles_to_distance: Vec<u16>,
    /// Scale: 5000; Units: kcal/cycle; Indexed by activity_type
    pub cycles_to_calories: Vec<u16>,
    /// Units: kcal / day
    pub resting_metabolic_rate: u16,
    /// 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 MonitoringInfo {
    /// Value's type: `u32`; Units: `s`
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u32`; Units: `s`
    pub const LOCAL_TIMESTAMP: u8 = 0;
    /// Value's type: `Vec<u8>`
    pub const ACTIVITY_TYPE: u8 = 1;
    /// Value's type: `Vec<u16>`; Scale: `5000`; Units: `m/cycle`
    pub const CYCLES_TO_DISTANCE: u8 = 3;
    /// Value's type: `Vec<u16>`; Scale: `5000`; Units: `kcal/cycle`
    pub const CYCLES_TO_CALORIES: u8 = 4;
    /// Value's type: `u16`; Units: `kcal / day`
    pub const RESTING_METABOLIC_RATE: u8 = 5;

    /// Create new MonitoringInfo with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            timestamp: typedef::DateTime(u32::MAX),
            local_timestamp: typedef::LocalDateTime(u32::MAX),
            activity_type: Vec::<typedef::ActivityType>::new(),
            cycles_to_distance: Vec::<u16>::new(),
            cycles_to_calories: Vec::<u16>::new(),
            resting_metabolic_rate: u16::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

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

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

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

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

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

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

        const KNOWN_NUMS: [u64; 4] = [59, 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()),
            local_timestamp: typedef::LocalDateTime(vals[0].as_u32()),
            activity_type: match &vals[1] {
                Value::VecUint8(v) => {
                    let mut vs = Vec::with_capacity(v.len());
                    for x in v {
                        vs.push(typedef::ActivityType(*x))
                    }
                    vs
                }
                _ => Vec::new(),
            },
            cycles_to_distance: vals[3].as_vec_u16(),
            cycles_to_calories: vals[4].as_vec_u16(),
            resting_metabolic_rate: vals[5].as_u16(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<MonitoringInfo> for Message {
    fn from(m: MonitoringInfo) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 6];
        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.local_timestamp != typedef::LocalDateTime(u32::MAX) {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::LOCAL_DATE_TIME,
                value: Value::Uint32(m.local_timestamp.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.activity_type != Vec::<typedef::ActivityType>::new() {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::ACTIVITY_TYPE,
                value: Value::VecUint8({
                    let mut v = Vec::with_capacity(m.activity_type.len());
                    for x in &m.activity_type {
                        v.push(x.0)
                    }
                    v
                }),
                is_expanded: false,
            };
            len += 1;
        }
        if m.cycles_to_distance != Vec::<u16>::new() {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::UINT16,
                value: Value::VecUint16(m.cycles_to_distance),
                is_expanded: false,
            };
            len += 1;
        }
        if m.cycles_to_calories != Vec::<u16>::new() {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT16,
                value: Value::VecUint16(m.cycles_to_calories),
                is_expanded: false,
            };
            len += 1;
        }
        if m.resting_metabolic_rate != u16::MAX {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.resting_metabolic_rate),
                is_expanded: false,
            };
            len += 1;
        }

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