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)]
/// AntChannelId is a AntChannelId message.
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`
    pub const CHANNEL_NUMBER: u8 = 0;
    /// Value's type: `u8`; Base: UINT8Z
    pub const DEVICE_TYPE: u8 = 1;
    /// Value's type: `u16`; Base: UINT16Z
    pub const DEVICE_NUMBER: u8 = 2;
    /// Value's type: `u8`; Base: UINT8Z
    pub const TRANSMISSION_TYPE: u8 = 3;
    /// Value's type: `u8`
    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 != typedef::DeviceIndex(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,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.channel_number),
                is_expanded: false,
            });
        };
        if m.device_type != u8::MIN {
            fields.push(Field {
                num: 1,
                profile_type: ProfileType::UINT8Z,
                value: Value::Uint8(m.device_type),
                is_expanded: false,
            });
        };
        if m.device_number != u16::MIN {
            fields.push(Field {
                num: 2,
                profile_type: ProfileType::UINT16Z,
                value: Value::Uint16(m.device_number),
                is_expanded: false,
            });
        };
        if m.transmission_type != u8::MIN {
            fields.push(Field {
                num: 3,
                profile_type: ProfileType::UINT8Z,
                value: Value::Uint8(m.transmission_type),
                is_expanded: false,
            });
        };
        if m.device_index != typedef::DeviceIndex(u8::MAX) {
            fields.push(Field {
                num: 4,
                profile_type: ProfileType::DEVICE_INDEX,
                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,
        }
    }
}