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)]
/// SegmentId is a SegmentId message.
pub struct SegmentId {
    /// Friendly name assigned to segment
    pub name: String,
    /// UUID of the segment
    pub uuid: String,
    /// Sport associated with the segment
    pub sport: typedef::Sport,
    /// Segment enabled for evaluation
    pub enabled: typedef::Bool,
    /// Primary key of the user that created the segment
    pub user_profile_primary_key: u32,
    /// ID of the device that created the segment
    pub device_id: u32,
    /// Index for the Leader Board entry selected as the default race participant
    pub default_race_leader: u8,
    /// Indicates if any segments should be deleted
    pub delete_status: typedef::SegmentDeleteStatus,
    /// Indicates how the segment was selected to be sent to the device
    pub selection_type: typedef::SegmentSelectionType,
    /// 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 SegmentId {
    /// Value's type: `String`
    pub const NAME: u8 = 0;
    /// Value's type: `String`
    pub const UUID: u8 = 1;
    /// Value's type: `u8`
    pub const SPORT: u8 = 2;
    /// Value's type: `u8`
    pub const ENABLED: u8 = 3;
    /// Value's type: `u32`
    pub const USER_PROFILE_PRIMARY_KEY: u8 = 4;
    /// Value's type: `u32`
    pub const DEVICE_ID: u8 = 5;
    /// Value's type: `u8`
    pub const DEFAULT_RACE_LEADER: u8 = 6;
    /// Value's type: `u8`
    pub const DELETE_STATUS: u8 = 7;
    /// Value's type: `u8`
    pub const SELECTION_TYPE: u8 = 8;

    /// Create new SegmentId with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            name: String::new(),
            uuid: String::new(),
            sport: typedef::Sport(u8::MAX),
            enabled: typedef::Bool(u8::MAX),
            user_profile_primary_key: u32::MAX,
            device_id: u32::MAX,
            default_race_leader: u8::MAX,
            delete_status: typedef::SegmentDeleteStatus(u8::MAX),
            selection_type: typedef::SegmentSelectionType(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}

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

impl From<&Message> for SegmentId {
    /// from creates new SegmentId struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        let mut vals: [&Value; 9] = [const { &Value::Invalid }; 9];

        const KNOWN_NUMS: [u64; 4] = [511, 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 {
            name: vals[0].as_string(),
            uuid: vals[1].as_string(),
            sport: typedef::Sport(vals[2].as_u8()),
            enabled: typedef::Bool(vals[3].as_u8()),
            user_profile_primary_key: vals[4].as_u32(),
            device_id: vals[5].as_u32(),
            default_race_leader: vals[6].as_u8(),
            delete_status: typedef::SegmentDeleteStatus(vals[7].as_u8()),
            selection_type: typedef::SegmentSelectionType(vals[8].as_u8()),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<SegmentId> for Message {
    fn from(m: SegmentId) -> 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.name != String::new() {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::STRING,
                value: Value::String(m.name),
                is_expanded: false,
            };
            len += 1;
        }
        if m.uuid != String::new() {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::STRING,
                value: Value::String(m.uuid),
                is_expanded: false,
            };
            len += 1;
        }
        if m.sport != typedef::Sport(u8::MAX) {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::SPORT,
                value: Value::Uint8(m.sport.0),
                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.device_id != u32::MAX {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.device_id),
                is_expanded: false,
            };
            len += 1;
        }
        if m.default_race_leader != u8::MAX {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.default_race_leader),
                is_expanded: false,
            };
            len += 1;
        }
        if m.delete_status != typedef::SegmentDeleteStatus(u8::MAX) {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::SEGMENT_DELETE_STATUS,
                value: Value::Uint8(m.delete_status.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.selection_type != typedef::SegmentSelectionType(u8::MAX) {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::SEGMENT_SELECTION_TYPE,
                value: Value::Uint8(m.selection_type.0),
                is_expanded: false,
            };
            len += 1;
        }

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