rustyfit 0.5.0

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)]
/// Goal is a Goal message.
pub struct Goal {
    pub message_index: typedef::MessageIndex,
    pub sport: typedef::Sport,
    pub sub_sport: typedef::SubSport,
    pub start_date: typedef::DateTime,
    pub end_date: typedef::DateTime,
    pub r#type: typedef::Goal,
    pub value: u32,
    pub repeat: typedef::Bool,
    pub target_value: u32,
    pub recurrence: typedef::GoalRecurrence,
    pub recurrence_value: u16,
    pub enabled: typedef::Bool,
    pub source: typedef::GoalSource,
    /// 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 Goal {
    /// Value's type: `u16`
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `u8`
    pub const SPORT: u8 = 0;
    /// Value's type: `u8`
    pub const SUB_SPORT: u8 = 1;
    /// Value's type: `u32`
    pub const START_DATE: u8 = 2;
    /// Value's type: `u32`
    pub const END_DATE: u8 = 3;
    /// Value's type: `u8`
    pub const TYPE: u8 = 4;
    /// Value's type: `u32`
    pub const VALUE: u8 = 5;
    /// Value's type: `u8`
    pub const REPEAT: u8 = 6;
    /// Value's type: `u32`
    pub const TARGET_VALUE: u8 = 7;
    /// Value's type: `u8`
    pub const RECURRENCE: u8 = 8;
    /// Value's type: `u16`
    pub const RECURRENCE_VALUE: u8 = 9;
    /// Value's type: `u8`
    pub const ENABLED: u8 = 10;
    /// Value's type: `u8`
    pub const SOURCE: u8 = 11;

    /// Create new Goal with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            sport: typedef::Sport(u8::MAX),
            sub_sport: typedef::SubSport(u8::MAX),
            start_date: typedef::DateTime(u32::MAX),
            end_date: typedef::DateTime(u32::MAX),
            r#type: typedef::Goal(u8::MAX),
            value: u32::MAX,
            repeat: typedef::Bool(u8::MAX),
            target_value: u32::MAX,
            recurrence: typedef::GoalRecurrence(u8::MAX),
            recurrence_value: u16::MAX,
            enabled: typedef::Bool(u8::MAX),
            source: typedef::GoalSource(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}

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

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

        const KNOWN_NUMS: [u64; 4] = [4095, 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()),
            sport: typedef::Sport(vals[0].as_u8()),
            sub_sport: typedef::SubSport(vals[1].as_u8()),
            start_date: typedef::DateTime(vals[2].as_u32()),
            end_date: typedef::DateTime(vals[3].as_u32()),
            r#type: typedef::Goal(vals[4].as_u8()),
            value: vals[5].as_u32(),
            repeat: typedef::Bool(vals[6].as_u8()),
            target_value: vals[7].as_u32(),
            recurrence: typedef::GoalRecurrence(vals[8].as_u8()),
            recurrence_value: vals[9].as_u16(),
            enabled: typedef::Bool(vals[10].as_u8()),
            source: typedef::GoalSource(vals[11].as_u8()),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<Goal> for Message {
    fn from(m: Goal) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 13];
        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.sport != typedef::Sport(u8::MAX) {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::SPORT,
                value: Value::Uint8(m.sport.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.sub_sport != typedef::SubSport(u8::MAX) {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::SUB_SPORT,
                value: Value::Uint8(m.sub_sport.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.start_date != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.start_date.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.end_date != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.end_date.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.r#type != typedef::Goal(u8::MAX) {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::GOAL,
                value: Value::Uint8(m.r#type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.value != u32::MAX {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.value),
                is_expanded: false,
            };
            len += 1;
        }
        if m.repeat != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.repeat.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.target_value != u32::MAX {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.target_value),
                is_expanded: false,
            };
            len += 1;
        }
        if m.recurrence != typedef::GoalRecurrence(u8::MAX) {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::GOAL_RECURRENCE,
                value: Value::Uint8(m.recurrence.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.recurrence_value != u16::MAX {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.recurrence_value),
                is_expanded: false,
            };
            len += 1;
        }
        if m.enabled != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.enabled.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.source != typedef::GoalSource(u8::MAX) {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::GOAL_SOURCE,
                value: Value::Uint8(m.source.0),
                is_expanded: false,
            };
            len += 1;
        }

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