rustyfit 0.5.0

This project hosts the Rust implementation for The Flexible and Interoperable Data Transfer (FIT) Protocol
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(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]

use crate::profile::{ProfileType, typedef};
use crate::proto::*;

#[derive(Debug, Clone)]
/// MemoGlob is a MemoGlob message.
pub struct MemoGlob {
    /// Sequence number of memo blocks
    pub part_index: u32,
    /// Deprecated. Use data field.
    pub memo: Vec<u8>,
    /// Message Number of the parent message
    pub mesg_num: typedef::MesgNum,
    /// Index of mesg that this glob is associated with.
    pub parent_index: typedef::MessageIndex,
    /// Field within the parent that this glob is associated with
    pub field_num: u8,
    /// Base: UINT8Z; Block of utf8 bytes. Note, mutltibyte characters may be split across adjoining memo_glob messages.
    pub data: Vec<u8>,
    /// 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 MemoGlob {
    /// Value's type: `u32`
    pub const PART_INDEX: u8 = 250;
    /// Value's type: `Vec<u8>`
    pub const MEMO: u8 = 0;
    /// Value's type: `u16`
    pub const MESG_NUM: u8 = 1;
    /// Value's type: `u16`
    pub const PARENT_INDEX: u8 = 2;
    /// Value's type: `u8`
    pub const FIELD_NUM: u8 = 3;
    /// Value's type: `Vec<u8>`; Base: UINT8Z
    pub const DATA: u8 = 4;

    /// Create new MemoGlob with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            part_index: u32::MAX,
            memo: Vec::<u8>::new(),
            mesg_num: typedef::MesgNum(u16::MAX),
            parent_index: typedef::MessageIndex(u16::MAX),
            field_num: u8::MAX,
            data: Vec::<u8>::new(),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}

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

impl From<&Message> for MemoGlob {
    /// from creates new MemoGlob struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        let mut vals: [&Value; 251] = [const { &Value::Invalid }; 251];

        const KNOWN_NUMS: [u64; 4] = [31, 0, 0, 288230376151711744];
        let mut n = 0u64;
        for field in &mesg.fields {
            n += (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 ^ 1
        }
        let mut unknown_fields: Vec<Field> = Vec::with_capacity(n as usize);

        for field in &mesg.fields {
            if (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 == 0 {
                unknown_fields.push(field.clone());
                continue;
            }
            vals[field.num as usize] = &field.value;
        }

        Self {
            part_index: vals[250].as_u32(),
            memo: vals[0].as_vec_u8(),
            mesg_num: typedef::MesgNum(vals[1].as_u16()),
            parent_index: typedef::MessageIndex(vals[2].as_u16()),
            field_num: vals[3].as_u8(),
            data: vals[4].as_vec_u8(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<MemoGlob> for Message {
    fn from(m: MemoGlob) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 6];
        let mut len = 0usize;

        if m.part_index != u32::MAX {
            arr[len] = Field {
                num: 250,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.part_index),
                is_expanded: false,
            };
            len += 1;
        }
        if m.memo != Vec::<u8>::new() {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::BYTE,
                value: Value::VecUint8(m.memo),
                is_expanded: false,
            };
            len += 1;
        }
        if m.mesg_num != typedef::MesgNum(u16::MAX) {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::MESG_NUM,
                value: Value::Uint16(m.mesg_num.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.parent_index != typedef::MessageIndex(u16::MAX) {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.parent_index.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.field_num != u8::MAX {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.field_num),
                is_expanded: false,
            };
            len += 1;
        }
        if m.data != Vec::<u8>::new() {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT8Z,
                value: Value::VecUint8(m.data),
                is_expanded: false,
            };
            len += 1;
        }

        Message {
            header: 0,
            num: typedef::MesgNum::MEMO_GLOB,
            fields: {
                let mut fields: Vec<Field> = Vec::with_capacity(len + m.unknown_fields.len());
                fields.extend_from_slice(&arr[..len]);
                fields.extend_from_slice(&m.unknown_fields);
                fields
            },
            developer_fields: m.developer_fields,
        }
    }
}