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::borrow::ToOwned;
use alloc::string::String;
use alloc::vec::Vec;

#[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
    }

    fn count_valid_fields(&self) -> usize {
        (self.message_index != typedef::MessageIndex(u16::MAX)) as usize
            + (!self.wkt_step_name.is_empty()) as usize
            + (self.duration_type != typedef::WktStepDuration(u8::MAX)) as usize
            + (self.duration_value != u32::MAX) as usize
            + (self.target_type != typedef::WktStepTarget(u8::MAX)) as usize
            + (self.target_value != u32::MAX) as usize
            + (self.custom_target_value_low != u32::MAX) as usize
            + (self.custom_target_value_high != u32::MAX) as usize
            + (self.intensity != typedef::Intensity(u8::MAX)) as usize
            + (!self.notes.is_empty()) as usize
            + (self.equipment != typedef::WorkoutEquipment(u8::MAX)) as usize
            + (self.exercise_category != typedef::ExerciseCategory(u16::MAX)) as usize
            + (self.exercise_name != u16::MAX) as usize
            + (self.exercise_weight != u16::MAX) as usize
            + (self.weight_display_unit != typedef::FitBaseUnit(u16::MAX)) as usize
            + (self.secondary_target_type != typedef::WktStepTarget(u8::MAX)) as usize
            + (self.secondary_target_value != u32::MAX) as usize
            + (self.secondary_custom_target_value_low != u32::MAX) as usize
            + (self.secondary_custom_target_value_high != u32::MAX) as usize
    }
}

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 {
        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 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.wkt_step_name = field.value.as_str().to_owned(),
                1 => v.duration_type = typedef::WktStepDuration(field.value.as_u8()),
                2 => v.duration_value = field.value.as_u32(),
                3 => v.target_type = typedef::WktStepTarget(field.value.as_u8()),
                4 => v.target_value = field.value.as_u32(),
                5 => v.custom_target_value_low = field.value.as_u32(),
                6 => v.custom_target_value_high = field.value.as_u32(),
                7 => v.intensity = typedef::Intensity(field.value.as_u8()),
                8 => v.notes = field.value.as_str().to_owned(),
                9 => v.equipment = typedef::WorkoutEquipment(field.value.as_u8()),
                10 => v.exercise_category = typedef::ExerciseCategory(field.value.as_u16()),
                11 => v.exercise_name = field.value.as_u16(),
                12 => v.exercise_weight = field.value.as_u16(),
                13 => v.weight_display_unit = typedef::FitBaseUnit(field.value.as_u16()),
                19 => v.secondary_target_type = typedef::WktStepTarget(field.value.as_u8()),
                20 => v.secondary_target_value = field.value.as_u32(),
                21 => v.secondary_custom_target_value_low = field.value.as_u32(),
                22 => v.secondary_custom_target_value_high = field.value.as_u32(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

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

        fields.extend_from_slice(&m.unknown_fields);

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