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

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

impl Schedule {
    pub const WORKOUT: Schedule = Schedule(0);
    pub const COURSE: Schedule = Schedule(1);

    fn as_str(self) -> Option<&'static str> {
        match self.0 {
            0 => Some("workout"),
            1 => Some("course"),
            _ => None,
        }
    }
}

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

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

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