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::borrow::ToOwned;
use alloc::string::String;
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};

/// Power Zone message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct PowerZone {
    pub message_index: typedef::MessageIndex,
    /// Units: watts
    pub high_value: u16,
    pub name: String,
    /// 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 PowerZone {
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::MessageIndex
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16; Units: `watts`
    pub const HIGH_VALUE: u8 = 1;
    /// Value's type: `String`; FitBaseType::STRING; ProfileType::String
    pub const NAME: u8 = 2;

    /// Create new PowerZone with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            high_value: u16::MAX,
            name: String::new(),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (self.message_index.0 != u16::MAX) as usize
            + (self.high_value != u16::MAX) as usize
            + (!self.name.is_empty()) as usize
    }
}

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

impl From<&Message> for PowerZone {
    /// from creates new PowerZone struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [6, 0, 0, 4611686018427387904];
        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 {
                254 => v.message_index = typedef::MessageIndex(field.value.as_u16()),
                1 => v.high_value = field.value.as_u16(),
                2 => v.name = field.value.as_str().to_owned(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if m.message_index.0 != u16::MAX {
            fields.push(Field {
                num: 254,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.message_index.0),
                is_expanded: false,
            });
        };
        if m.high_value != u16::MAX {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.high_value),
                is_expanded: false,
            });
        };
        if !m.name.is_empty() {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::STRING,
                value: Value::String(m.name),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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

#[cfg(feature = "serde")]
impl Serialize for PowerZone {
    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("PowerZone", n)?;
        if self.message_index.0 != u16::MAX {
            state.serialize_field("message_index", &self.message_index)?;
        }
        if self.high_value != u16::MAX {
            state.serialize_field("high_value", &self.high_value)?;
        }
        if !self.name.is_empty() {
            state.serialize_field("name", &self.name)?;
        }
        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 {
    message_index: typedef::MessageIndex,
    high_value: u16,
    name: String,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for PowerZone {
    fn from(m: De) -> Self {
        Self {
            message_index: m.message_index,
            high_value: m.high_value,
            name: m.name,
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            high_value: u16::MAX,
            name: String::new(),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}