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::vec::Vec;

#[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(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (self.message_index != typedef::MessageIndex(u16::MAX)) as usize
            + (self.sport != typedef::Sport(u8::MAX)) as usize
            + (self.sub_sport != typedef::SubSport(u8::MAX)) as usize
            + (self.start_date != typedef::DateTime(u32::MAX)) as usize
            + (self.end_date != typedef::DateTime(u32::MAX)) as usize
            + (self.r#type != typedef::Goal(u8::MAX)) as usize
            + (self.value != u32::MAX) as usize
            + (self.repeat != typedef::Bool(u8::MAX)) as usize
            + (self.target_value != u32::MAX) as usize
            + (self.recurrence != typedef::GoalRecurrence(u8::MAX)) as usize
            + (self.recurrence_value != u16::MAX) as usize
            + (self.enabled != typedef::Bool(u8::MAX)) as usize
            + (self.source != typedef::GoalSource(u8::MAX)) as usize
    }
}

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 {
        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 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 {
                254 => v.message_index = typedef::MessageIndex(field.value.as_u16()),
                0 => v.sport = typedef::Sport(field.value.as_u8()),
                1 => v.sub_sport = typedef::SubSport(field.value.as_u8()),
                2 => v.start_date = typedef::DateTime(field.value.as_u32()),
                3 => v.end_date = typedef::DateTime(field.value.as_u32()),
                4 => v.r#type = typedef::Goal(field.value.as_u8()),
                5 => v.value = field.value.as_u32(),
                6 => v.repeat = typedef::Bool(field.value.as_u8()),
                7 => v.target_value = field.value.as_u32(),
                8 => v.recurrence = typedef::GoalRecurrence(field.value.as_u8()),
                9 => v.recurrence_value = field.value.as_u16(),
                10 => v.enabled = typedef::Bool(field.value.as_u8()),
                11 => v.source = typedef::GoalSource(field.value.as_u8()),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if m.message_index != typedef::MessageIndex(u16::MAX) {
            fields.push(Field {
                num: 254,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.message_index.0),
                is_expanded: false,
            });
        };
        if m.sport != typedef::Sport(u8::MAX) {
            fields.push(Field {
                num: 0,
                profile_type: ProfileType::SPORT,
                value: Value::Uint8(m.sport.0),
                is_expanded: false,
            });
        };
        if m.sub_sport != typedef::SubSport(u8::MAX) {
            fields.push(Field {
                num: 1,
                profile_type: ProfileType::SUB_SPORT,
                value: Value::Uint8(m.sub_sport.0),
                is_expanded: false,
            });
        };
        if m.start_date != typedef::DateTime(u32::MAX) {
            fields.push(Field {
                num: 2,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.start_date.0),
                is_expanded: false,
            });
        };
        if m.end_date != typedef::DateTime(u32::MAX) {
            fields.push(Field {
                num: 3,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.end_date.0),
                is_expanded: false,
            });
        };
        if m.r#type != typedef::Goal(u8::MAX) {
            fields.push(Field {
                num: 4,
                profile_type: ProfileType::GOAL,
                value: Value::Uint8(m.r#type.0),
                is_expanded: false,
            });
        };
        if m.value != u32::MAX {
            fields.push(Field {
                num: 5,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.value),
                is_expanded: false,
            });
        };
        if m.repeat != typedef::Bool(u8::MAX) {
            fields.push(Field {
                num: 6,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.repeat.0),
                is_expanded: false,
            });
        };
        if m.target_value != u32::MAX {
            fields.push(Field {
                num: 7,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.target_value),
                is_expanded: false,
            });
        };
        if m.recurrence != typedef::GoalRecurrence(u8::MAX) {
            fields.push(Field {
                num: 8,
                profile_type: ProfileType::GOAL_RECURRENCE,
                value: Value::Uint8(m.recurrence.0),
                is_expanded: false,
            });
        };
        if m.recurrence_value != u16::MAX {
            fields.push(Field {
                num: 9,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.recurrence_value),
                is_expanded: false,
            });
        };
        if m.enabled != typedef::Bool(u8::MAX) {
            fields.push(Field {
                num: 10,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.enabled.0),
                is_expanded: false,
            });
        };
        if m.source != typedef::GoalSource(u8::MAX) {
            fields.push(Field {
                num: 11,
                profile_type: ProfileType::GOAL_SOURCE,
                value: Value::Uint8(m.source.0),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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