rustyfit 0.4.1

This project hosts the Rust implementation for The Flexible and Interoperable Data Transfer (FIT) Protocol
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::comparison_to_empty, clippy::manual_range_patterns)]

use crate::profile::{ProfileType, typedef};
use crate::proto::*;

#[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::<u8>::new(),
            sports: Vec::<typedef::SportBits0>::new(),
            workouts_supported: typedef::WorkoutCapabilities(u32::MIN),
            connectivity_supported: typedef::ConnectivityCapabilities(u32::MIN),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}

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 {
        let mut vals: [&Value; 24] = [const { &Value::Invalid }; 24];

        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 unknown_fields: Vec<Field> = Vec::with_capacity(n as usize);

        for field in &mesg.fields {
            if (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 == 0 {
                unknown_fields.push(field.clone());
                continue;
            }
            vals[field.num as usize] = &field.value;
        }

        Self {
            languages: vals[0].as_vec_u8(),
            sports: match &vals[1] {
                Value::VecUint8(v) => {
                    let mut vs = Vec::with_capacity(v.len());
                    for x in v {
                        vs.push(typedef::SportBits0(*x))
                    }
                    vs
                }
                _ => Vec::new(),
            },
            workouts_supported: typedef::WorkoutCapabilities(vals[21].as_u32z()),
            connectivity_supported: typedef::ConnectivityCapabilities(vals[23].as_u32z()),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<Capabilities> for Message {
    fn from(m: Capabilities) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 4];
        let mut len = 0usize;

        if m.languages != Vec::<u8>::new() {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::UINT8Z,
                value: Value::VecUint8(m.languages),
                is_expanded: false,
            };
            len += 1;
        }
        if m.sports != Vec::<typedef::SportBits0>::new() {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::SPORT_BITS_0,
                value: Value::VecUint8({
                    let mut v = Vec::with_capacity(m.sports.len());
                    for x in &m.sports {
                        v.push(x.0)
                    }
                    v
                }),
                is_expanded: false,
            };
            len += 1;
        }
        if m.workouts_supported != typedef::WorkoutCapabilities(u32::MIN) {
            arr[len] = Field {
                num: 21,
                profile_type: ProfileType::WORKOUT_CAPABILITIES,
                value: Value::Uint32(m.workouts_supported.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.connectivity_supported != typedef::ConnectivityCapabilities(u32::MIN) {
            arr[len] = Field {
                num: 23,
                profile_type: ProfileType::CONNECTIVITY_CAPABILITIES,
                value: Value::Uint32(m.connectivity_supported.0),
                is_expanded: false,
            };
            len += 1;
        }

        Message {
            header: 0,
            num: typedef::MesgNum::CAPABILITIES,
            fields: {
                let mut fields: Vec<Field> = Vec::with_capacity(len + m.unknown_fields.len());
                fields.extend_from_slice(&arr[..len]);
                fields.extend_from_slice(&m.unknown_fields);
                fields
            },
            developer_fields: m.developer_fields,
        }
    }
}