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)]
/// Workout is a Workout message.
pub struct Workout {
    pub message_index: typedef::MessageIndex,
    pub sport: typedef::Sport,
    /// Base: UINT32Z
    pub capabilities: typedef::WorkoutCapabilities,
    /// number of valid steps
    pub num_valid_steps: u16,
    pub wkt_name: String,
    pub sub_sport: typedef::SubSport,
    /// Scale: 100; Units: m
    pub pool_length: u16,
    pub pool_length_unit: typedef::DisplayMeasure,
    /// Description of the workout
    pub wkt_description: String,
    /// 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 Workout {
    /// Value's type: `u16`
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `u8`
    pub const SPORT: u8 = 4;
    /// Value's type: `u32`; Base: UINT32Z
    pub const CAPABILITIES: u8 = 5;
    /// Value's type: `u16`
    pub const NUM_VALID_STEPS: u8 = 6;
    /// Value's type: `String`
    pub const WKT_NAME: u8 = 8;
    /// Value's type: `u8`
    pub const SUB_SPORT: u8 = 11;
    /// Value's type: `u16`; Scale: `100`; Units: `m`
    pub const POOL_LENGTH: u8 = 14;
    /// Value's type: `u8`
    pub const POOL_LENGTH_UNIT: u8 = 15;
    /// Value's type: `String`
    pub const WKT_DESCRIPTION: u8 = 17;

    /// Create new Workout with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            sport: typedef::Sport(u8::MAX),
            capabilities: typedef::WorkoutCapabilities(u32::MIN),
            num_valid_steps: u16::MAX,
            wkt_name: String::new(),
            sub_sport: typedef::SubSport(u8::MAX),
            pool_length: u16::MAX,
            pool_length_unit: typedef::DisplayMeasure(u8::MAX),
            wkt_description: String::new(),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

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

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

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

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

        const KNOWN_NUMS: [u64; 4] = [182640, 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()),
            sport: typedef::Sport(vals[4].as_u8()),
            capabilities: typedef::WorkoutCapabilities(vals[5].as_u32z()),
            num_valid_steps: vals[6].as_u16(),
            wkt_name: vals[8].as_string(),
            sub_sport: typedef::SubSport(vals[11].as_u8()),
            pool_length: vals[14].as_u16(),
            pool_length_unit: typedef::DisplayMeasure(vals[15].as_u8()),
            wkt_description: vals[17].as_string(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<Workout> for Message {
    fn from(m: Workout) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 9];
        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.sport != typedef::Sport(u8::MAX) {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::SPORT,
                value: Value::Uint8(m.sport.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.capabilities != typedef::WorkoutCapabilities(u32::MIN) {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::WORKOUT_CAPABILITIES,
                value: Value::Uint32(m.capabilities.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.num_valid_steps != u16::MAX {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.num_valid_steps),
                is_expanded: false,
            };
            len += 1;
        }
        if m.wkt_name != String::new() {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::STRING,
                value: Value::String(m.wkt_name),
                is_expanded: false,
            };
            len += 1;
        }
        if m.sub_sport != typedef::SubSport(u8::MAX) {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::SUB_SPORT,
                value: Value::Uint8(m.sub_sport.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.pool_length != u16::MAX {
            arr[len] = Field {
                num: 14,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.pool_length),
                is_expanded: false,
            };
            len += 1;
        }
        if m.pool_length_unit != typedef::DisplayMeasure(u8::MAX) {
            arr[len] = Field {
                num: 15,
                profile_type: ProfileType::DISPLAY_MEASURE,
                value: Value::Uint8(m.pool_length_unit.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.wkt_description != String::new() {
            arr[len] = Field {
                num: 17,
                profile_type: ProfileType::STRING,
                value: Value::String(m.wkt_description),
                is_expanded: false,
            };
            len += 1;
        }

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