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)]
/// Set is a Set message.
pub struct Set {
    /// Timestamp of the set
    pub timestamp: typedef::DateTime,
    /// Scale: 1000; Units: s
    pub duration: u32,
    /// # of repitions of the movement
    pub repetitions: u16,
    /// Scale: 16; Units: kg; Amount of weight applied for the set
    pub weight: u16,
    pub set_type: typedef::SetType,
    /// Start time of the set
    pub start_time: typedef::DateTime,
    pub category: Vec<typedef::ExerciseCategory>,
    /// Based on the associated category, see [category]_exercise_names
    pub category_subtype: Vec<u16>,
    pub weight_display_unit: typedef::FitBaseUnit,
    pub message_index: typedef::MessageIndex,
    pub wkt_step_index: typedef::MessageIndex,
    /// 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 Set {
    /// Value's type: `u32`
    pub const TIMESTAMP: u8 = 254;
    /// Value's type: `u32`; Scale: `1000`; Units: `s`
    pub const DURATION: u8 = 0;
    /// Value's type: `u16`
    pub const REPETITIONS: u8 = 3;
    /// Value's type: `u16`; Scale: `16`; Units: `kg`
    pub const WEIGHT: u8 = 4;
    /// Value's type: `u8`
    pub const SET_TYPE: u8 = 5;
    /// Value's type: `u32`
    pub const START_TIME: u8 = 6;
    /// Value's type: `Vec<u16>`
    pub const CATEGORY: u8 = 7;
    /// Value's type: `Vec<u16>`
    pub const CATEGORY_SUBTYPE: u8 = 8;
    /// Value's type: `u16`
    pub const WEIGHT_DISPLAY_UNIT: u8 = 9;
    /// Value's type: `u16`
    pub const MESSAGE_INDEX: u8 = 10;
    /// Value's type: `u16`
    pub const WKT_STEP_INDEX: u8 = 11;

    /// Create new Set with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            timestamp: typedef::DateTime(u32::MAX),
            duration: u32::MAX,
            repetitions: u16::MAX,
            weight: u16::MAX,
            set_type: typedef::SetType(u8::MAX),
            start_time: typedef::DateTime(u32::MAX),
            category: Vec::new(),
            category_subtype: Vec::new(),
            weight_display_unit: typedef::FitBaseUnit(u16::MAX),
            message_index: typedef::MessageIndex(u16::MAX),
            wkt_step_index: typedef::MessageIndex(u16::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

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

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

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

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

    fn count_valid_fields(&self) -> usize {
        (self.timestamp != typedef::DateTime(u32::MAX)) as usize
            + (self.duration != u32::MAX) as usize
            + (self.repetitions != u16::MAX) as usize
            + (self.weight != u16::MAX) as usize
            + (self.set_type != typedef::SetType(u8::MAX)) as usize
            + (self.start_time != typedef::DateTime(u32::MAX)) as usize
            + (!self.category.is_empty()) as usize
            + (!self.category_subtype.is_empty()) as usize
            + (self.weight_display_unit != typedef::FitBaseUnit(u16::MAX)) as usize
            + (self.message_index != typedef::MessageIndex(u16::MAX)) as usize
            + (self.wkt_step_index != typedef::MessageIndex(u16::MAX)) as usize
    }
}

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

impl From<&Message> for Set {
    /// from creates new Set struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [4089, 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.timestamp = typedef::DateTime(field.value.as_u32()),
                0 => v.duration = field.value.as_u32(),
                3 => v.repetitions = field.value.as_u16(),
                4 => v.weight = field.value.as_u16(),
                5 => v.set_type = typedef::SetType(field.value.as_u8()),
                6 => v.start_time = typedef::DateTime(field.value.as_u32()),
                7 => {
                    v.category = match &field.value {
                        Value::VecUint16(v) => {
                            let mut vs = Vec::with_capacity(v.len());
                            vs.extend(v.iter().map(|&x| typedef::ExerciseCategory(x)));
                            vs
                        }
                        _ => Vec::new(),
                    }
                }
                8 => v.category_subtype = field.value.to_vec_u16(),
                9 => v.weight_display_unit = typedef::FitBaseUnit(field.value.as_u16()),
                10 => v.message_index = typedef::MessageIndex(field.value.as_u16()),
                11 => v.wkt_step_index = typedef::MessageIndex(field.value.as_u16()),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if m.timestamp != typedef::DateTime(u32::MAX) {
            fields.push(Field {
                num: 254,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.timestamp.0),
                is_expanded: false,
            });
        };
        if m.duration != u32::MAX {
            fields.push(Field {
                num: 0,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.duration),
                is_expanded: false,
            });
        };
        if m.repetitions != u16::MAX {
            fields.push(Field {
                num: 3,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.repetitions),
                is_expanded: false,
            });
        };
        if m.weight != u16::MAX {
            fields.push(Field {
                num: 4,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.weight),
                is_expanded: false,
            });
        };
        if m.set_type != typedef::SetType(u8::MAX) {
            fields.push(Field {
                num: 5,
                profile_type: ProfileType::SET_TYPE,
                value: Value::Uint8(m.set_type.0),
                is_expanded: false,
            });
        };
        if m.start_time != typedef::DateTime(u32::MAX) {
            fields.push(Field {
                num: 6,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.start_time.0),
                is_expanded: false,
            });
        };
        if !m.category.is_empty() {
            fields.push(Field {
                num: 7,
                profile_type: ProfileType::EXERCISE_CATEGORY,
                value: Value::VecUint16({
                    let (ptr, len, capacity) = m.category.into_raw_parts();
                    unsafe { Vec::from_raw_parts(ptr.cast::<u16>(), len, capacity) }
                }),
                is_expanded: false,
            });
        };
        if !m.category_subtype.is_empty() {
            fields.push(Field {
                num: 8,
                profile_type: ProfileType::UINT16,
                value: Value::VecUint16(m.category_subtype),
                is_expanded: false,
            });
        };
        if m.weight_display_unit != typedef::FitBaseUnit(u16::MAX) {
            fields.push(Field {
                num: 9,
                profile_type: ProfileType::FIT_BASE_UNIT,
                value: Value::Uint16(m.weight_display_unit.0),
                is_expanded: false,
            });
        };
        if m.message_index != typedef::MessageIndex(u16::MAX) {
            fields.push(Field {
                num: 10,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.message_index.0),
                is_expanded: false,
            });
        };
        if m.wkt_step_index != typedef::MessageIndex(u16::MAX) {
            fields.push(Field {
                num: 11,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.wkt_step_index.0),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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