#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
#[derive(Debug, Clone)]
pub struct Set {
pub timestamp: typedef::DateTime,
pub duration: u32,
pub repetitions: u16,
pub weight: u16,
pub set_type: typedef::SetType,
pub start_time: typedef::DateTime,
pub category: Vec<typedef::ExerciseCategory>,
pub category_subtype: Vec<u16>,
pub weight_display_unit: typedef::FitBaseUnit,
pub message_index: typedef::MessageIndex,
pub wkt_step_index: typedef::MessageIndex,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl Set {
pub const TIMESTAMP: u8 = 254;
pub const DURATION: u8 = 0;
pub const REPETITIONS: u8 = 3;
pub const WEIGHT: u8 = 4;
pub const SET_TYPE: u8 = 5;
pub const START_TIME: u8 = 6;
pub const CATEGORY: u8 = 7;
pub const CATEGORY_SUBTYPE: u8 = 8;
pub const WEIGHT_DISPLAY_UNIT: u8 = 9;
pub const MESSAGE_INDEX: u8 = 10;
pub const WKT_STEP_INDEX: u8 = 11;
pub const fn new() -> Self {
Self {
timestamp: typedef::DateTime(u32::MAX),
duration: u32::MAX,
repetitions: u16::MAX,
weight: u16::MAX,
set_type: typedef::SetType(u8::MAX),
start_time: typedef::DateTime(u32::MAX),
category: Vec::<typedef::ExerciseCategory>::new(),
category_subtype: Vec::<u16>::new(),
weight_display_unit: typedef::FitBaseUnit(u16::MAX),
message_index: typedef::MessageIndex(u16::MAX),
wkt_step_index: typedef::MessageIndex(u16::MAX),
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn duration_scaled(&self) -> f64 {
if self.duration == u32::MAX {
return f64::from_bits(u64::MAX);
}
self.duration as f64 / 1000.0 - 0.0
}
pub fn set_duration_scaled(&mut self, v: f64) -> &mut Set {
let unscaled = (v + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
self.duration = u32::MAX;
return self;
}
self.duration = unscaled as u32;
self
}
pub fn weight_scaled(&self) -> f64 {
if self.weight == u16::MAX {
return f64::from_bits(u64::MAX);
}
self.weight as f64 / 16.0 - 0.0
}
pub fn set_weight_scaled(&mut self, v: f64) -> &mut Set {
let unscaled = (v + 0.0) * 16.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
self.weight = u16::MAX;
return self;
}
self.weight = unscaled as u16;
self
}
}
impl Default for Set {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for Set {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 255] = [const { &Value::Invalid }; 255];
const KNOWN_NUMS: [u64; 4] = [4089, 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 {
timestamp: typedef::DateTime(vals[254].as_u32()),
duration: vals[0].as_u32(),
repetitions: vals[3].as_u16(),
weight: vals[4].as_u16(),
set_type: typedef::SetType(vals[5].as_u8()),
start_time: typedef::DateTime(vals[6].as_u32()),
category: match &vals[7] {
Value::VecUint16(v) => {
let mut vs = Vec::with_capacity(v.len());
for x in v {
vs.push(typedef::ExerciseCategory(*x))
}
vs
}
_ => Vec::new(),
},
category_subtype: vals[8].as_vec_u16(),
weight_display_unit: typedef::FitBaseUnit(vals[9].as_u16()),
message_index: typedef::MessageIndex(vals[10].as_u16()),
wkt_step_index: typedef::MessageIndex(vals[11].as_u16()),
unknown_fields,
developer_fields: mesg.developer_fields.clone(),
}
}
}
impl From<Set> for Message {
fn from(m: Set) -> Self {
let mut arr = [const {
Field {
num: 0,
profile_type: ProfileType(0),
value: Value::Invalid,
is_expanded: false,
}
}; 11];
let mut len = 0usize;
if m.timestamp != typedef::DateTime(u32::MAX) {
arr[len] = Field {
num: 254,
profile_type: ProfileType::DATE_TIME,
value: Value::Uint32(m.timestamp.0),
is_expanded: false,
};
len += 1;
}
if m.duration != u32::MAX {
arr[len] = Field {
num: 0,
profile_type: ProfileType::UINT32,
value: Value::Uint32(m.duration),
is_expanded: false,
};
len += 1;
}
if m.repetitions != u16::MAX {
arr[len] = Field {
num: 3,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.repetitions),
is_expanded: false,
};
len += 1;
}
if m.weight != u16::MAX {
arr[len] = Field {
num: 4,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.weight),
is_expanded: false,
};
len += 1;
}
if m.set_type != typedef::SetType(u8::MAX) {
arr[len] = Field {
num: 5,
profile_type: ProfileType::SET_TYPE,
value: Value::Uint8(m.set_type.0),
is_expanded: false,
};
len += 1;
}
if m.start_time != typedef::DateTime(u32::MAX) {
arr[len] = Field {
num: 6,
profile_type: ProfileType::DATE_TIME,
value: Value::Uint32(m.start_time.0),
is_expanded: false,
};
len += 1;
}
if m.category != Vec::<typedef::ExerciseCategory>::new() {
arr[len] = Field {
num: 7,
profile_type: ProfileType::EXERCISE_CATEGORY,
value: Value::VecUint16({
let mut v = Vec::with_capacity(m.category.len());
for x in &m.category {
v.push(x.0)
}
v
}),
is_expanded: false,
};
len += 1;
}
if m.category_subtype != Vec::<u16>::new() {
arr[len] = Field {
num: 8,
profile_type: ProfileType::UINT16,
value: Value::VecUint16(m.category_subtype),
is_expanded: false,
};
len += 1;
}
if m.weight_display_unit != typedef::FitBaseUnit(u16::MAX) {
arr[len] = Field {
num: 9,
profile_type: ProfileType::FIT_BASE_UNIT,
value: Value::Uint16(m.weight_display_unit.0),
is_expanded: false,
};
len += 1;
}
if m.message_index != typedef::MessageIndex(u16::MAX) {
arr[len] = Field {
num: 10,
profile_type: ProfileType::MESSAGE_INDEX,
value: Value::Uint16(m.message_index.0),
is_expanded: false,
};
len += 1;
}
if m.wkt_step_index != typedef::MessageIndex(u16::MAX) {
arr[len] = Field {
num: 11,
profile_type: ProfileType::MESSAGE_INDEX,
value: Value::Uint16(m.wkt_step_index.0),
is_expanded: false,
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::SET,
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,
}
}
}