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::borrow::ToOwned;
use alloc::string::String;
use alloc::vec::Vec;

#[derive(Debug, Clone)]
/// SegmentFile is a SegmentFile message.
pub struct SegmentFile {
    pub message_index: typedef::MessageIndex,
    /// UUID of the segment file
    pub file_uuid: String,
    /// Enabled state of the segment file
    pub enabled: typedef::Bool,
    /// Primary key of the user that created the segment file
    pub user_profile_primary_key: u32,
    /// Leader type of each leader in the segment file
    pub leader_type: Vec<typedef::SegmentLeaderboardType>,
    /// Group primary key of each leader in the segment file
    pub leader_group_primary_key: Vec<u32>,
    /// Activity ID of each leader in the segment file
    pub leader_activity_id: Vec<u32>,
    /// String version of the activity ID of each leader in the segment file. 21 characters long for each ID, express in decimal
    pub leader_activity_id_string: Vec<String>,
    /// Index for the Leader Board entry selected as the default race participant
    pub default_race_leader: u8,
    /// 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 SegmentFile {
    /// Value's type: `u16`
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `String`
    pub const FILE_UUID: u8 = 1;
    /// Value's type: `u8`
    pub const ENABLED: u8 = 3;
    /// Value's type: `u32`
    pub const USER_PROFILE_PRIMARY_KEY: u8 = 4;
    /// Value's type: `Vec<u8>`
    pub const LEADER_TYPE: u8 = 7;
    /// Value's type: `Vec<u32>`
    pub const LEADER_GROUP_PRIMARY_KEY: u8 = 8;
    /// Value's type: `Vec<u32>`
    pub const LEADER_ACTIVITY_ID: u8 = 9;
    /// Value's type: `Vec<String>`
    pub const LEADER_ACTIVITY_ID_STRING: u8 = 10;
    /// Value's type: `u8`
    pub const DEFAULT_RACE_LEADER: u8 = 11;

    /// Create new SegmentFile with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            file_uuid: String::new(),
            enabled: typedef::Bool(u8::MAX),
            user_profile_primary_key: u32::MAX,
            leader_type: Vec::new(),
            leader_group_primary_key: Vec::new(),
            leader_activity_id: Vec::new(),
            leader_activity_id_string: Vec::new(),
            default_race_leader: u8::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (self.message_index != typedef::MessageIndex(u16::MAX)) as usize
            + (!self.file_uuid.is_empty()) as usize
            + (self.enabled != typedef::Bool(u8::MAX)) as usize
            + (self.user_profile_primary_key != u32::MAX) as usize
            + (!self.leader_type.is_empty()) as usize
            + (!self.leader_group_primary_key.is_empty()) as usize
            + (!self.leader_activity_id.is_empty()) as usize
            + (!self.leader_activity_id_string.is_empty()) as usize
            + (self.default_race_leader != u8::MAX) as usize
    }
}

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

impl From<&Message> for SegmentFile {
    /// from creates new SegmentFile struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [3994, 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.file_uuid = field.value.as_str().to_owned(),
                3 => v.enabled = typedef::Bool(field.value.as_u8()),
                4 => v.user_profile_primary_key = field.value.as_u32(),
                7 => {
                    v.leader_type = match &field.value {
                        Value::VecUint8(v) => {
                            let mut vs = Vec::with_capacity(v.len());
                            vs.extend(v.iter().map(|&x| typedef::SegmentLeaderboardType(x)));
                            vs
                        }
                        _ => Vec::new(),
                    }
                }
                8 => v.leader_group_primary_key = field.value.to_vec_u32(),
                9 => v.leader_activity_id = field.value.to_vec_u32(),
                10 => v.leader_activity_id_string = field.value.to_vec_string(),
                11 => v.default_race_leader = field.value.as_u8(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if m.message_index != typedef::MessageIndex(u16::MAX) {
            fields.push(Field {
                num: 254,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.message_index.0),
                is_expanded: false,
            });
        };
        if !m.file_uuid.is_empty() {
            fields.push(Field {
                num: 1,
                profile_type: ProfileType::STRING,
                value: Value::String(m.file_uuid),
                is_expanded: false,
            });
        };
        if m.enabled != typedef::Bool(u8::MAX) {
            fields.push(Field {
                num: 3,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.enabled.0),
                is_expanded: false,
            });
        };
        if m.user_profile_primary_key != u32::MAX {
            fields.push(Field {
                num: 4,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.user_profile_primary_key),
                is_expanded: false,
            });
        };
        if !m.leader_type.is_empty() {
            fields.push(Field {
                num: 7,
                profile_type: ProfileType::SEGMENT_LEADERBOARD_TYPE,
                value: Value::VecUint8({
                    let (ptr, len, capacity) = m.leader_type.into_raw_parts();
                    unsafe { Vec::from_raw_parts(ptr.cast::<u8>(), len, capacity) }
                }),
                is_expanded: false,
            });
        };
        if !m.leader_group_primary_key.is_empty() {
            fields.push(Field {
                num: 8,
                profile_type: ProfileType::UINT32,
                value: Value::VecUint32(m.leader_group_primary_key),
                is_expanded: false,
            });
        };
        if !m.leader_activity_id.is_empty() {
            fields.push(Field {
                num: 9,
                profile_type: ProfileType::UINT32,
                value: Value::VecUint32(m.leader_activity_id),
                is_expanded: false,
            });
        };
        if !m.leader_activity_id_string.is_empty() {
            fields.push(Field {
                num: 10,
                profile_type: ProfileType::STRING,
                value: Value::VecString(m.leader_activity_id_string),
                is_expanded: false,
            });
        };
        if m.default_race_leader != u8::MAX {
            fields.push(Field {
                num: 11,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.default_race_leader),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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