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};

/// Ant Channel Id message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct AntChannelId {
    pub channel_number: u8,
    /// Base: UINT8Z
    pub device_type: u8,
    /// Base: UINT16Z
    pub device_number: u16,
    /// Base: UINT8Z
    pub transmission_type: u8,
    pub device_index: typedef::DeviceIndex,
    /// 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 AntChannelId {
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::Uint8
    pub const CHANNEL_NUMBER: u8 = 0;
    /// Value's type: `u8`; FitBaseType::UINT8Z; ProfileType::Uint8z
    pub const DEVICE_TYPE: u8 = 1;
    /// Value's type: `u16`; FitBaseType::UINT16Z; ProfileType::Uint16z
    pub const DEVICE_NUMBER: u8 = 2;
    /// Value's type: `u8`; FitBaseType::UINT8Z; ProfileType::Uint8z
    pub const TRANSMISSION_TYPE: u8 = 3;
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::DeviceIndex
    pub const DEVICE_INDEX: u8 = 4;

    /// Create new AntChannelId with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            channel_number: u8::MAX,
            device_type: u8::MIN,
            device_number: u16::MIN,
            transmission_type: u8::MIN,
            device_index: typedef::DeviceIndex(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (self.channel_number != u8::MAX) as usize
            + (self.device_type != u8::MIN) as usize
            + (self.device_number != u16::MIN) as usize
            + (self.transmission_type != u8::MIN) as usize
            + (self.device_index.0 != u8::MAX) as usize
    }
}

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

impl From<&Message> for AntChannelId {
    /// from creates new AntChannelId struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [31, 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 {
                0 => v.channel_number = field.value.as_u8(),
                1 => v.device_type = field.value.as_u8z(),
                2 => v.device_number = field.value.as_u16z(),
                3 => v.transmission_type = field.value.as_u8z(),
                4 => v.device_index = typedef::DeviceIndex(field.value.as_u8()),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if m.channel_number != u8::MAX {
            fields.push(Field {
                num: 0,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.channel_number),
                is_expanded: false,
            });
        };
        if m.device_type != u8::MIN {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::UINT8Z,
                value: Value::Uint8(m.device_type),
                is_expanded: false,
            });
        };
        if m.device_number != u16::MIN {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::UINT16Z,
                value: Value::Uint16(m.device_number),
                is_expanded: false,
            });
        };
        if m.transmission_type != u8::MIN {
            fields.push(Field {
                num: 3,
                base_type: FitBaseType::UINT8Z,
                value: Value::Uint8(m.transmission_type),
                is_expanded: false,
            });
        };
        if m.device_index.0 != u8::MAX {
            fields.push(Field {
                num: 4,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.device_index.0),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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

#[cfg(feature = "serde")]
impl Serialize for AntChannelId {
    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("AntChannelId", n)?;
        if self.channel_number != u8::MAX {
            state.serialize_field("channel_number", &self.channel_number)?;
        }
        if self.device_type != u8::MIN {
            state.serialize_field("device_type", &self.device_type)?;
        }
        if self.device_number != u16::MIN {
            state.serialize_field("device_number", &self.device_number)?;
        }
        if self.transmission_type != u8::MIN {
            state.serialize_field("transmission_type", &self.transmission_type)?;
        }
        if self.device_index.0 != u8::MAX {
            state.serialize_field("device_index", &self.device_index)?;
        }
        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 {
    channel_number: u8,
    device_type: u8,
    device_number: u16,
    transmission_type: u8,
    device_index: typedef::DeviceIndex,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for AntChannelId {
    fn from(m: De) -> Self {
        Self {
            channel_number: m.channel_number,
            device_type: m.device_type,
            device_number: m.device_number,
            transmission_type: m.transmission_type,
            device_index: m.device_index,
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            channel_number: u8::MAX,
            device_type: u8::MIN,
            device_number: u16::MIN,
            transmission_type: u8::MIN,
            device_index: typedef::DeviceIndex(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}