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.

#![allow(clippy::manual_range_patterns)]

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

fn is_expanded(state: &[u8], num: u8) -> bool {
    match num {
        2 | 3 => (state[num as usize >> 3] >> (num & 7)) & 1 == 1,
        _ => false,
    }
}

/// Exd Data Field Configuration message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct ExdDataFieldConfiguration {
    pub screen_index: u8,
    pub concept_field: u8,
    pub field_id: u8,
    pub concept_count: u8,
    pub display_type: typedef::ExdDisplayType,
    /// Array: \[32\]
    pub title: [String; 32],
    state: [u8; 1], // Used for tracking expanded fields.
    /// 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 ExdDataFieldConfiguration {
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::Uint8
    pub const SCREEN_INDEX: u8 = 0;
    /// Value's type: `u8`; FitBaseType::BYTE; ProfileType::Byte
    pub const CONCEPT_FIELD: u8 = 1;
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::Uint8
    pub const FIELD_ID: u8 = 2;
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::Uint8
    pub const CONCEPT_COUNT: u8 = 3;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::ExdDisplayType
    pub const DISPLAY_TYPE: u8 = 4;
    /// Value's type: `[String; 32]`; FitBaseType::STRING; ProfileType::String
    pub const TITLE: u8 = 5;

    /// Create new ExdDataFieldConfiguration with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            screen_index: u8::MAX,
            concept_field: u8::MAX,
            field_id: u8::MAX,
            concept_count: u8::MAX,
            display_type: typedef::ExdDisplayType(u8::MAX),
            title: [const { String::new() }; 32],
            state: [0u8; 1],
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Marks whether given field's num is an expanded field (field that being generated through a component expansion).
    pub fn mark_as_expanded(&mut self, num: u8, flag: bool) -> bool {
        match num {
            2 | 3 => {
                if flag {
                    self.state[num as usize >> 3] |= 1 << (num & 7)
                } else {
                    self.state[num as usize >> 3] &= !(1 << (num & 7))
                }
                true
            }
            _ => false,
        }
    }

    /// checks whether given field's num is a field generated through a component expansion.
    pub fn is_expanded(&self, num: u8) -> bool {
        is_expanded(&self.state, num)
    }

    fn count_valid_fields(&self) -> usize {
        (self.screen_index != u8::MAX) as usize
            + (self.concept_field != u8::MAX) as usize
            + (self.field_id != u8::MAX) as usize
            + (self.concept_count != u8::MAX) as usize
            + (self.display_type.0 != u8::MAX) as usize
            + (self.title != [const { String::new() }; 32]) as usize
    }
}

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

impl From<&Message> for ExdDataFieldConfiguration {
    /// from creates new ExdDataFieldConfiguration struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        const KNOWN_NUMS: [u64; 4] = [63, 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.screen_index = field.value.as_u8(),
                1 => v.concept_field = field.value.as_u8(),
                2 => v.field_id = field.value.as_u8(),
                3 => v.concept_count = field.value.as_u8(),
                4 => v.display_type = typedef::ExdDisplayType(field.value.as_u8()),
                5 => {
                    v.title = match &field.value {
                        Value::VecString(v) => {
                            let mut arr = [const { String::new() }; 32];
                            for (i, x) in v.iter().take(32).enumerate() {
                                arr[i] = x.to_owned();
                            }
                            arr
                        }
                        _ => [const { String::new() }; 32],
                    }
                }
                _ => {
                    v.unknown_fields.push(field.clone());
                    continue;
                }
            };
            if field.is_expanded && field.num < 4 {
                v.state[field.num as usize >> 3] |= 1 << (field.num & 7)
            }
        }

        v
    }
}

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

        if m.screen_index != u8::MAX {
            fields.push(Field {
                num: 0,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.screen_index),
                is_expanded: false,
            });
        };
        if m.concept_field != u8::MAX {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::BYTE,
                value: Value::Uint8(m.concept_field),
                is_expanded: false,
            });
        };
        if m.field_id != u8::MAX {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.field_id),
                is_expanded: is_expanded(&m.state, 2),
            });
        };
        if m.concept_count != u8::MAX {
            fields.push(Field {
                num: 3,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.concept_count),
                is_expanded: is_expanded(&m.state, 3),
            });
        };
        if m.display_type.0 != u8::MAX {
            fields.push(Field {
                num: 4,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.display_type.0),
                is_expanded: false,
            });
        };
        if m.title != [const { String::new() }; 32] {
            fields.push(Field {
                num: 5,
                base_type: FitBaseType::STRING,
                value: Value::VecString(Vec::from(&m.title)),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

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

#[cfg(feature = "serde")]
impl Serialize for ExdDataFieldConfiguration {
    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("ExdDataFieldConfiguration", n)?;
        if self.screen_index != u8::MAX {
            state.serialize_field("screen_index", &self.screen_index)?;
        }
        if self.concept_field != u8::MAX {
            state.serialize_field("concept_field", &self.concept_field)?;
        }
        if self.field_id != u8::MAX {
            state.serialize_field("field_id", &self.field_id)?;
        }
        if self.concept_count != u8::MAX {
            state.serialize_field("concept_count", &self.concept_count)?;
        }
        if self.display_type.0 != u8::MAX {
            state.serialize_field("display_type", &self.display_type)?;
        }
        if self.title != [const { String::new() }; 32] {
            state.serialize_field("title", &self.title)?;
        }
        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 {
    screen_index: u8,
    concept_field: u8,
    field_id: u8,
    concept_count: u8,
    display_type: typedef::ExdDisplayType,
    title: [String; 32],
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for ExdDataFieldConfiguration {
    fn from(m: De) -> Self {
        Self {
            screen_index: m.screen_index,
            concept_field: m.concept_field,
            field_id: m.field_id,
            concept_count: m.concept_count,
            display_type: m.display_type,
            title: m.title,
            state: [0u8; 1],
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            screen_index: u8::MAX,
            concept_field: u8::MAX,
            field_id: u8::MAX,
            concept_count: u8::MAX,
            display_type: typedef::ExdDisplayType(u8::MAX),
            title: [const { String::new() }; 32],
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}