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 crate::semconv;
use alloc::vec::Vec;

#[derive(Debug, Clone)]
/// GpsMetadata is a GpsMetadata message.
pub struct GpsMetadata {
    /// Units: s; Whole second part of the timestamp.
    pub timestamp: typedef::DateTime,
    /// Units: ms; Millisecond part of the timestamp.
    pub timestamp_ms: u16,
    /// Units: semicircles
    pub position_lat: i32,
    /// Units: semicircles
    pub position_long: i32,
    /// Scale: 5; Offset: 500; Units: m
    pub enhanced_altitude: u32,
    /// Scale: 1000; Units: m/s
    pub enhanced_speed: u32,
    /// Scale: 100; Units: degrees
    pub heading: u16,
    /// Units: s; Used to correlate UTC to system time if the timestamp of the message is in system time. This UTC time is derived from the GPS data.
    pub utc_timestamp: typedef::DateTime,
    /// Array: [3]; Scale: 100; Units: m/s; velocity[0] is lon velocity. Velocity[1] is lat velocity. Velocity[2] is altitude velocity.
    pub velocity: [i16; 3],
    /// 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 GpsMetadata {
    /// Value's type: `u32`; Units: `s`
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u16`; Units: `ms`
    pub const TIMESTAMP_MS: u8 = 0;
    /// Value's type: `i32`; Units: `semicircles`
    pub const POSITION_LAT: u8 = 1;
    /// Value's type: `i32`; Units: `semicircles`
    pub const POSITION_LONG: u8 = 2;
    /// Value's type: `u32`; Scale: `5`; Offset: `500`; Units: `m`
    pub const ENHANCED_ALTITUDE: u8 = 3;
    /// Value's type: `u32`; Scale: `1000`; Units: `m/s`
    pub const ENHANCED_SPEED: u8 = 4;
    /// Value's type: `u16`; Scale: `100`; Units: `degrees`
    pub const HEADING: u8 = 5;
    /// Value's type: `u32`; Units: `s`
    pub const UTC_TIMESTAMP: u8 = 6;
    /// Value's type: `[i16; 3]`; Scale: `100`; Units: `m/s`
    pub const VELOCITY: u8 = 7;

    /// Create new GpsMetadata with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            timestamp: typedef::DateTime(u32::MAX),
            timestamp_ms: u16::MAX,
            position_lat: i32::MAX,
            position_long: i32::MAX,
            enhanced_altitude: u32::MAX,
            enhanced_speed: u32::MAX,
            heading: u16::MAX,
            utc_timestamp: typedef::DateTime(u32::MAX),
            velocity: [i16::MAX; 3],
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `position_lat` in degrees instead of semicircles. It returns invalid f64 when value is valid.
    pub fn position_lat_degrees(&self) -> f64 {
        semconv::to_degrees(self.position_lat)
    }

    /// Returns `position_long` in degrees instead of semicircles. It returns invalid f64 when value is valid.
    pub fn position_long_degrees(&self) -> f64 {
        semconv::to_degrees(self.position_long)
    }

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

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

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

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

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

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

    /// Returns `velocity` in its scaled value. It returns invalid f64 when value is valid.
    pub fn velocity_scaled(&self) -> [f64; 3] {
        let mut v = [f64::from_bits(u64::MAX); 3];
        for (i, &x) in self.velocity.iter().enumerate() {
            if x == i16::MAX {
                continue;
            }
            v[i] = x as f64 / 100.0 - 0.0;
        }
        v
    }

    /// Set `velocity` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_velocity_scaled(&mut self, v: [f64; 3]) -> &mut GpsMetadata {
        self.velocity = [i16::MAX; 3];
        for (i, &x) in v.iter().enumerate() {
            let unscaled = (x + 0.0) * 100.0;
            if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64 {
                continue;
            }
            self.velocity[i] = (unscaled as i16);
        }
        self
    }

    fn count_valid_fields(&self) -> usize {
        (self.timestamp != typedef::DateTime(u32::MAX)) as usize
            + (self.timestamp_ms != u16::MAX) as usize
            + (self.position_lat != i32::MAX) as usize
            + (self.position_long != i32::MAX) as usize
            + (self.enhanced_altitude != u32::MAX) as usize
            + (self.enhanced_speed != u32::MAX) as usize
            + (self.heading != u16::MAX) as usize
            + (self.utc_timestamp != typedef::DateTime(u32::MAX)) as usize
            + (self.velocity != [i16::MAX; 3]) as usize
    }
}

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

impl From<&Message> for GpsMetadata {
    /// from creates new GpsMetadata struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [255, 0, 0, 2305843009213693952];
        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 {
                253 => v.timestamp = typedef::DateTime(field.value.as_u32()),
                0 => v.timestamp_ms = field.value.as_u16(),
                1 => v.position_lat = field.value.as_i32(),
                2 => v.position_long = field.value.as_i32(),
                3 => v.enhanced_altitude = field.value.as_u32(),
                4 => v.enhanced_speed = field.value.as_u32(),
                5 => v.heading = field.value.as_u16(),
                6 => v.utc_timestamp = typedef::DateTime(field.value.as_u32()),
                7 => {
                    v.velocity = match &field.value {
                        Value::VecInt16(v) => {
                            let mut arr = [i16::MAX; 3];
                            for (i, x) in v.iter().take(3).enumerate() {
                                arr[i] = *x;
                            }
                            arr
                        }
                        _ => [i16::MAX; 3],
                    }
                }
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if m.timestamp != typedef::DateTime(u32::MAX) {
            fields.push(Field {
                num: 253,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.timestamp.0),
                is_expanded: false,
            });
        };
        if m.timestamp_ms != u16::MAX {
            fields.push(Field {
                num: 0,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.timestamp_ms),
                is_expanded: false,
            });
        };
        if m.position_lat != i32::MAX {
            fields.push(Field {
                num: 1,
                profile_type: ProfileType::SINT32,
                value: Value::Int32(m.position_lat),
                is_expanded: false,
            });
        };
        if m.position_long != i32::MAX {
            fields.push(Field {
                num: 2,
                profile_type: ProfileType::SINT32,
                value: Value::Int32(m.position_long),
                is_expanded: false,
            });
        };
        if m.enhanced_altitude != u32::MAX {
            fields.push(Field {
                num: 3,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.enhanced_altitude),
                is_expanded: false,
            });
        };
        if m.enhanced_speed != u32::MAX {
            fields.push(Field {
                num: 4,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.enhanced_speed),
                is_expanded: false,
            });
        };
        if m.heading != u16::MAX {
            fields.push(Field {
                num: 5,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.heading),
                is_expanded: false,
            });
        };
        if m.utc_timestamp != typedef::DateTime(u32::MAX) {
            fields.push(Field {
                num: 6,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.utc_timestamp.0),
                is_expanded: false,
            });
        };
        if m.velocity != [i16::MAX; 3] {
            fields.push(Field {
                num: 7,
                profile_type: ProfileType::SINT16,
                value: Value::VecInt16(Vec::from(&m.velocity)),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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