#![allow(unused, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
use alloc::vec::Vec;
#[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::new(),
category_subtype: Vec::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
}
fn count_valid_fields(&self) -> usize {
(self.timestamp != typedef::DateTime(u32::MAX)) as usize
+ (self.duration != u32::MAX) as usize
+ (self.repetitions != u16::MAX) as usize
+ (self.weight != u16::MAX) as usize
+ (self.set_type != typedef::SetType(u8::MAX)) as usize
+ (self.start_time != typedef::DateTime(u32::MAX)) as usize
+ (!self.category.is_empty()) as usize
+ (!self.category_subtype.is_empty()) as usize
+ (self.weight_display_unit != typedef::FitBaseUnit(u16::MAX)) as usize
+ (self.message_index != typedef::MessageIndex(u16::MAX)) as usize
+ (self.wkt_step_index != typedef::MessageIndex(u16::MAX)) as usize
}
}
impl Default for Set {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for Set {
fn from(mesg: &Message) -> Self {
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 v = Self::new();
v.unknown_fields = Vec::<Field>::with_capacity(n as usize);
v.developer_fields = mesg.developer_fields.clone();
for field in &mesg.fields {
match field.num {
254 => v.timestamp = typedef::DateTime(field.value.as_u32()),
0 => v.duration = field.value.as_u32(),
3 => v.repetitions = field.value.as_u16(),
4 => v.weight = field.value.as_u16(),
5 => v.set_type = typedef::SetType(field.value.as_u8()),
6 => v.start_time = typedef::DateTime(field.value.as_u32()),
7 => {
v.category = match &field.value {
Value::VecUint16(v) => {
let mut vs = Vec::with_capacity(v.len());
vs.extend(v.iter().map(|&x| typedef::ExerciseCategory(x)));
vs
}
_ => Vec::new(),
}
}
8 => v.category_subtype = field.value.to_vec_u16(),
9 => v.weight_display_unit = typedef::FitBaseUnit(field.value.as_u16()),
10 => v.message_index = typedef::MessageIndex(field.value.as_u16()),
11 => v.wkt_step_index = typedef::MessageIndex(field.value.as_u16()),
_ => v.unknown_fields.push(field.clone()),
};
}
v
}
}
impl From<Set> for Message {
fn from(m: Set) -> Self {
let mut fields =
Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());
if m.timestamp != typedef::DateTime(u32::MAX) {
fields.push(Field {
num: 254,
profile_type: ProfileType::DATE_TIME,
value: Value::Uint32(m.timestamp.0),
is_expanded: false,
});
};
if m.duration != u32::MAX {
fields.push(Field {
num: 0,
profile_type: ProfileType::UINT32,
value: Value::Uint32(m.duration),
is_expanded: false,
});
};
if m.repetitions != u16::MAX {
fields.push(Field {
num: 3,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.repetitions),
is_expanded: false,
});
};
if m.weight != u16::MAX {
fields.push(Field {
num: 4,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.weight),
is_expanded: false,
});
};
if m.set_type != typedef::SetType(u8::MAX) {
fields.push(Field {
num: 5,
profile_type: ProfileType::SET_TYPE,
value: Value::Uint8(m.set_type.0),
is_expanded: false,
});
};
if m.start_time != typedef::DateTime(u32::MAX) {
fields.push(Field {
num: 6,
profile_type: ProfileType::DATE_TIME,
value: Value::Uint32(m.start_time.0),
is_expanded: false,
});
};
if !m.category.is_empty() {
fields.push(Field {
num: 7,
profile_type: ProfileType::EXERCISE_CATEGORY,
value: Value::VecUint16({
let (ptr, len, capacity) = m.category.into_raw_parts();
unsafe { Vec::from_raw_parts(ptr.cast::<u16>(), len, capacity) }
}),
is_expanded: false,
});
};
if !m.category_subtype.is_empty() {
fields.push(Field {
num: 8,
profile_type: ProfileType::UINT16,
value: Value::VecUint16(m.category_subtype),
is_expanded: false,
});
};
if m.weight_display_unit != typedef::FitBaseUnit(u16::MAX) {
fields.push(Field {
num: 9,
profile_type: ProfileType::FIT_BASE_UNIT,
value: Value::Uint16(m.weight_display_unit.0),
is_expanded: false,
});
};
if m.message_index != typedef::MessageIndex(u16::MAX) {
fields.push(Field {
num: 10,
profile_type: ProfileType::MESSAGE_INDEX,
value: Value::Uint16(m.message_index.0),
is_expanded: false,
});
};
if m.wkt_step_index != typedef::MessageIndex(u16::MAX) {
fields.push(Field {
num: 11,
profile_type: ProfileType::MESSAGE_INDEX,
value: Value::Uint16(m.wkt_step_index.0),
is_expanded: false,
});
};
fields.extend_from_slice(&m.unknown_fields);
Self {
header: 0,
num: typedef::MesgNum::SET,
fields,
developer_fields: m.developer_fields,
}
}
}