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::borrow::ToOwned;
use alloc::string::String;
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};

/// Weather Alert message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct WeatherAlert {
    pub timestamp: typedef::DateTime,
    /// Unique identifier from GCS report ID string, length is 12
    pub report_id: String,
    /// Time alert was issued
    pub issue_time: typedef::DateTime,
    /// Time alert expires
    pub expire_time: typedef::DateTime,
    /// Warning, Watch, Advisory, Statement
    pub severity: typedef::WeatherSeverity,
    /// Tornado, Severe Thunderstorm, etc.
    pub r#type: typedef::WeatherSevereType,
    /// 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 WeatherAlert {
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::DateTime
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `String`; FitBaseType::STRING; ProfileType::String
    pub const REPORT_ID: u8 = 0;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::DateTime
    pub const ISSUE_TIME: u8 = 1;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::DateTime
    pub const EXPIRE_TIME: u8 = 2;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::WeatherSeverity
    pub const SEVERITY: u8 = 3;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::WeatherSevereType
    pub const TYPE: u8 = 4;

    /// Create new WeatherAlert with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            timestamp: typedef::DateTime(u32::MAX),
            report_id: String::new(),
            issue_time: typedef::DateTime(u32::MAX),
            expire_time: typedef::DateTime(u32::MAX),
            severity: typedef::WeatherSeverity(u8::MAX),
            r#type: typedef::WeatherSevereType(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (self.timestamp.0 != u32::MAX) as usize
            + (!self.report_id.is_empty()) as usize
            + (self.issue_time.0 != u32::MAX) as usize
            + (self.expire_time.0 != u32::MAX) as usize
            + (self.severity.0 != u8::MAX) as usize
            + (self.r#type.0 != u8::MAX) as usize
    }
}

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

impl From<&Message> for WeatherAlert {
    /// from creates new WeatherAlert struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [31, 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.report_id = field.value.as_str().to_owned(),
                1 => v.issue_time = typedef::DateTime(field.value.as_u32()),
                2 => v.expire_time = typedef::DateTime(field.value.as_u32()),
                3 => v.severity = typedef::WeatherSeverity(field.value.as_u8()),
                4 => v.r#type = typedef::WeatherSevereType(field.value.as_u8()),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

impl From<WeatherAlert> for Message {
    fn from(m: WeatherAlert) -> 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.report_id.is_empty() {
            fields.push(Field {
                num: 0,
                base_type: FitBaseType::STRING,
                value: Value::String(m.report_id),
                is_expanded: false,
            });
        };
        if m.issue_time.0 != u32::MAX {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.issue_time.0),
                is_expanded: false,
            });
        };
        if m.expire_time.0 != u32::MAX {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.expire_time.0),
                is_expanded: false,
            });
        };
        if m.severity.0 != u8::MAX {
            fields.push(Field {
                num: 3,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.severity.0),
                is_expanded: false,
            });
        };
        if m.r#type.0 != u8::MAX {
            fields.push(Field {
                num: 4,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.r#type.0),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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

#[cfg(feature = "serde")]
impl Serialize for WeatherAlert {
    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("WeatherAlert", n)?;
        if let Some(v) = self.timestamp.unix_timestamp() {
            state.serialize_field("timestamp", &v)?;
        }
        if !self.report_id.is_empty() {
            state.serialize_field("report_id", &self.report_id)?;
        }
        if let Some(v) = self.issue_time.unix_timestamp() {
            state.serialize_field("issue_time", &v)?;
        }
        if let Some(v) = self.expire_time.unix_timestamp() {
            state.serialize_field("expire_time", &v)?;
        }
        if self.severity.0 != u8::MAX {
            state.serialize_field("severity", &self.severity)?;
        }
        if self.r#type.0 != u8::MAX {
            state.serialize_field("type", &self.r#type)?;
        }
        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>,
    report_id: String,
    issue_time: Option<i64>,
    expire_time: Option<i64>,
    severity: typedef::WeatherSeverity,
    r#type: typedef::WeatherSevereType,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for WeatherAlert {
    fn from(m: De) -> Self {
        Self {
            timestamp: m.timestamp.map_or_else(
                || typedef::DateTime(u32::MAX),
                typedef::DateTime::from_unix_timestamp,
            ),
            report_id: m.report_id,
            issue_time: m.issue_time.map_or_else(
                || typedef::DateTime(u32::MAX),
                typedef::DateTime::from_unix_timestamp,
            ),
            expire_time: m.expire_time.map_or_else(
                || typedef::DateTime(u32::MAX),
                typedef::DateTime::from_unix_timestamp,
            ),
            severity: m.severity,
            r#type: m.r#type,
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            timestamp: None,
            report_id: String::new(),
            issue_time: None,
            expire_time: None,
            severity: typedef::WeatherSeverity(u8::MAX),
            r#type: typedef::WeatherSevereType(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}