#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
#[derive(Debug, Clone)]
pub struct FieldDescription {
pub developer_data_index: u8,
pub field_definition_number: u8,
pub fit_base_type_id: typedef::FitBaseType,
pub field_name: Vec<String>,
pub array: u8,
pub components: String,
pub scale: u8,
pub offset: i8,
pub units: Vec<String>,
pub bits: String,
pub accumulate: String,
pub fit_base_unit_id: typedef::FitBaseUnit,
pub native_mesg_num: typedef::MesgNum,
pub native_field_num: u8,
pub unknown_fields: Vec<Field>,
}
impl FieldDescription {
pub const DEVELOPER_DATA_INDEX: u8 = 0;
pub const FIELD_DEFINITION_NUMBER: u8 = 1;
pub const FIT_BASE_TYPE_ID: u8 = 2;
pub const FIELD_NAME: u8 = 3;
pub const ARRAY: u8 = 4;
pub const COMPONENTS: u8 = 5;
pub const SCALE: u8 = 6;
pub const OFFSET: u8 = 7;
pub const UNITS: u8 = 8;
pub const BITS: u8 = 9;
pub const ACCUMULATE: u8 = 10;
pub const FIT_BASE_UNIT_ID: u8 = 13;
pub const NATIVE_MESG_NUM: u8 = 14;
pub const NATIVE_FIELD_NUM: u8 = 15;
pub const fn new() -> Self {
Self {
developer_data_index: u8::MAX,
field_definition_number: u8::MAX,
fit_base_type_id: typedef::FitBaseType(u8::MAX),
field_name: Vec::<String>::new(),
array: u8::MAX,
components: String::new(),
scale: u8::MAX,
offset: i8::MAX,
units: Vec::<String>::new(),
bits: String::new(),
accumulate: String::new(),
fit_base_unit_id: typedef::FitBaseUnit(u16::MAX),
native_mesg_num: typedef::MesgNum(u16::MAX),
native_field_num: u8::MAX,
unknown_fields: Vec::new(),
}
}
}
impl Default for FieldDescription {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for FieldDescription {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 16] = [const { &Value::Invalid }; 16];
const KNOWN_NUMS: [u64; 4] = [59391, 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;
}
vals[field.num as usize] = &field.value;
}
Self {
developer_data_index: vals[0].as_u8(),
field_definition_number: vals[1].as_u8(),
fit_base_type_id: typedef::FitBaseType(vals[2].as_u8()),
field_name: vals[3].as_vec_string(),
array: vals[4].as_u8(),
components: vals[5].as_string(),
scale: vals[6].as_u8(),
offset: vals[7].as_i8(),
units: vals[8].as_vec_string(),
bits: vals[9].as_string(),
accumulate: vals[10].as_string(),
fit_base_unit_id: typedef::FitBaseUnit(vals[13].as_u16()),
native_mesg_num: typedef::MesgNum(vals[14].as_u16()),
native_field_num: vals[15].as_u8(),
unknown_fields,
}
}
}
impl From<FieldDescription> for Message {
fn from(m: FieldDescription) -> Self {
let mut arr = [const {
Field {
num: 0,
profile_type: ProfileType(0),
value: Value::Invalid,
is_expanded: false,
}
}; 14];
let mut len = 0usize;
if m.developer_data_index != u8::MAX {
arr[len] = Field {
num: 0,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.developer_data_index),
is_expanded: false,
};
len += 1;
}
if m.field_definition_number != u8::MAX {
arr[len] = Field {
num: 1,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.field_definition_number),
is_expanded: false,
};
len += 1;
}
if m.fit_base_type_id != typedef::FitBaseType(u8::MAX) {
arr[len] = Field {
num: 2,
profile_type: ProfileType::FIT_BASE_TYPE,
value: Value::Uint8(m.fit_base_type_id.0),
is_expanded: false,
};
len += 1;
}
if m.field_name != Vec::<String>::new() {
arr[len] = Field {
num: 3,
profile_type: ProfileType::STRING,
value: Value::VecString(m.field_name),
is_expanded: false,
};
len += 1;
}
if m.array != u8::MAX {
arr[len] = Field {
num: 4,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.array),
is_expanded: false,
};
len += 1;
}
if m.components != String::new() {
arr[len] = Field {
num: 5,
profile_type: ProfileType::STRING,
value: Value::String(m.components),
is_expanded: false,
};
len += 1;
}
if m.scale != u8::MAX {
arr[len] = Field {
num: 6,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.scale),
is_expanded: false,
};
len += 1;
}
if m.offset != i8::MAX {
arr[len] = Field {
num: 7,
profile_type: ProfileType::SINT8,
value: Value::Int8(m.offset),
is_expanded: false,
};
len += 1;
}
if m.units != Vec::<String>::new() {
arr[len] = Field {
num: 8,
profile_type: ProfileType::STRING,
value: Value::VecString(m.units),
is_expanded: false,
};
len += 1;
}
if m.bits != String::new() {
arr[len] = Field {
num: 9,
profile_type: ProfileType::STRING,
value: Value::String(m.bits),
is_expanded: false,
};
len += 1;
}
if m.accumulate != String::new() {
arr[len] = Field {
num: 10,
profile_type: ProfileType::STRING,
value: Value::String(m.accumulate),
is_expanded: false,
};
len += 1;
}
if m.fit_base_unit_id != typedef::FitBaseUnit(u16::MAX) {
arr[len] = Field {
num: 13,
profile_type: ProfileType::FIT_BASE_UNIT,
value: Value::Uint16(m.fit_base_unit_id.0),
is_expanded: false,
};
len += 1;
}
if m.native_mesg_num != typedef::MesgNum(u16::MAX) {
arr[len] = Field {
num: 14,
profile_type: ProfileType::MESG_NUM,
value: Value::Uint16(m.native_mesg_num.0),
is_expanded: false,
};
len += 1;
}
if m.native_field_num != u8::MAX {
arr[len] = Field {
num: 15,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.native_field_num),
is_expanded: false,
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::FIELD_DESCRIPTION,
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: Vec::new(),
}
}
}