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)]
/// 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::<typedef::SegmentLeaderboardType>::new(),
            leader_group_primary_key: Vec::<u32>::new(),
            leader_activity_id: Vec::<u32>::new(),
            leader_activity_id_string: Vec::<String>::new(),
            default_race_leader: u8::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}

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

        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 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 {
            message_index: typedef::MessageIndex(vals[254].as_u16()),
            file_uuid: vals[1].as_string(),
            enabled: typedef::Bool(vals[3].as_u8()),
            user_profile_primary_key: vals[4].as_u32(),
            leader_type: match &vals[7] {
                Value::VecUint8(v) => {
                    let mut vs = Vec::with_capacity(v.len());
                    for x in v {
                        vs.push(typedef::SegmentLeaderboardType(*x))
                    }
                    vs
                }
                _ => Vec::new(),
            },
            leader_group_primary_key: vals[8].as_vec_u32(),
            leader_activity_id: vals[9].as_vec_u32(),
            leader_activity_id_string: vals[10].as_vec_string(),
            default_race_leader: vals[11].as_u8(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

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

        if m.message_index != typedef::MessageIndex(u16::MAX) {
            arr[len] = Field {
                num: 254,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.message_index.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.file_uuid != String::new() {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::STRING,
                value: Value::String(m.file_uuid),
                is_expanded: false,
            };
            len += 1;
        }
        if m.enabled != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.enabled.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.user_profile_primary_key != u32::MAX {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.user_profile_primary_key),
                is_expanded: false,
            };
            len += 1;
        }
        if m.leader_type != Vec::<typedef::SegmentLeaderboardType>::new() {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::SEGMENT_LEADERBOARD_TYPE,
                value: Value::VecUint8({
                    let mut v = Vec::with_capacity(m.leader_type.len());
                    for x in &m.leader_type {
                        v.push(x.0)
                    }
                    v
                }),
                is_expanded: false,
            };
            len += 1;
        }
        if m.leader_group_primary_key != Vec::<u32>::new() {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::UINT32,
                value: Value::VecUint32(m.leader_group_primary_key),
                is_expanded: false,
            };
            len += 1;
        }
        if m.leader_activity_id != Vec::<u32>::new() {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::UINT32,
                value: Value::VecUint32(m.leader_activity_id),
                is_expanded: false,
            };
            len += 1;
        }
        if m.leader_activity_id_string != Vec::<String>::new() {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::STRING,
                value: Value::VecString(m.leader_activity_id_string),
                is_expanded: false,
            };
            len += 1;
        }
        if m.default_race_leader != u8::MAX {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.default_race_leader),
                is_expanded: false,
            };
            len += 1;
        }

        Message {
            header: 0,
            num: typedef::MesgNum::SEGMENT_FILE,
            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,
        }
    }
}