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};

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

impl Goal {
    pub const TIME: Goal = Goal(0);
    pub const DISTANCE: Goal = Goal(1);
    pub const CALORIES: Goal = Goal(2);
    pub const FREQUENCY: Goal = Goal(3);
    pub const STEPS: Goal = Goal(4);
    pub const ASCENT: Goal = Goal(5);
    pub const ACTIVE_MINUTES: Goal = Goal(6);

    fn as_str(self) -> Option<&'static str> {
        match self.0 {
            0 => Some("time"),
            1 => Some("distance"),
            2 => Some("calories"),
            3 => Some("frequency"),
            4 => Some("steps"),
            5 => Some("ascent"),
            6 => Some("active_minutes"),
            _ => None,
        }
    }
}

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

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

#[cfg(feature = "serde")]
impl Serialize for Goal {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("Goal", 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 Goal {
    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)
    }
}