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 core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer, de, ser::SerializeStruct};

/// Event type.
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Event(pub u8);

impl Event {
    /// Group 0. Start / stop_all
    pub const TIMER: Event = Event(0);
    /// start / stop
    pub const WORKOUT: Event = Event(3);
    /// Start at beginning of workout. Stop at end of each step.
    pub const WORKOUT_STEP: Event = Event(4);
    /// stop_all group 0
    pub const POWER_DOWN: Event = Event(5);
    /// stop_all group 0
    pub const POWER_UP: Event = Event(6);
    /// start / stop group 0
    pub const OFF_COURSE: Event = Event(7);
    /// Stop at end of each session.
    pub const SESSION: Event = Event(8);
    /// Stop at end of each lap.
    pub const LAP: Event = Event(9);
    /// marker
    pub const COURSE_POINT: Event = Event(10);
    /// marker
    pub const BATTERY: Event = Event(11);
    /// Group 1. Start at beginning of activity if VP enabled, when VP pace is changed during activity or VP enabled mid activity. stop_disable when VP disabled.
    pub const VIRTUAL_PARTNER_PACE: Event = Event(12);
    /// Group 0. Start / stop when in alert condition.
    pub const HR_HIGH_ALERT: Event = Event(13);
    /// Group 0. Start / stop when in alert condition.
    pub const HR_LOW_ALERT: Event = Event(14);
    /// Group 0. Start / stop when in alert condition.
    pub const SPEED_HIGH_ALERT: Event = Event(15);
    /// Group 0. Start / stop when in alert condition.
    pub const SPEED_LOW_ALERT: Event = Event(16);
    /// Group 0. Start / stop when in alert condition.
    pub const CAD_HIGH_ALERT: Event = Event(17);
    /// Group 0. Start / stop when in alert condition.
    pub const CAD_LOW_ALERT: Event = Event(18);
    /// Group 0. Start / stop when in alert condition.
    pub const POWER_HIGH_ALERT: Event = Event(19);
    /// Group 0. Start / stop when in alert condition.
    pub const POWER_LOW_ALERT: Event = Event(20);
    /// marker
    pub const RECOVERY_HR: Event = Event(21);
    /// marker
    pub const BATTERY_LOW: Event = Event(22);
    /// Group 1. Start if enabled mid activity (not required at start of activity). Stop when duration is reached. stop_disable if disabled.
    pub const TIME_DURATION_ALERT: Event = Event(23);
    /// Group 1. Start if enabled mid activity (not required at start of activity). Stop when duration is reached. stop_disable if disabled.
    pub const DISTANCE_DURATION_ALERT: Event = Event(24);
    /// Group 1. Start if enabled mid activity (not required at start of activity). Stop when duration is reached. stop_disable if disabled.
    pub const CALORIE_DURATION_ALERT: Event = Event(25);
    /// Group 1.. Stop at end of activity.
    pub const ACTIVITY: Event = Event(26);
    /// marker
    pub const FITNESS_EQUIPMENT: Event = Event(27);
    /// Stop at end of each length.
    pub const LENGTH: Event = Event(28);
    /// marker
    pub const USER_MARKER: Event = Event(32);
    /// marker
    pub const SPORT_POINT: Event = Event(33);
    /// start/stop/marker
    pub const CALIBRATION: Event = Event(36);
    /// marker
    pub const FRONT_GEAR_CHANGE: Event = Event(42);
    /// marker
    pub const REAR_GEAR_CHANGE: Event = Event(43);
    /// marker
    pub const RIDER_POSITION_CHANGE: Event = Event(44);
    /// Group 0. Start / stop when in alert condition.
    pub const ELEV_HIGH_ALERT: Event = Event(45);
    /// Group 0. Start / stop when in alert condition.
    pub const ELEV_LOW_ALERT: Event = Event(46);
    /// marker
    pub const COMM_TIMEOUT: Event = Event(47);
    /// marker
    pub const AUTO_ACTIVITY_DETECT: Event = Event(54);
    /// marker
    pub const DIVE_ALERT: Event = Event(56);
    /// marker
    pub const DIVE_GAS_SWITCHED: Event = Event(57);
    /// marker
    pub const TANK_PRESSURE_RESERVE: Event = Event(71);
    /// marker
    pub const TANK_PRESSURE_CRITICAL: Event = Event(72);
    /// marker
    pub const TANK_LOST: Event = Event(73);
    /// start/stop/marker
    pub const RADAR_THREAT_ALERT: Event = Event(75);
    /// marker
    pub const TANK_BATTERY_LOW: Event = Event(76);
    /// marker - tank pod has connected
    pub const TANK_POD_CONNECTED: Event = Event(81);
    /// marker - tank pod has lost connection
    pub const TANK_POD_DISCONNECTED: Event = Event(82);

    fn as_str(self) -> Option<&'static str> {
        match self.0 {
            0 => Some("timer"),
            3 => Some("workout"),
            4 => Some("workout_step"),
            5 => Some("power_down"),
            6 => Some("power_up"),
            7 => Some("off_course"),
            8 => Some("session"),
            9 => Some("lap"),
            10 => Some("course_point"),
            11 => Some("battery"),
            12 => Some("virtual_partner_pace"),
            13 => Some("hr_high_alert"),
            14 => Some("hr_low_alert"),
            15 => Some("speed_high_alert"),
            16 => Some("speed_low_alert"),
            17 => Some("cad_high_alert"),
            18 => Some("cad_low_alert"),
            19 => Some("power_high_alert"),
            20 => Some("power_low_alert"),
            21 => Some("recovery_hr"),
            22 => Some("battery_low"),
            23 => Some("time_duration_alert"),
            24 => Some("distance_duration_alert"),
            25 => Some("calorie_duration_alert"),
            26 => Some("activity"),
            27 => Some("fitness_equipment"),
            28 => Some("length"),
            32 => Some("user_marker"),
            33 => Some("sport_point"),
            36 => Some("calibration"),
            42 => Some("front_gear_change"),
            43 => Some("rear_gear_change"),
            44 => Some("rider_position_change"),
            45 => Some("elev_high_alert"),
            46 => Some("elev_low_alert"),
            47 => Some("comm_timeout"),
            54 => Some("auto_activity_detect"),
            56 => Some("dive_alert"),
            57 => Some("dive_gas_switched"),
            71 => Some("tank_pressure_reserve"),
            72 => Some("tank_pressure_critical"),
            73 => Some("tank_lost"),
            75 => Some("radar_threat_alert"),
            76 => Some("tank_battery_low"),
            81 => Some("tank_pod_connected"),
            82 => Some("tank_pod_disconnected"),
            _ => None,
        }
    }
}

impl Default for Event {
    fn default() -> Self {
        Self(u8::MAX)
    }
}

impl fmt::Display for Event {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.as_str() {
            Some(s) => write!(f, "{}", s),
            None => write!(f, "Event({})", self.0),
        }
    }
}

#[cfg(feature = "serde")]
impl Serialize for Event {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("Event", 2)?;
        if let Some(s) = self.as_str() {
            state.serialize_field("t", s)?;
        }
        state.serialize_field("c", &self.0)?;
        state.end()
    }
}

#[cfg(feature = "serde")]
#[cfg_attr(feature = "serde", derive(Deserialize))]
struct De<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    t: Option<&'a str>,
    c: u8,
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Event {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let repr = De::deserialize(deserializer)?;
        let v = Self(repr.c);
        if let Some(t) = repr.t
            && let Some(s) = v.as_str()
            && t != s
        {
            return Err(de::Error::custom("tag and content mismatch"));
        }
        Ok(v)
    }
}