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)]
/// 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(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (!self.name.is_empty()) as usize
            + (!self.uuid.is_empty()) as usize
            + (self.sport != typedef::Sport(u8::MAX)) as usize
            + (self.enabled != typedef::Bool(u8::MAX)) as usize
            + (self.user_profile_primary_key != u32::MAX) as usize
            + (self.device_id != u32::MAX) as usize
            + (self.default_race_leader != u8::MAX) as usize
            + (self.delete_status != typedef::SegmentDeleteStatus(u8::MAX)) as usize
            + (self.selection_type != typedef::SegmentSelectionType(u8::MAX)) as usize
    }
}

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 {
        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 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.name = field.value.as_str().to_owned(),
                1 => v.uuid = field.value.as_str().to_owned(),
                2 => v.sport = typedef::Sport(field.value.as_u8()),
                3 => v.enabled = typedef::Bool(field.value.as_u8()),
                4 => v.user_profile_primary_key = field.value.as_u32(),
                5 => v.device_id = field.value.as_u32(),
                6 => v.default_race_leader = field.value.as_u8(),
                7 => v.delete_status = typedef::SegmentDeleteStatus(field.value.as_u8()),
                8 => v.selection_type = typedef::SegmentSelectionType(field.value.as_u8()),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if !m.name.is_empty() {
            fields.push(Field {
                num: 0,
                profile_type: ProfileType::STRING,
                value: Value::String(m.name),
                is_expanded: false,
            });
        };
        if !m.uuid.is_empty() {
            fields.push(Field {
                num: 1,
                profile_type: ProfileType::STRING,
                value: Value::String(m.uuid),
                is_expanded: false,
            });
        };
        if m.sport != typedef::Sport(u8::MAX) {
            fields.push(Field {
                num: 2,
                profile_type: ProfileType::SPORT,
                value: Value::Uint8(m.sport.0),
                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.device_id != u32::MAX {
            fields.push(Field {
                num: 5,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.device_id),
                is_expanded: false,
            });
        };
        if m.default_race_leader != u8::MAX {
            fields.push(Field {
                num: 6,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.default_race_leader),
                is_expanded: false,
            });
        };
        if m.delete_status != typedef::SegmentDeleteStatus(u8::MAX) {
            fields.push(Field {
                num: 7,
                profile_type: ProfileType::SEGMENT_DELETE_STATUS,
                value: Value::Uint8(m.delete_status.0),
                is_expanded: false,
            });
        };
        if m.selection_type != typedef::SegmentSelectionType(u8::MAX) {
            fields.push(Field {
                num: 8,
                profile_type: ProfileType::SEGMENT_SELECTION_TYPE,
                value: Value::Uint8(m.selection_type.0),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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