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)]
/// MaxMetData is a MaxMetData message.
pub struct MaxMetData {
    /// Time maxMET and vo2 were calculated
    pub update_time: typedef::DateTime,
    /// Scale: 10; Units: mL/kg/min
    pub vo2_max: u16,
    pub sport: typedef::Sport,
    pub sub_sport: typedef::SubSport,
    pub max_met_category: typedef::MaxMetCategory,
    /// Indicates if calibrated data was used in the calculation
    pub calibrated_data: typedef::Bool,
    /// Indicates if the estimate was obtained using a chest strap or wrist heart rate
    pub hr_source: typedef::MaxMetHeartRateSource,
    /// Indidcates if the estimate was obtained using onboard GPS or connected GPS
    pub speed_source: typedef::MaxMetSpeedSource,
    /// 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 MaxMetData {
    /// Value's type: `u32`
    pub const UPDATE_TIME: u8 = 0;
    /// Value's type: `u16`; Scale: `10`; Units: `mL/kg/min`
    pub const VO2_MAX: u8 = 2;
    /// Value's type: `u8`
    pub const SPORT: u8 = 5;
    /// Value's type: `u8`
    pub const SUB_SPORT: u8 = 6;
    /// Value's type: `u8`
    pub const MAX_MET_CATEGORY: u8 = 8;
    /// Value's type: `u8`
    pub const CALIBRATED_DATA: u8 = 9;
    /// Value's type: `u8`
    pub const HR_SOURCE: u8 = 12;
    /// Value's type: `u8`
    pub const SPEED_SOURCE: u8 = 13;

    /// Create new MaxMetData with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            update_time: typedef::DateTime(u32::MAX),
            vo2_max: u16::MAX,
            sport: typedef::Sport(u8::MAX),
            sub_sport: typedef::SubSport(u8::MAX),
            max_met_category: typedef::MaxMetCategory(u8::MAX),
            calibrated_data: typedef::Bool(u8::MAX),
            hr_source: typedef::MaxMetHeartRateSource(u8::MAX),
            speed_source: typedef::MaxMetSpeedSource(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `vo2_max` in its scaled value. It returns invalid f64 when value is valid.
    pub fn vo2_max_scaled(&self) -> f64 {
        if self.vo2_max == u16::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.vo2_max as f64 / 10.0 - 0.0
    }

    /// Set `vo2_max` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_vo2_max_scaled(&mut self, v: f64) -> &mut MaxMetData {
        let unscaled = (v + 0.0) * 10.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
            self.vo2_max = u16::MAX;
            return self;
        }
        self.vo2_max = unscaled as u16;
        self
    }
}

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

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

        const KNOWN_NUMS: [u64; 4] = [13157, 0, 0, 0];
        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 {
            update_time: typedef::DateTime(vals[0].as_u32()),
            vo2_max: vals[2].as_u16(),
            sport: typedef::Sport(vals[5].as_u8()),
            sub_sport: typedef::SubSport(vals[6].as_u8()),
            max_met_category: typedef::MaxMetCategory(vals[8].as_u8()),
            calibrated_data: typedef::Bool(vals[9].as_u8()),
            hr_source: typedef::MaxMetHeartRateSource(vals[12].as_u8()),
            speed_source: typedef::MaxMetSpeedSource(vals[13].as_u8()),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

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

        if m.update_time != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.update_time.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.vo2_max != u16::MAX {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.vo2_max),
                is_expanded: false,
            };
            len += 1;
        }
        if m.sport != typedef::Sport(u8::MAX) {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::SPORT,
                value: Value::Uint8(m.sport.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.sub_sport != typedef::SubSport(u8::MAX) {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::SUB_SPORT,
                value: Value::Uint8(m.sub_sport.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.max_met_category != typedef::MaxMetCategory(u8::MAX) {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::MAX_MET_CATEGORY,
                value: Value::Uint8(m.max_met_category.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.calibrated_data != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.calibrated_data.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.hr_source != typedef::MaxMetHeartRateSource(u8::MAX) {
            arr[len] = Field {
                num: 12,
                profile_type: ProfileType::MAX_MET_HEART_RATE_SOURCE,
                value: Value::Uint8(m.hr_source.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.speed_source != typedef::MaxMetSpeedSource(u8::MAX) {
            arr[len] = Field {
                num: 13,
                profile_type: ProfileType::MAX_MET_SPEED_SOURCE,
                value: Value::Uint8(m.speed_source.0),
                is_expanded: false,
            };
            len += 1;
        }

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