rustyfit 0.4.1

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)]
/// WorkoutStep is a WorkoutStep message.
pub struct WorkoutStep {
    pub message_index: typedef::MessageIndex,
    pub wkt_step_name: String,
    pub duration_type: typedef::WktStepDuration,
    pub duration_value: u32,
    pub target_type: typedef::WktStepTarget,
    pub target_value: u32,
    pub custom_target_value_low: u32,
    pub custom_target_value_high: u32,
    pub intensity: typedef::Intensity,
    pub notes: String,
    pub equipment: typedef::WorkoutEquipment,
    pub exercise_category: typedef::ExerciseCategory,
    pub exercise_name: u16,
    /// Scale: 100; Units: kg
    pub exercise_weight: u16,
    pub weight_display_unit: typedef::FitBaseUnit,
    pub secondary_target_type: typedef::WktStepTarget,
    pub secondary_target_value: u32,
    pub secondary_custom_target_value_low: u32,
    pub secondary_custom_target_value_high: 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 WorkoutStep {
    /// Value's type: `u16`
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `String`
    pub const WKT_STEP_NAME: u8 = 0;
    /// Value's type: `u8`
    pub const DURATION_TYPE: u8 = 1;
    /// Value's type: `u32`
    pub const DURATION_VALUE: u8 = 2;
    /// Value's type: `u8`
    pub const TARGET_TYPE: u8 = 3;
    /// Value's type: `u32`
    pub const TARGET_VALUE: u8 = 4;
    /// Value's type: `u32`
    pub const CUSTOM_TARGET_VALUE_LOW: u8 = 5;
    /// Value's type: `u32`
    pub const CUSTOM_TARGET_VALUE_HIGH: u8 = 6;
    /// Value's type: `u8`
    pub const INTENSITY: u8 = 7;
    /// Value's type: `String`
    pub const NOTES: u8 = 8;
    /// Value's type: `u8`
    pub const EQUIPMENT: u8 = 9;
    /// Value's type: `u16`
    pub const EXERCISE_CATEGORY: u8 = 10;
    /// Value's type: `u16`
    pub const EXERCISE_NAME: u8 = 11;
    /// Value's type: `u16`; Scale: `100`; Units: `kg`
    pub const EXERCISE_WEIGHT: u8 = 12;
    /// Value's type: `u16`
    pub const WEIGHT_DISPLAY_UNIT: u8 = 13;
    /// Value's type: `u8`
    pub const SECONDARY_TARGET_TYPE: u8 = 19;
    /// Value's type: `u32`
    pub const SECONDARY_TARGET_VALUE: u8 = 20;
    /// Value's type: `u32`
    pub const SECONDARY_CUSTOM_TARGET_VALUE_LOW: u8 = 21;
    /// Value's type: `u32`
    pub const SECONDARY_CUSTOM_TARGET_VALUE_HIGH: u8 = 22;

    /// Create new WorkoutStep with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            wkt_step_name: String::new(),
            duration_type: typedef::WktStepDuration(u8::MAX),
            duration_value: u32::MAX,
            target_type: typedef::WktStepTarget(u8::MAX),
            target_value: u32::MAX,
            custom_target_value_low: u32::MAX,
            custom_target_value_high: u32::MAX,
            intensity: typedef::Intensity(u8::MAX),
            notes: String::new(),
            equipment: typedef::WorkoutEquipment(u8::MAX),
            exercise_category: typedef::ExerciseCategory(u16::MAX),
            exercise_name: u16::MAX,
            exercise_weight: u16::MAX,
            weight_display_unit: typedef::FitBaseUnit(u16::MAX),
            secondary_target_type: typedef::WktStepTarget(u8::MAX),
            secondary_target_value: u32::MAX,
            secondary_custom_target_value_low: u32::MAX,
            secondary_custom_target_value_high: u32::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

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

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

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

impl From<&Message> for WorkoutStep {
    /// from creates new WorkoutStep struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        let mut vals: [&Value; 255] = [const { &Value::Invalid }; 255];

        const KNOWN_NUMS: [u64; 4] = [7880703, 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 {
            message_index: typedef::MessageIndex(vals[254].as_u16()),
            wkt_step_name: vals[0].as_string(),
            duration_type: typedef::WktStepDuration(vals[1].as_u8()),
            duration_value: vals[2].as_u32(),
            target_type: typedef::WktStepTarget(vals[3].as_u8()),
            target_value: vals[4].as_u32(),
            custom_target_value_low: vals[5].as_u32(),
            custom_target_value_high: vals[6].as_u32(),
            intensity: typedef::Intensity(vals[7].as_u8()),
            notes: vals[8].as_string(),
            equipment: typedef::WorkoutEquipment(vals[9].as_u8()),
            exercise_category: typedef::ExerciseCategory(vals[10].as_u16()),
            exercise_name: vals[11].as_u16(),
            exercise_weight: vals[12].as_u16(),
            weight_display_unit: typedef::FitBaseUnit(vals[13].as_u16()),
            secondary_target_type: typedef::WktStepTarget(vals[19].as_u8()),
            secondary_target_value: vals[20].as_u32(),
            secondary_custom_target_value_low: vals[21].as_u32(),
            secondary_custom_target_value_high: vals[22].as_u32(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

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

        if m.message_index != typedef::MessageIndex(u16::MAX) {
            arr[len] = Field {
                num: 254,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.message_index.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.wkt_step_name != String::new() {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::STRING,
                value: Value::String(m.wkt_step_name),
                is_expanded: false,
            };
            len += 1;
        }
        if m.duration_type != typedef::WktStepDuration(u8::MAX) {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::WKT_STEP_DURATION,
                value: Value::Uint8(m.duration_type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.duration_value != u32::MAX {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.duration_value),
                is_expanded: false,
            };
            len += 1;
        }
        if m.target_type != typedef::WktStepTarget(u8::MAX) {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::WKT_STEP_TARGET,
                value: Value::Uint8(m.target_type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.target_value != u32::MAX {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.target_value),
                is_expanded: false,
            };
            len += 1;
        }
        if m.custom_target_value_low != u32::MAX {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.custom_target_value_low),
                is_expanded: false,
            };
            len += 1;
        }
        if m.custom_target_value_high != u32::MAX {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.custom_target_value_high),
                is_expanded: false,
            };
            len += 1;
        }
        if m.intensity != typedef::Intensity(u8::MAX) {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::INTENSITY,
                value: Value::Uint8(m.intensity.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.notes != String::new() {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::STRING,
                value: Value::String(m.notes),
                is_expanded: false,
            };
            len += 1;
        }
        if m.equipment != typedef::WorkoutEquipment(u8::MAX) {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::WORKOUT_EQUIPMENT,
                value: Value::Uint8(m.equipment.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.exercise_category != typedef::ExerciseCategory(u16::MAX) {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::EXERCISE_CATEGORY,
                value: Value::Uint16(m.exercise_category.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.exercise_name != u16::MAX {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.exercise_name),
                is_expanded: false,
            };
            len += 1;
        }
        if m.exercise_weight != u16::MAX {
            arr[len] = Field {
                num: 12,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.exercise_weight),
                is_expanded: false,
            };
            len += 1;
        }
        if m.weight_display_unit != typedef::FitBaseUnit(u16::MAX) {
            arr[len] = Field {
                num: 13,
                profile_type: ProfileType::FIT_BASE_UNIT,
                value: Value::Uint16(m.weight_display_unit.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.secondary_target_type != typedef::WktStepTarget(u8::MAX) {
            arr[len] = Field {
                num: 19,
                profile_type: ProfileType::WKT_STEP_TARGET,
                value: Value::Uint8(m.secondary_target_type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.secondary_target_value != u32::MAX {
            arr[len] = Field {
                num: 20,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.secondary_target_value),
                is_expanded: false,
            };
            len += 1;
        }
        if m.secondary_custom_target_value_low != u32::MAX {
            arr[len] = Field {
                num: 21,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.secondary_custom_target_value_low),
                is_expanded: false,
            };
            len += 1;
        }
        if m.secondary_custom_target_value_high != u32::MAX {
            arr[len] = Field {
                num: 22,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.secondary_custom_target_value_high),
                is_expanded: false,
            };
            len += 1;
        }

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