#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
#[derive(Debug, Clone)]
pub struct Workout {
pub message_index: typedef::MessageIndex,
pub sport: typedef::Sport,
pub capabilities: typedef::WorkoutCapabilities,
pub num_valid_steps: u16,
pub wkt_name: String,
pub sub_sport: typedef::SubSport,
pub pool_length: u16,
pub pool_length_unit: typedef::DisplayMeasure,
pub wkt_description: String,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl Workout {
pub const MESSAGE_INDEX: u8 = 254;
pub const SPORT: u8 = 4;
pub const CAPABILITIES: u8 = 5;
pub const NUM_VALID_STEPS: u8 = 6;
pub const WKT_NAME: u8 = 8;
pub const SUB_SPORT: u8 = 11;
pub const POOL_LENGTH: u8 = 14;
pub const POOL_LENGTH_UNIT: u8 = 15;
pub const WKT_DESCRIPTION: u8 = 17;
pub const fn new() -> Self {
Self {
message_index: typedef::MessageIndex(u16::MAX),
sport: typedef::Sport(u8::MAX),
capabilities: typedef::WorkoutCapabilities(u32::MIN),
num_valid_steps: u16::MAX,
wkt_name: String::new(),
sub_sport: typedef::SubSport(u8::MAX),
pool_length: u16::MAX,
pool_length_unit: typedef::DisplayMeasure(u8::MAX),
wkt_description: String::new(),
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn pool_length_scaled(&self) -> f64 {
if self.pool_length == u16::MAX {
return f64::from_bits(u64::MAX);
}
self.pool_length as f64 / 100.0 - 0.0
}
pub fn set_pool_length_scaled(&mut self, v: f64) -> &mut Workout {
let unscaled = (v + 0.0) * 100.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
self.pool_length = u16::MAX;
return self;
}
self.pool_length = unscaled as u16;
self
}
}
impl Default for Workout {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for Workout {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 255] = [const { &Value::Invalid }; 255];
const KNOWN_NUMS: [u64; 4] = [182640, 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()),
sport: typedef::Sport(vals[4].as_u8()),
capabilities: typedef::WorkoutCapabilities(vals[5].as_u32z()),
num_valid_steps: vals[6].as_u16(),
wkt_name: vals[8].as_string(),
sub_sport: typedef::SubSport(vals[11].as_u8()),
pool_length: vals[14].as_u16(),
pool_length_unit: typedef::DisplayMeasure(vals[15].as_u8()),
wkt_description: vals[17].as_string(),
unknown_fields,
developer_fields: mesg.developer_fields.clone(),
}
}
}
impl From<Workout> for Message {
fn from(m: Workout) -> Self {
let mut arr = [const {
Field {
num: 0,
profile_type: ProfileType(0),
value: Value::Invalid,
is_expanded: false,
}
}; 9];
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.sport != typedef::Sport(u8::MAX) {
arr[len] = Field {
num: 4,
profile_type: ProfileType::SPORT,
value: Value::Uint8(m.sport.0),
is_expanded: false,
};
len += 1;
}
if m.capabilities != typedef::WorkoutCapabilities(u32::MIN) {
arr[len] = Field {
num: 5,
profile_type: ProfileType::WORKOUT_CAPABILITIES,
value: Value::Uint32(m.capabilities.0),
is_expanded: false,
};
len += 1;
}
if m.num_valid_steps != u16::MAX {
arr[len] = Field {
num: 6,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.num_valid_steps),
is_expanded: false,
};
len += 1;
}
if m.wkt_name != String::new() {
arr[len] = Field {
num: 8,
profile_type: ProfileType::STRING,
value: Value::String(m.wkt_name),
is_expanded: false,
};
len += 1;
}
if m.sub_sport != typedef::SubSport(u8::MAX) {
arr[len] = Field {
num: 11,
profile_type: ProfileType::SUB_SPORT,
value: Value::Uint8(m.sub_sport.0),
is_expanded: false,
};
len += 1;
}
if m.pool_length != u16::MAX {
arr[len] = Field {
num: 14,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.pool_length),
is_expanded: false,
};
len += 1;
}
if m.pool_length_unit != typedef::DisplayMeasure(u8::MAX) {
arr[len] = Field {
num: 15,
profile_type: ProfileType::DISPLAY_MEASURE,
value: Value::Uint8(m.pool_length_unit.0),
is_expanded: false,
};
len += 1;
}
if m.wkt_description != String::new() {
arr[len] = Field {
num: 17,
profile_type: ProfileType::STRING,
value: Value::String(m.wkt_description),
is_expanded: false,
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::WORKOUT,
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,
}
}
}