rustyfit 0.10.0

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.

use crate::profile::typedef::{self, FitBaseType};
use crate::proto::*;
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};

/// Activity message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct Activity {
    pub timestamp: typedef::DateTime,
    /// Scale: 1000; Units: s; Exclude pauses
    pub total_timer_time: u32,
    pub num_sessions: u16,
    pub r#type: typedef::Activity,
    pub event: typedef::Event,
    pub event_type: typedef::EventType,
    /// timestamp epoch expressed in local time, used to convert activity timestamps to local time
    pub local_timestamp: typedef::LocalDateTime,
    pub event_group: u8,
    /// 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 Activity {
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::DateTime
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::Uint32; Scale: `1000`; Units: `s`
    pub const TOTAL_TIMER_TIME: u8 = 0;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16
    pub const NUM_SESSIONS: u8 = 1;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::Activity
    pub const TYPE: u8 = 2;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::Event
    pub const EVENT: u8 = 3;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::EventType
    pub const EVENT_TYPE: u8 = 4;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::LocalDateTime
    pub const LOCAL_TIMESTAMP: u8 = 5;
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::Uint8
    pub const EVENT_GROUP: u8 = 6;

    /// Create new Activity with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            timestamp: typedef::DateTime(u32::MAX),
            total_timer_time: u32::MAX,
            num_sessions: u16::MAX,
            r#type: typedef::Activity(u8::MAX),
            event: typedef::Event(u8::MAX),
            event_type: typedef::EventType(u8::MAX),
            local_timestamp: typedef::LocalDateTime(u32::MAX),
            event_group: u8::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `total_timer_time` in its scaled value. It returns `None` when value is invalid.
    ///
    /// Units: s
    pub fn total_timer_time_scaled(&self) -> Option<f64> {
        if self.total_timer_time == u32::MAX {
            return None;
        }
        Some(self.total_timer_time as f64 / 1000.0 - 0.0)
    }

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

    fn count_valid_fields(&self) -> usize {
        (self.timestamp.0 != u32::MAX) as usize
            + (self.total_timer_time != u32::MAX) as usize
            + (self.num_sessions != u16::MAX) as usize
            + (self.r#type.0 != u8::MAX) as usize
            + (self.event.0 != u8::MAX) as usize
            + (self.event_type.0 != u8::MAX) as usize
            + (self.local_timestamp.0 != u32::MAX) as usize
            + (self.event_group != u8::MAX) as usize
    }
}

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

impl From<&Message> for Activity {
    /// from creates new Activity struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [127, 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.total_timer_time = field.value.as_u32(),
                1 => v.num_sessions = field.value.as_u16(),
                2 => v.r#type = typedef::Activity(field.value.as_u8()),
                3 => v.event = typedef::Event(field.value.as_u8()),
                4 => v.event_type = typedef::EventType(field.value.as_u8()),
                5 => v.local_timestamp = typedef::LocalDateTime(field.value.as_u32()),
                6 => v.event_group = field.value.as_u8(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if m.timestamp.0 != u32::MAX {
            fields.push(Field {
                num: 253,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.timestamp.0),
                is_expanded: false,
            });
        };
        if m.total_timer_time != u32::MAX {
            fields.push(Field {
                num: 0,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.total_timer_time),
                is_expanded: false,
            });
        };
        if m.num_sessions != u16::MAX {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.num_sessions),
                is_expanded: false,
            });
        };
        if m.r#type.0 != u8::MAX {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.r#type.0),
                is_expanded: false,
            });
        };
        if m.event.0 != u8::MAX {
            fields.push(Field {
                num: 3,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.event.0),
                is_expanded: false,
            });
        };
        if m.event_type.0 != u8::MAX {
            fields.push(Field {
                num: 4,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.event_type.0),
                is_expanded: false,
            });
        };
        if m.local_timestamp.0 != u32::MAX {
            fields.push(Field {
                num: 5,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.local_timestamp.0),
                is_expanded: false,
            });
        };
        if m.event_group != u8::MAX {
            fields.push(Field {
                num: 6,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.event_group),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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

#[cfg(feature = "serde")]
impl Serialize for Activity {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let n = self.count_valid_fields() + 2;
        let mut state = serializer.serialize_struct("Activity", n)?;
        if let Some(v) = self.timestamp.unix_timestamp() {
            state.serialize_field("timestamp", &v)?;
        }
        if let Some(v) = self.total_timer_time_scaled() {
            state.serialize_field("total_timer_time", &v)?;
        }
        if self.num_sessions != u16::MAX {
            state.serialize_field("num_sessions", &self.num_sessions)?;
        }
        if self.r#type.0 != u8::MAX {
            state.serialize_field("type", &self.r#type)?;
        }
        if self.event.0 != u8::MAX {
            state.serialize_field("event", &self.event)?;
        }
        if self.event_type.0 != u8::MAX {
            state.serialize_field("event_type", &self.event_type)?;
        }
        if let Some(v) = self.local_timestamp.unix_timestamp() {
            state.serialize_field("local_timestamp", &v)?;
        }
        if self.event_group != u8::MAX {
            state.serialize_field("event_group", &self.event_group)?;
        }
        if !self.unknown_fields.is_empty() {
            state.serialize_field("unknown_fields", &self.unknown_fields)?;
        }
        if !self.developer_fields.is_empty() {
            state.serialize_field("developer_fields", &self.developer_fields)?;
        }
        state.end()
    }
}

#[cfg(feature = "serde")]
#[cfg_attr(feature = "serde", derive(Deserialize), serde(default))]
struct De {
    timestamp: Option<i64>,
    total_timer_time: f64,
    num_sessions: u16,
    r#type: typedef::Activity,
    event: typedef::Event,
    event_type: typedef::EventType,
    local_timestamp: Option<i64>,
    event_group: u8,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for Activity {
    fn from(m: De) -> Self {
        Self {
            timestamp: m.timestamp.map_or_else(
                || typedef::DateTime(u32::MAX),
                typedef::DateTime::from_unix_timestamp,
            ),
            total_timer_time: {
                let unscaled = (m.total_timer_time + 0.0) * 1000.0;
                if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
                    u32::MAX
                } else {
                    unscaled as u32
                }
            },
            num_sessions: m.num_sessions,
            r#type: m.r#type,
            event: m.event,
            event_type: m.event_type,
            local_timestamp: m.local_timestamp.map_or_else(
                || typedef::LocalDateTime(u32::MAX),
                typedef::LocalDateTime::from_unix_timestamp,
            ),
            event_group: m.event_group,
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            timestamp: None,
            total_timer_time: f64::from_bits(u64::MAX),
            num_sessions: u16::MAX,
            r#type: typedef::Activity(u8::MAX),
            event: typedef::Event(u8::MAX),
            event_type: typedef::EventType(u8::MAX),
            local_timestamp: None,
            event_group: u8::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}