rustyfit 0.10.0

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.

use crate::profile::typedef::{self, FitBaseType};
use crate::proto::*;
use alloc::string::String;
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};

/// Exercise Title message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct ExerciseTitle {
    pub message_index: typedef::MessageIndex,
    pub exercise_category: typedef::ExerciseCategory,
    pub exercise_name: u16,
    pub wkt_step_name: Vec<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 ExerciseTitle {
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::MessageIndex
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::ExerciseCategory
    pub const EXERCISE_CATEGORY: u8 = 0;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16
    pub const EXERCISE_NAME: u8 = 1;
    /// Value's type: `Vec<String>`; FitBaseType::STRING; ProfileType::String
    pub const WKT_STEP_NAME: u8 = 2;

    /// Create new ExerciseTitle with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            exercise_category: typedef::ExerciseCategory(u16::MAX),
            exercise_name: u16::MAX,
            wkt_step_name: Vec::new(),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (self.message_index.0 != u16::MAX) as usize
            + (self.exercise_category.0 != u16::MAX) as usize
            + (self.exercise_name != u16::MAX) as usize
            + (!self.wkt_step_name.is_empty()) as usize
    }
}

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

impl From<&Message> for ExerciseTitle {
    /// from creates new ExerciseTitle struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [7, 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.exercise_category = typedef::ExerciseCategory(field.value.as_u16()),
                1 => v.exercise_name = field.value.as_u16(),
                2 => v.wkt_step_name = field.value.to_vec_string(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if m.message_index.0 != u16::MAX {
            fields.push(Field {
                num: 254,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.message_index.0),
                is_expanded: false,
            });
        };
        if m.exercise_category.0 != u16::MAX {
            fields.push(Field {
                num: 0,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.exercise_category.0),
                is_expanded: false,
            });
        };
        if m.exercise_name != u16::MAX {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.exercise_name),
                is_expanded: false,
            });
        };
        if !m.wkt_step_name.is_empty() {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::STRING,
                value: Value::VecString(m.wkt_step_name),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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

#[cfg(feature = "serde")]
impl Serialize for ExerciseTitle {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let n = self.count_valid_fields() + 2;
        let mut state = serializer.serialize_struct("ExerciseTitle", n)?;
        if self.message_index.0 != u16::MAX {
            state.serialize_field("message_index", &self.message_index)?;
        }
        if self.exercise_category.0 != u16::MAX {
            state.serialize_field("exercise_category", &self.exercise_category)?;
        }
        if self.exercise_name != u16::MAX {
            state.serialize_field("exercise_name", &self.exercise_name)?;
        }
        if !self.wkt_step_name.is_empty() {
            state.serialize_field("wkt_step_name", &self.wkt_step_name)?;
        }
        if !self.unknown_fields.is_empty() {
            state.serialize_field("unknown_fields", &self.unknown_fields)?;
        }
        if !self.developer_fields.is_empty() {
            state.serialize_field("developer_fields", &self.developer_fields)?;
        }
        state.end()
    }
}

#[cfg(feature = "serde")]
#[cfg_attr(feature = "serde", derive(Deserialize), serde(default))]
struct De {
    message_index: typedef::MessageIndex,
    exercise_category: typedef::ExerciseCategory,
    exercise_name: u16,
    wkt_step_name: Vec<String>,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for ExerciseTitle {
    fn from(m: De) -> Self {
        Self {
            message_index: m.message_index,
            exercise_category: m.exercise_category,
            exercise_name: m.exercise_name,
            wkt_step_name: m.wkt_step_name,
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            exercise_category: typedef::ExerciseCategory(u16::MAX),
            exercise_name: u16::MAX,
            wkt_step_name: Vec::new(),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}