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)]
/// ZonesTarget is a ZonesTarget message.
pub struct ZonesTarget {
    pub max_heart_rate: u8,
    pub threshold_heart_rate: u8,
    pub functional_threshold_power: u16,
    pub hr_calc_type: typedef::HrZoneCalc,
    pub pwr_calc_type: typedef::PwrZoneCalc,
    /// 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 ZonesTarget {
    /// Value's type: `u8`
    pub const MAX_HEART_RATE: u8 = 1;
    /// Value's type: `u8`
    pub const THRESHOLD_HEART_RATE: u8 = 2;
    /// Value's type: `u16`
    pub const FUNCTIONAL_THRESHOLD_POWER: u8 = 3;
    /// Value's type: `u8`
    pub const HR_CALC_TYPE: u8 = 5;
    /// Value's type: `u8`
    pub const PWR_CALC_TYPE: u8 = 7;

    /// Create new ZonesTarget with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            max_heart_rate: u8::MAX,
            threshold_heart_rate: u8::MAX,
            functional_threshold_power: u16::MAX,
            hr_calc_type: typedef::HrZoneCalc(u8::MAX),
            pwr_calc_type: typedef::PwrZoneCalc(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (self.max_heart_rate != u8::MAX) as usize
            + (self.threshold_heart_rate != u8::MAX) as usize
            + (self.functional_threshold_power != u16::MAX) as usize
            + (self.hr_calc_type != typedef::HrZoneCalc(u8::MAX)) as usize
            + (self.pwr_calc_type != typedef::PwrZoneCalc(u8::MAX)) as usize
    }
}

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

impl From<&Message> for ZonesTarget {
    /// from creates new ZonesTarget struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [174, 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 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 {
                1 => v.max_heart_rate = field.value.as_u8(),
                2 => v.threshold_heart_rate = field.value.as_u8(),
                3 => v.functional_threshold_power = field.value.as_u16(),
                5 => v.hr_calc_type = typedef::HrZoneCalc(field.value.as_u8()),
                7 => v.pwr_calc_type = typedef::PwrZoneCalc(field.value.as_u8()),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

impl From<ZonesTarget> for Message {
    fn from(m: ZonesTarget) -> Self {
        let mut fields =
            Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());

        if m.max_heart_rate != u8::MAX {
            fields.push(Field {
                num: 1,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.max_heart_rate),
                is_expanded: false,
            });
        };
        if m.threshold_heart_rate != u8::MAX {
            fields.push(Field {
                num: 2,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.threshold_heart_rate),
                is_expanded: false,
            });
        };
        if m.functional_threshold_power != u16::MAX {
            fields.push(Field {
                num: 3,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.functional_threshold_power),
                is_expanded: false,
            });
        };
        if m.hr_calc_type != typedef::HrZoneCalc(u8::MAX) {
            fields.push(Field {
                num: 5,
                profile_type: ProfileType::HR_ZONE_CALC,
                value: Value::Uint8(m.hr_calc_type.0),
                is_expanded: false,
            });
        };
        if m.pwr_calc_type != typedef::PwrZoneCalc(u8::MAX) {
            fields.push(Field {
                num: 7,
                profile_type: ProfileType::PWR_ZONE_CALC,
                value: Value::Uint8(m.pwr_calc_type.0),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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