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

/// Video message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct Video {
    pub url: String,
    pub hosting_provider: String,
    /// Units: ms; Playback time of video
    pub duration: u32,
    /// 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 Video {
    /// Value's type: `String`; FitBaseType::STRING; ProfileType::String
    pub const URL: u8 = 0;
    /// Value's type: `String`; FitBaseType::STRING; ProfileType::String
    pub const HOSTING_PROVIDER: u8 = 1;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::Uint32; Units: `ms`
    pub const DURATION: u8 = 2;

    /// Create new Video with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            url: String::new(),
            hosting_provider: String::new(),
            duration: u32::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    fn count_valid_fields(&self) -> usize {
        (!self.url.is_empty()) as usize
            + (!self.hosting_provider.is_empty()) as usize
            + (self.duration != u32::MAX) as usize
    }
}

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

impl From<&Message> for Video {
    /// from creates new Video struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [7, 0, 0, 0];
        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 {
                0 => v.url = field.value.as_str().to_owned(),
                1 => v.hosting_provider = field.value.as_str().to_owned(),
                2 => v.duration = field.value.as_u32(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

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

        if !m.url.is_empty() {
            fields.push(Field {
                num: 0,
                base_type: FitBaseType::STRING,
                value: Value::String(m.url),
                is_expanded: false,
            });
        };
        if !m.hosting_provider.is_empty() {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::STRING,
                value: Value::String(m.hosting_provider),
                is_expanded: false,
            });
        };
        if m.duration != u32::MAX {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.duration),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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

#[cfg(feature = "serde")]
impl Serialize for Video {
    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("Video", n)?;
        if !self.url.is_empty() {
            state.serialize_field("url", &self.url)?;
        }
        if !self.hosting_provider.is_empty() {
            state.serialize_field("hosting_provider", &self.hosting_provider)?;
        }
        if self.duration != u32::MAX {
            state.serialize_field("duration", &self.duration)?;
        }
        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 {
    url: String,
    hosting_provider: String,
    duration: u32,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for Video {
    fn from(m: De) -> Self {
        Self {
            url: m.url,
            hosting_provider: m.hosting_provider,
            duration: m.duration,
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            url: String::new(),
            hosting_provider: String::new(),
            duration: u32::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}