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)]
/// 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::<typedef::ExerciseCategory>::new(),
            category_subtype: Vec::<u16>::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
    }
}

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 {
        let mut vals: [&Value; 255] = [const { &Value::Invalid }; 255];

        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 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 {
            timestamp: typedef::DateTime(vals[254].as_u32()),
            duration: vals[0].as_u32(),
            repetitions: vals[3].as_u16(),
            weight: vals[4].as_u16(),
            set_type: typedef::SetType(vals[5].as_u8()),
            start_time: typedef::DateTime(vals[6].as_u32()),
            category: match &vals[7] {
                Value::VecUint16(v) => {
                    let mut vs = Vec::with_capacity(v.len());
                    for x in v {
                        vs.push(typedef::ExerciseCategory(*x))
                    }
                    vs
                }
                _ => Vec::new(),
            },
            category_subtype: vals[8].as_vec_u16(),
            weight_display_unit: typedef::FitBaseUnit(vals[9].as_u16()),
            message_index: typedef::MessageIndex(vals[10].as_u16()),
            wkt_step_index: typedef::MessageIndex(vals[11].as_u16()),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<Set> for Message {
    fn from(m: Set) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 11];
        let mut len = 0usize;

        if m.timestamp != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 254,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.timestamp.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.duration != u32::MAX {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.duration),
                is_expanded: false,
            };
            len += 1;
        }
        if m.repetitions != u16::MAX {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.repetitions),
                is_expanded: false,
            };
            len += 1;
        }
        if m.weight != u16::MAX {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.weight),
                is_expanded: false,
            };
            len += 1;
        }
        if m.set_type != typedef::SetType(u8::MAX) {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::SET_TYPE,
                value: Value::Uint8(m.set_type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.start_time != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.start_time.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.category != Vec::<typedef::ExerciseCategory>::new() {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::EXERCISE_CATEGORY,
                value: Value::VecUint16({
                    let mut v = Vec::with_capacity(m.category.len());
                    for x in &m.category {
                        v.push(x.0)
                    }
                    v
                }),
                is_expanded: false,
            };
            len += 1;
        }
        if m.category_subtype != Vec::<u16>::new() {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::UINT16,
                value: Value::VecUint16(m.category_subtype),
                is_expanded: false,
            };
            len += 1;
        }
        if m.weight_display_unit != typedef::FitBaseUnit(u16::MAX) {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::FIT_BASE_UNIT,
                value: Value::Uint16(m.weight_display_unit.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.message_index != typedef::MessageIndex(u16::MAX) {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.message_index.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.wkt_step_index != typedef::MessageIndex(u16::MAX) {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.wkt_step_index.0),
                is_expanded: false,
            };
            len += 1;
        }

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