#![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)]
pub struct ExdDataFieldConfiguration {
pub screen_index: u8,
pub concept_field: u8,
pub field_id: u8,
pub concept_count: u8,
pub display_type: typedef::ExdDisplayType,
pub title: [String; 32],
state: [u8; 1], pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl ExdDataFieldConfiguration {
pub const SCREEN_INDEX: u8 = 0;
pub const CONCEPT_FIELD: u8 = 1;
pub const FIELD_ID: u8 = 2;
pub const CONCEPT_COUNT: u8 = 3;
pub const DISPLAY_TYPE: u8 = 4;
pub const TITLE: u8 = 5;
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(),
}
}
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,
}
}
pub fn is_expanded(&self, num: u8) -> bool {
is_expanded(&self.state, num)
}
}
impl Default for ExdDataFieldConfiguration {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for ExdDataFieldConfiguration {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 6] = [const { &Value::Invalid }; 6];
let mut state = [0u8; 1];
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 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_count: vals[3].as_u8(),
display_type: typedef::ExdDisplayType(vals[4].as_u8()),
title: match &vals[5] {
Value::VecString(v) => {
let mut arr: [String; 32] = Default::default();
for (i, x) in v.iter().enumerate() {
arr[i] = x.to_owned();
}
arr
}
_ => Default::default(),
},
state,
unknown_fields,
developer_fields: mesg.developer_fields.clone(),
}
}
}
impl From<ExdDataFieldConfiguration> for Message {
fn from(m: ExdDataFieldConfiguration) -> Self {
let mut arr = [const {
Field {
num: 0,
profile_type: ProfileType(0),
value: Value::Invalid,
is_expanded: false,
}
}; 6];
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_count != u8::MAX {
arr[len] = Field {
num: 3,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.concept_count),
is_expanded: is_expanded(&state, 3),
};
len += 1;
}
if m.display_type != typedef::ExdDisplayType(u8::MAX) {
arr[len] = Field {
num: 4,
profile_type: ProfileType::EXD_DISPLAY_TYPE,
value: Value::Uint8(m.display_type.0),
is_expanded: false,
};
len += 1;
}
if m.title != [const { String::new() }; 32] {
arr[len] = Field {
num: 5,
profile_type: ProfileType::STRING,
value: Value::VecString(Vec::from(&m.title)),
is_expanded: false,
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::EXD_DATA_FIELD_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,
}
}
}