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::*;

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

#[derive(Debug, Clone)]
/// ExdDataConceptConfiguration is a ExdDataConceptConfiguration message.
pub struct ExdDataConceptConfiguration {
    pub screen_index: u8,
    pub concept_field: u8,
    pub field_id: u8,
    pub concept_index: u8,
    pub data_page: u8,
    pub concept_key: u8,
    pub scaling: u8,
    pub data_units: typedef::ExdDataUnits,
    pub qualifier: typedef::ExdQualifiers,
    pub descriptor: typedef::ExdDescriptors,
    pub is_signed: typedef::Bool,
    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 ExdDataConceptConfiguration {
    /// Value's type: `u8`
    pub const SCREEN_INDEX: u8 = 0;
    /// Value's type: `u8`
    pub const CONCEPT_FIELD: u8 = 1;
    /// Value's type: `u8`
    pub const FIELD_ID: u8 = 2;
    /// Value's type: `u8`
    pub const CONCEPT_INDEX: u8 = 3;
    /// Value's type: `u8`
    pub const DATA_PAGE: u8 = 4;
    /// Value's type: `u8`
    pub const CONCEPT_KEY: u8 = 5;
    /// Value's type: `u8`
    pub const SCALING: u8 = 6;
    /// Value's type: `u8`
    pub const DATA_UNITS: u8 = 8;
    /// Value's type: `u8`
    pub const QUALIFIER: u8 = 9;
    /// Value's type: `u8`
    pub const DESCRIPTOR: u8 = 10;
    /// Value's type: `u8`
    pub const IS_SIGNED: u8 = 11;

    /// Create new ExdDataConceptConfiguration 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_index: u8::MAX,
            data_page: u8::MAX,
            concept_key: u8::MAX,
            scaling: u8::MAX,
            data_units: typedef::ExdDataUnits(u8::MAX),
            qualifier: typedef::ExdQualifiers(u8::MAX),
            descriptor: typedef::ExdDescriptors(u8::MAX),
            is_signed: typedef::Bool(u8::MAX),
            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)
    }
}

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

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

        const KNOWN_NUMS: [u64; 4] = [3967, 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 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;
            }
            if field.is_expanded && field.num < 4 {
                state[field.num as usize >> 3] |= 1 << (field.num & 7)
            }
            vals[field.num as usize] = &field.value;
        }

        Self {
            screen_index: vals[0].as_u8(),
            concept_field: vals[1].as_u8(),
            field_id: vals[2].as_u8(),
            concept_index: vals[3].as_u8(),
            data_page: vals[4].as_u8(),
            concept_key: vals[5].as_u8(),
            scaling: vals[6].as_u8(),
            data_units: typedef::ExdDataUnits(vals[8].as_u8()),
            qualifier: typedef::ExdQualifiers(vals[9].as_u8()),
            descriptor: typedef::ExdDescriptors(vals[10].as_u8()),
            is_signed: typedef::Bool(vals[11].as_u8()),
            state,
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

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

        if m.screen_index != u8::MAX {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.screen_index),
                is_expanded: false,
            };
            len += 1;
        }
        if m.concept_field != u8::MAX {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::BYTE,
                value: Value::Uint8(m.concept_field),
                is_expanded: false,
            };
            len += 1;
        }
        if m.field_id != u8::MAX {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.field_id),
                is_expanded: is_expanded(&state, 2),
            };
            len += 1;
        }
        if m.concept_index != u8::MAX {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.concept_index),
                is_expanded: is_expanded(&state, 3),
            };
            len += 1;
        }
        if m.data_page != u8::MAX {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.data_page),
                is_expanded: false,
            };
            len += 1;
        }
        if m.concept_key != u8::MAX {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.concept_key),
                is_expanded: false,
            };
            len += 1;
        }
        if m.scaling != u8::MAX {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.scaling),
                is_expanded: false,
            };
            len += 1;
        }
        if m.data_units != typedef::ExdDataUnits(u8::MAX) {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::EXD_DATA_UNITS,
                value: Value::Uint8(m.data_units.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.qualifier != typedef::ExdQualifiers(u8::MAX) {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::EXD_QUALIFIERS,
                value: Value::Uint8(m.qualifier.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.descriptor != typedef::ExdDescriptors(u8::MAX) {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::EXD_DESCRIPTORS,
                value: Value::Uint8(m.descriptor.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.is_signed != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.is_signed.0),
                is_expanded: false,
            };
            len += 1;
        }

        Message {
            header: 0,
            num: typedef::MesgNum::EXD_DATA_CONCEPT_CONFIGURATION,
            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,
        }
    }
}