rustyfit 0.10.0

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.

use crate::profile::typedef::{self, FitBaseType};
use crate::proto::*;
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};

/// Zones Target message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
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`; FitBaseType::UINT8; ProfileType::Uint8
    pub const MAX_HEART_RATE: u8 = 1;
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::Uint8
    pub const THRESHOLD_HEART_RATE: u8 = 2;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16
    pub const FUNCTIONAL_THRESHOLD_POWER: u8 = 3;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::HrZoneCalc
    pub const HR_CALC_TYPE: u8 = 5;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::PwrZoneCalc
    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.0 != u8::MAX) as usize
            + (self.pwr_calc_type.0 != 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,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.max_heart_rate),
                is_expanded: false,
            });
        };
        if m.threshold_heart_rate != u8::MAX {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.threshold_heart_rate),
                is_expanded: false,
            });
        };
        if m.functional_threshold_power != u16::MAX {
            fields.push(Field {
                num: 3,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.functional_threshold_power),
                is_expanded: false,
            });
        };
        if m.hr_calc_type.0 != u8::MAX {
            fields.push(Field {
                num: 5,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.hr_calc_type.0),
                is_expanded: false,
            });
        };
        if m.pwr_calc_type.0 != u8::MAX {
            fields.push(Field {
                num: 7,
                base_type: FitBaseType::ENUM,
                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,
        }
    }
}

#[cfg(feature = "serde")]
impl Serialize for ZonesTarget {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let n = self.count_valid_fields() + 2;
        let mut state = serializer.serialize_struct("ZonesTarget", n)?;
        if self.max_heart_rate != u8::MAX {
            state.serialize_field("max_heart_rate", &self.max_heart_rate)?;
        }
        if self.threshold_heart_rate != u8::MAX {
            state.serialize_field("threshold_heart_rate", &self.threshold_heart_rate)?;
        }
        if self.functional_threshold_power != u16::MAX {
            state.serialize_field(
                "functional_threshold_power",
                &self.functional_threshold_power,
            )?;
        }
        if self.hr_calc_type.0 != u8::MAX {
            state.serialize_field("hr_calc_type", &self.hr_calc_type)?;
        }
        if self.pwr_calc_type.0 != u8::MAX {
            state.serialize_field("pwr_calc_type", &self.pwr_calc_type)?;
        }
        if !self.unknown_fields.is_empty() {
            state.serialize_field("unknown_fields", &self.unknown_fields)?;
        }
        if !self.developer_fields.is_empty() {
            state.serialize_field("developer_fields", &self.developer_fields)?;
        }
        state.end()
    }
}

#[cfg(feature = "serde")]
#[cfg_attr(feature = "serde", derive(Deserialize), serde(default))]
struct De {
    max_heart_rate: u8,
    threshold_heart_rate: u8,
    functional_threshold_power: u16,
    hr_calc_type: typedef::HrZoneCalc,
    pwr_calc_type: typedef::PwrZoneCalc,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for ZonesTarget {
    fn from(m: De) -> Self {
        Self {
            max_heart_rate: m.max_heart_rate,
            threshold_heart_rate: m.threshold_heart_rate,
            functional_threshold_power: m.functional_threshold_power,
            hr_calc_type: m.hr_calc_type,
            pwr_calc_type: m.pwr_calc_type,
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> 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(),
        }
    }
}