#![allow(unused, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
use crate::semconv;
use alloc::borrow::ToOwned;
use alloc::string::String;
use alloc::vec::Vec;
#[derive(Debug, Clone)]
pub struct CoursePoint {
pub message_index: typedef::MessageIndex,
pub timestamp: typedef::DateTime,
pub position_lat: i32,
pub position_long: i32,
pub distance: u32,
pub r#type: typedef::CoursePoint,
pub name: String,
pub favorite: typedef::Bool,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl CoursePoint {
pub const MESSAGE_INDEX: u8 = 254;
pub const TIMESTAMP: u8 = 1;
pub const POSITION_LAT: u8 = 2;
pub const POSITION_LONG: u8 = 3;
pub const DISTANCE: u8 = 4;
pub const TYPE: u8 = 5;
pub const NAME: u8 = 6;
pub const FAVORITE: u8 = 8;
pub const fn new() -> Self {
Self {
message_index: typedef::MessageIndex(u16::MAX),
timestamp: typedef::DateTime(u32::MAX),
position_lat: i32::MAX,
position_long: i32::MAX,
distance: u32::MAX,
r#type: typedef::CoursePoint(u8::MAX),
name: String::new(),
favorite: typedef::Bool(u8::MAX),
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn position_lat_degrees(&self) -> f64 {
semconv::to_degrees(self.position_lat)
}
pub fn position_long_degrees(&self) -> f64 {
semconv::to_degrees(self.position_long)
}
pub fn distance_scaled(&self) -> f64 {
if self.distance == u32::MAX {
return f64::from_bits(u64::MAX);
}
self.distance as f64 / 100.0 - 0.0
}
pub fn set_distance_scaled(&mut self, v: f64) -> &mut CoursePoint {
let unscaled = (v + 0.0) * 100.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
self.distance = u32::MAX;
return self;
}
self.distance = unscaled as u32;
self
}
fn count_valid_fields(&self) -> usize {
(self.message_index != typedef::MessageIndex(u16::MAX)) as usize
+ (self.timestamp != typedef::DateTime(u32::MAX)) as usize
+ (self.position_lat != i32::MAX) as usize
+ (self.position_long != i32::MAX) as usize
+ (self.distance != u32::MAX) as usize
+ (self.r#type != typedef::CoursePoint(u8::MAX)) as usize
+ (!self.name.is_empty()) as usize
+ (self.favorite != typedef::Bool(u8::MAX)) as usize
}
}
impl Default for CoursePoint {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for CoursePoint {
fn from(mesg: &Message) -> Self {
const KNOWN_NUMS: [u64; 4] = [382, 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.message_index = typedef::MessageIndex(field.value.as_u16()),
1 => v.timestamp = typedef::DateTime(field.value.as_u32()),
2 => v.position_lat = field.value.as_i32(),
3 => v.position_long = field.value.as_i32(),
4 => v.distance = field.value.as_u32(),
5 => v.r#type = typedef::CoursePoint(field.value.as_u8()),
6 => v.name = field.value.as_str().to_owned(),
8 => v.favorite = typedef::Bool(field.value.as_u8()),
_ => v.unknown_fields.push(field.clone()),
};
}
v
}
}
impl From<CoursePoint> for Message {
fn from(m: CoursePoint) -> Self {
let mut fields =
Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());
if m.message_index != typedef::MessageIndex(u16::MAX) {
fields.push(Field {
num: 254,
profile_type: ProfileType::MESSAGE_INDEX,
value: Value::Uint16(m.message_index.0),
is_expanded: false,
});
};
if m.timestamp != typedef::DateTime(u32::MAX) {
fields.push(Field {
num: 1,
profile_type: ProfileType::DATE_TIME,
value: Value::Uint32(m.timestamp.0),
is_expanded: false,
});
};
if m.position_lat != i32::MAX {
fields.push(Field {
num: 2,
profile_type: ProfileType::SINT32,
value: Value::Int32(m.position_lat),
is_expanded: false,
});
};
if m.position_long != i32::MAX {
fields.push(Field {
num: 3,
profile_type: ProfileType::SINT32,
value: Value::Int32(m.position_long),
is_expanded: false,
});
};
if m.distance != u32::MAX {
fields.push(Field {
num: 4,
profile_type: ProfileType::UINT32,
value: Value::Uint32(m.distance),
is_expanded: false,
});
};
if m.r#type != typedef::CoursePoint(u8::MAX) {
fields.push(Field {
num: 5,
profile_type: ProfileType::COURSE_POINT,
value: Value::Uint8(m.r#type.0),
is_expanded: false,
});
};
if !m.name.is_empty() {
fields.push(Field {
num: 6,
profile_type: ProfileType::STRING,
value: Value::String(m.name),
is_expanded: false,
});
};
if m.favorite != typedef::Bool(u8::MAX) {
fields.push(Field {
num: 8,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.favorite.0),
is_expanded: false,
});
};
fields.extend_from_slice(&m.unknown_fields);
Self {
header: 0,
num: typedef::MesgNum::COURSE_POINT,
fields,
developer_fields: m.developer_fields,
}
}
}