#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
#[derive(Debug, Clone)]
pub struct ZonesTarget {
pub max_heart_rate: u8,
pub threshold_heart_rate: u8,
pub functional_threshold_power: u16,
pub hr_calc_type: typedef::HrZoneCalc,
pub pwr_calc_type: typedef::PwrZoneCalc,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl ZonesTarget {
pub const MAX_HEART_RATE: u8 = 1;
pub const THRESHOLD_HEART_RATE: u8 = 2;
pub const FUNCTIONAL_THRESHOLD_POWER: u8 = 3;
pub const HR_CALC_TYPE: u8 = 5;
pub const PWR_CALC_TYPE: u8 = 7;
pub const fn new() -> Self {
Self {
max_heart_rate: u8::MAX,
threshold_heart_rate: u8::MAX,
functional_threshold_power: u16::MAX,
hr_calc_type: typedef::HrZoneCalc(u8::MAX),
pwr_calc_type: typedef::PwrZoneCalc(u8::MAX),
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
}
impl Default for ZonesTarget {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for ZonesTarget {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 8] = [const { &Value::Invalid }; 8];
const KNOWN_NUMS: [u64; 4] = [174, 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 {
max_heart_rate: vals[1].as_u8(),
threshold_heart_rate: vals[2].as_u8(),
functional_threshold_power: vals[3].as_u16(),
hr_calc_type: typedef::HrZoneCalc(vals[5].as_u8()),
pwr_calc_type: typedef::PwrZoneCalc(vals[7].as_u8()),
unknown_fields,
developer_fields: mesg.developer_fields.clone(),
}
}
}
impl From<ZonesTarget> for Message {
fn from(m: ZonesTarget) -> 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.max_heart_rate != u8::MAX {
arr[len] = Field {
num: 1,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.max_heart_rate),
is_expanded: false,
};
len += 1;
}
if m.threshold_heart_rate != u8::MAX {
arr[len] = Field {
num: 2,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.threshold_heart_rate),
is_expanded: false,
};
len += 1;
}
if m.functional_threshold_power != u16::MAX {
arr[len] = Field {
num: 3,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.functional_threshold_power),
is_expanded: false,
};
len += 1;
}
if m.hr_calc_type != typedef::HrZoneCalc(u8::MAX) {
arr[len] = Field {
num: 5,
profile_type: ProfileType::HR_ZONE_CALC,
value: Value::Uint8(m.hr_calc_type.0),
is_expanded: false,
};
len += 1;
}
if m.pwr_calc_type != typedef::PwrZoneCalc(u8::MAX) {
arr[len] = Field {
num: 7,
profile_type: ProfileType::PWR_ZONE_CALC,
value: Value::Uint8(m.pwr_calc_type.0),
is_expanded: false,
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::ZONES_TARGET,
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,
}
}
}