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::vec::Vec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};

/// Training Settings message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct TrainingSettings {
    /// Scale: 100; Units: m
    pub target_distance: u32,
    /// Scale: 1000; Units: m/s
    pub target_speed: u16,
    /// Units: s
    pub target_time: u32,
    /// Scale: 1e+06; Units: m/s; A more precise target speed field
    pub precise_target_speed: u32,
    /// 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 TrainingSettings {
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::Uint32; Scale: `100`; Units: `m`
    pub const TARGET_DISTANCE: u8 = 31;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16; Scale: `1000`; Units: `m/s`
    pub const TARGET_SPEED: u8 = 32;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::Uint32; Units: `s`
    pub const TARGET_TIME: u8 = 33;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::Uint32; Scale: `1e+06`; Units: `m/s`
    pub const PRECISE_TARGET_SPEED: u8 = 153;

    /// Create new TrainingSettings with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            target_distance: u32::MAX,
            target_speed: u16::MAX,
            target_time: u32::MAX,
            precise_target_speed: u32::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `target_distance` in its scaled value. It returns `None` when value is invalid.
    ///
    /// Units: m
    pub fn target_distance_scaled(&self) -> Option<f64> {
        if self.target_distance == u32::MAX {
            return None;
        }
        Some(self.target_distance as f64 / 100.0 - 0.0)
    }

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

    /// Returns `target_speed` in its scaled value. It returns `None` when value is invalid.
    ///
    /// Units: m/s
    pub fn target_speed_scaled(&self) -> Option<f64> {
        if self.target_speed == u16::MAX {
            return None;
        }
        Some(self.target_speed as f64 / 1000.0 - 0.0)
    }

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

    /// Returns `precise_target_speed` in its scaled value. It returns `None` when value is invalid.
    ///
    /// Units: m/s
    pub fn precise_target_speed_scaled(&self) -> Option<f64> {
        if self.precise_target_speed == u32::MAX {
            return None;
        }
        Some(self.precise_target_speed as f64 / 1000000.0 - 0.0)
    }

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

    fn count_valid_fields(&self) -> usize {
        (self.target_distance != u32::MAX) as usize
            + (self.target_speed != u16::MAX) as usize
            + (self.target_time != u32::MAX) as usize
            + (self.precise_target_speed != u32::MAX) as usize
    }
}

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

impl From<&Message> for TrainingSettings {
    /// from creates new TrainingSettings struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [15032385536, 0, 33554432, 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 {
                31 => v.target_distance = field.value.as_u32(),
                32 => v.target_speed = field.value.as_u16(),
                33 => v.target_time = field.value.as_u32(),
                153 => v.precise_target_speed = field.value.as_u32(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if m.target_distance != u32::MAX {
            fields.push(Field {
                num: 31,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.target_distance),
                is_expanded: false,
            });
        };
        if m.target_speed != u16::MAX {
            fields.push(Field {
                num: 32,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.target_speed),
                is_expanded: false,
            });
        };
        if m.target_time != u32::MAX {
            fields.push(Field {
                num: 33,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.target_time),
                is_expanded: false,
            });
        };
        if m.precise_target_speed != u32::MAX {
            fields.push(Field {
                num: 153,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.precise_target_speed),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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

#[cfg(feature = "serde")]
impl Serialize for TrainingSettings {
    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("TrainingSettings", n)?;
        if let Some(v) = self.target_distance_scaled() {
            state.serialize_field("target_distance", &v)?;
        }
        if let Some(v) = self.target_speed_scaled() {
            state.serialize_field("target_speed", &v)?;
        }
        if self.target_time != u32::MAX {
            state.serialize_field("target_time", &self.target_time)?;
        }
        if let Some(v) = self.precise_target_speed_scaled() {
            state.serialize_field("precise_target_speed", &v)?;
        }
        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 {
    target_distance: f64,
    target_speed: f64,
    target_time: u32,
    precise_target_speed: f64,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for TrainingSettings {
    fn from(m: De) -> Self {
        Self {
            target_distance: {
                let unscaled = (m.target_distance + 0.0) * 100.0;
                if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
                    u32::MAX
                } else {
                    unscaled as u32
                }
            },
            target_speed: {
                let unscaled = (m.target_speed + 0.0) * 1000.0;
                if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
                    u16::MAX
                } else {
                    unscaled as u16
                }
            },
            target_time: m.target_time,
            precise_target_speed: {
                let unscaled = (m.precise_target_speed + 0.0) * 1000000.0;
                if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
                    u32::MAX
                } else {
                    unscaled as u32
                }
            },
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            target_distance: f64::from_bits(u64::MAX),
            target_speed: f64::from_bits(u64::MAX),
            target_time: u32::MAX,
            precise_target_speed: f64::from_bits(u64::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}