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)]
/// DiveApneaAlarm is a DiveApneaAlarm message.
pub struct DiveApneaAlarm {
    /// Index of the alarm
    pub message_index: typedef::MessageIndex,
    /// Scale: 1000; Units: m; Depth setting (m) for depth type alarms
    pub depth: u32,
    /// Units: s; Time setting (s) for time type alarms
    pub time: i32,
    /// Enablement flag
    pub enabled: typedef::Bool,
    /// Alarm type setting
    pub alarm_type: typedef::DiveAlarmType,
    /// Tone and Vibe setting for the alarm.
    pub sound: typedef::Tone,
    /// Dive types the alarm will trigger on
    pub dive_types: Vec<typedef::SubSport>,
    /// Alarm ID
    pub id: u32,
    /// Show a visible pop-up for this alarm
    pub popup_enabled: typedef::Bool,
    /// Trigger the alarm on descent
    pub trigger_on_descent: typedef::Bool,
    /// Trigger the alarm on ascent
    pub trigger_on_ascent: typedef::Bool,
    /// Repeat alarm each time threshold is crossed?
    pub repeating: typedef::Bool,
    /// Scale: 1000; Units: mps; Ascent/descent rate (mps) setting for speed type alarms
    pub speed: i32,
    /// 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 DiveApneaAlarm {
    /// Value's type: `u16`
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `u32`; Scale: `1000`; Units: `m`
    pub const DEPTH: u8 = 0;
    /// Value's type: `i32`; Units: `s`
    pub const TIME: u8 = 1;
    /// Value's type: `u8`
    pub const ENABLED: u8 = 2;
    /// Value's type: `u8`
    pub const ALARM_TYPE: u8 = 3;
    /// Value's type: `u8`
    pub const SOUND: u8 = 4;
    /// Value's type: `Vec<u8>`
    pub const DIVE_TYPES: u8 = 5;
    /// Value's type: `u32`
    pub const ID: u8 = 6;
    /// Value's type: `u8`
    pub const POPUP_ENABLED: u8 = 7;
    /// Value's type: `u8`
    pub const TRIGGER_ON_DESCENT: u8 = 8;
    /// Value's type: `u8`
    pub const TRIGGER_ON_ASCENT: u8 = 9;
    /// Value's type: `u8`
    pub const REPEATING: u8 = 10;
    /// Value's type: `i32`; Scale: `1000`; Units: `mps`
    pub const SPEED: u8 = 11;

    /// Create new DiveApneaAlarm with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            depth: u32::MAX,
            time: i32::MAX,
            enabled: typedef::Bool(u8::MAX),
            alarm_type: typedef::DiveAlarmType(u8::MAX),
            sound: typedef::Tone(u8::MAX),
            dive_types: Vec::<typedef::SubSport>::new(),
            id: u32::MAX,
            popup_enabled: typedef::Bool(u8::MAX),
            trigger_on_descent: typedef::Bool(u8::MAX),
            trigger_on_ascent: typedef::Bool(u8::MAX),
            repeating: typedef::Bool(u8::MAX),
            speed: i32::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

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

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

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

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

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

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

        const KNOWN_NUMS: [u64; 4] = [4095, 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()),
            depth: vals[0].as_u32(),
            time: vals[1].as_i32(),
            enabled: typedef::Bool(vals[2].as_u8()),
            alarm_type: typedef::DiveAlarmType(vals[3].as_u8()),
            sound: typedef::Tone(vals[4].as_u8()),
            dive_types: match &vals[5] {
                Value::VecUint8(v) => {
                    let mut vs = Vec::with_capacity(v.len());
                    for x in v {
                        vs.push(typedef::SubSport(*x))
                    }
                    vs
                }
                _ => Vec::new(),
            },
            id: vals[6].as_u32(),
            popup_enabled: typedef::Bool(vals[7].as_u8()),
            trigger_on_descent: typedef::Bool(vals[8].as_u8()),
            trigger_on_ascent: typedef::Bool(vals[9].as_u8()),
            repeating: typedef::Bool(vals[10].as_u8()),
            speed: vals[11].as_i32(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<DiveApneaAlarm> for Message {
    fn from(m: DiveApneaAlarm) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 13];
        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.depth != u32::MAX {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.depth),
                is_expanded: false,
            };
            len += 1;
        }
        if m.time != i32::MAX {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::SINT32,
                value: Value::Int32(m.time),
                is_expanded: false,
            };
            len += 1;
        }
        if m.enabled != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.enabled.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.alarm_type != typedef::DiveAlarmType(u8::MAX) {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::DIVE_ALARM_TYPE,
                value: Value::Uint8(m.alarm_type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.sound != typedef::Tone(u8::MAX) {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::TONE,
                value: Value::Uint8(m.sound.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.dive_types != Vec::<typedef::SubSport>::new() {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::SUB_SPORT,
                value: Value::VecUint8({
                    let mut v = Vec::with_capacity(m.dive_types.len());
                    for x in &m.dive_types {
                        v.push(x.0)
                    }
                    v
                }),
                is_expanded: false,
            };
            len += 1;
        }
        if m.id != u32::MAX {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.id),
                is_expanded: false,
            };
            len += 1;
        }
        if m.popup_enabled != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.popup_enabled.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.trigger_on_descent != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.trigger_on_descent.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.trigger_on_ascent != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.trigger_on_ascent.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.repeating != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.repeating.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.speed != i32::MAX {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::SINT32,
                value: Value::Int32(m.speed),
                is_expanded: false,
            };
            len += 1;
        }

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