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)]
/// SegmentLeaderboardEntry is a SegmentLeaderboardEntry message.
pub struct SegmentLeaderboardEntry {
    pub message_index: typedef::MessageIndex,
    /// Friendly name assigned to leader
    pub name: String,
    /// Leader classification
    pub r#type: typedef::SegmentLeaderboardType,
    /// Primary user ID of this leader
    pub group_primary_key: u32,
    /// ID of the activity associated with this leader time
    pub activity_id: u32,
    /// Scale: 1000; Units: s; Segment Time (includes pauses)
    pub segment_time: u32,
    /// String version of the activity_id. 21 characters long, express in decimal
    pub activity_id_string: String,
    /// 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 SegmentLeaderboardEntry {
    /// Value's type: `u16`
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `String`
    pub const NAME: u8 = 0;
    /// Value's type: `u8`
    pub const TYPE: u8 = 1;
    /// Value's type: `u32`
    pub const GROUP_PRIMARY_KEY: u8 = 2;
    /// Value's type: `u32`
    pub const ACTIVITY_ID: u8 = 3;
    /// Value's type: `u32`; Scale: `1000`; Units: `s`
    pub const SEGMENT_TIME: u8 = 4;
    /// Value's type: `String`
    pub const ACTIVITY_ID_STRING: u8 = 5;

    /// Create new SegmentLeaderboardEntry with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            name: String::new(),
            r#type: typedef::SegmentLeaderboardType(u8::MAX),
            group_primary_key: u32::MAX,
            activity_id: u32::MAX,
            segment_time: u32::MAX,
            activity_id_string: String::new(),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `segment_time` in its scaled value. It returns invalid f64 when value is valid.
    pub fn segment_time_scaled(&self) -> f64 {
        if self.segment_time == u32::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.segment_time as f64 / 1000.0 - 0.0
    }

    /// Set `segment_time` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_segment_time_scaled(&mut self, v: f64) -> &mut SegmentLeaderboardEntry {
        let unscaled = (v + 0.0) * 1000.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
            self.segment_time = u32::MAX;
            return self;
        }
        self.segment_time = unscaled as u32;
        self
    }
}

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

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

        const KNOWN_NUMS: [u64; 4] = [63, 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()),
            name: vals[0].as_string(),
            r#type: typedef::SegmentLeaderboardType(vals[1].as_u8()),
            group_primary_key: vals[2].as_u32(),
            activity_id: vals[3].as_u32(),
            segment_time: vals[4].as_u32(),
            activity_id_string: vals[5].as_string(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<SegmentLeaderboardEntry> for Message {
    fn from(m: SegmentLeaderboardEntry) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 7];
        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.name != String::new() {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::STRING,
                value: Value::String(m.name),
                is_expanded: false,
            };
            len += 1;
        }
        if m.r#type != typedef::SegmentLeaderboardType(u8::MAX) {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::SEGMENT_LEADERBOARD_TYPE,
                value: Value::Uint8(m.r#type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.group_primary_key != u32::MAX {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.group_primary_key),
                is_expanded: false,
            };
            len += 1;
        }
        if m.activity_id != u32::MAX {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.activity_id),
                is_expanded: false,
            };
            len += 1;
        }
        if m.segment_time != u32::MAX {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.segment_time),
                is_expanded: false,
            };
            len += 1;
        }
        if m.activity_id_string != String::new() {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::STRING,
                value: Value::String(m.activity_id_string),
                is_expanded: false,
            };
            len += 1;
        }

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