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)]
/// Capabilities is a Capabilities message.
pub struct Capabilities {
    /// Base: UINT8Z; Use language_bits_x types where x is index of array.
    pub languages: Vec<u8>,
    /// Base: UINT8Z; Use sport_bits_x types where x is index of array.
    pub sports: Vec<typedef::SportBits0>,
    /// Base: UINT32Z
    pub workouts_supported: typedef::WorkoutCapabilities,
    /// Base: UINT32Z
    pub connectivity_supported: typedef::ConnectivityCapabilities,
    /// 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 Capabilities {
    /// Value's type: `Vec<u8>`; Base: UINT8Z
    pub const LANGUAGES: u8 = 0;
    /// Value's type: `Vec<u8>`; Base: UINT8Z
    pub const SPORTS: u8 = 1;
    /// Value's type: `u32`; Base: UINT32Z
    pub const WORKOUTS_SUPPORTED: u8 = 21;
    /// Value's type: `u32`; Base: UINT32Z
    pub const CONNECTIVITY_SUPPORTED: u8 = 23;

    /// Create new Capabilities with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            languages: Vec::new(),
            sports: Vec::new(),
            workouts_supported: typedef::WorkoutCapabilities(u32::MIN),
            connectivity_supported: typedef::ConnectivityCapabilities(u32::MIN),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (!self.languages.is_empty()) as usize
            + (!self.sports.is_empty()) as usize
            + (self.workouts_supported != typedef::WorkoutCapabilities(u32::MIN)) as usize
            + (self.connectivity_supported != typedef::ConnectivityCapabilities(u32::MIN)) as usize
    }
}

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

impl From<&Message> for Capabilities {
    /// from creates new Capabilities struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [10485763, 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.languages = field.value.to_vec_u8(),
                1 => {
                    v.sports = match &field.value {
                        Value::VecUint8(v) => {
                            let mut vs = Vec::with_capacity(v.len());
                            vs.extend(v.iter().map(|&x| typedef::SportBits0(x)));
                            vs
                        }
                        _ => Vec::new(),
                    }
                }
                21 => v.workouts_supported = typedef::WorkoutCapabilities(field.value.as_u32z()),
                23 => {
                    v.connectivity_supported =
                        typedef::ConnectivityCapabilities(field.value.as_u32z())
                }
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if !m.languages.is_empty() {
            fields.push(Field {
                num: 0,
                profile_type: ProfileType::UINT8Z,
                value: Value::VecUint8(m.languages),
                is_expanded: false,
            });
        };
        if !m.sports.is_empty() {
            fields.push(Field {
                num: 1,
                profile_type: ProfileType::SPORT_BITS_0,
                value: Value::VecUint8({
                    let (ptr, len, capacity) = m.sports.into_raw_parts();
                    unsafe { Vec::from_raw_parts(ptr.cast::<u8>(), len, capacity) }
                }),
                is_expanded: false,
            });
        };
        if m.workouts_supported != typedef::WorkoutCapabilities(u32::MIN) {
            fields.push(Field {
                num: 21,
                profile_type: ProfileType::WORKOUT_CAPABILITIES,
                value: Value::Uint32(m.workouts_supported.0),
                is_expanded: false,
            });
        };
        if m.connectivity_supported != typedef::ConnectivityCapabilities(u32::MIN) {
            fields.push(Field {
                num: 23,
                profile_type: ProfileType::CONNECTIVITY_CAPABILITIES,
                value: Value::Uint32(m.connectivity_supported.0),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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