#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
#[derive(Debug, Clone)]
pub struct DiveGas {
pub message_index: typedef::MessageIndex,
pub helium_content: u8,
pub oxygen_content: u8,
pub status: typedef::DiveGasStatus,
pub mode: typedef::DiveGasMode,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl DiveGas {
pub const MESSAGE_INDEX: u8 = 254;
pub const HELIUM_CONTENT: u8 = 0;
pub const OXYGEN_CONTENT: u8 = 1;
pub const STATUS: u8 = 2;
pub const MODE: u8 = 3;
pub const fn new() -> Self {
Self {
message_index: typedef::MessageIndex(u16::MAX),
helium_content: u8::MAX,
oxygen_content: u8::MAX,
status: typedef::DiveGasStatus(u8::MAX),
mode: typedef::DiveGasMode(u8::MAX),
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
}
impl Default for DiveGas {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for DiveGas {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 255] = [const { &Value::Invalid }; 255];
const KNOWN_NUMS: [u64; 4] = [15, 0, 0, 4611686018427387904];
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 {
message_index: typedef::MessageIndex(vals[254].as_u16()),
helium_content: vals[0].as_u8(),
oxygen_content: vals[1].as_u8(),
status: typedef::DiveGasStatus(vals[2].as_u8()),
mode: typedef::DiveGasMode(vals[3].as_u8()),
unknown_fields,
developer_fields: mesg.developer_fields.clone(),
}
}
}
impl From<DiveGas> for Message {
fn from(m: DiveGas) -> Self {
let mut arr = [const {
Field {
num: 0,
profile_type: ProfileType(0),
value: Value::Invalid,
is_expanded: false,
}
}; 5];
let mut len = 0usize;
if m.message_index != typedef::MessageIndex(u16::MAX) {
arr[len] = Field {
num: 254,
profile_type: ProfileType::MESSAGE_INDEX,
value: Value::Uint16(m.message_index.0),
is_expanded: false,
};
len += 1;
}
if m.helium_content != u8::MAX {
arr[len] = Field {
num: 0,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.helium_content),
is_expanded: false,
};
len += 1;
}
if m.oxygen_content != u8::MAX {
arr[len] = Field {
num: 1,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.oxygen_content),
is_expanded: false,
};
len += 1;
}
if m.status != typedef::DiveGasStatus(u8::MAX) {
arr[len] = Field {
num: 2,
profile_type: ProfileType::DIVE_GAS_STATUS,
value: Value::Uint8(m.status.0),
is_expanded: false,
};
len += 1;
}
if m.mode != typedef::DiveGasMode(u8::MAX) {
arr[len] = Field {
num: 3,
profile_type: ProfileType::DIVE_GAS_MODE,
value: Value::Uint8(m.mode.0),
is_expanded: false,
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::DIVE_GAS,
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,
}
}
}