use core::fmt;
use core::marker::PhantomData;
use crate::messages::{base, field, FieldDef, FieldOut, Lap, Message, Session, RECORD_GLOBAL};
use crate::raw::RawField;
use crate::types::{Manufacturer, Timestamp};
pub const DEVELOPERS_MAX: usize = 8;
pub const DEVELOPER_FIELDS_MAX: usize = 32;
pub const SUMMARY_BYTES_MAX: usize = 128;
pub const FIELD_NAME_MAX: usize = 63;
pub const UNITS_MAX: usize = 15;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Developer {
application_id: [u8; 16],
manufacturer: Option<Manufacturer>,
application_version: Option<u32>,
}
impl Developer {
#[must_use]
pub const fn new(application_id: [u8; 16]) -> Self {
Self { application_id, manufacturer: None, application_version: None }
}
#[must_use]
pub const fn manufacturer(mut self, manufacturer: Manufacturer) -> Self {
self.manufacturer = Some(manufacturer);
self
}
#[must_use]
pub const fn application_version(mut self, version: u32) -> Self {
self.application_version = Some(version);
self
}
pub(crate) const fn application_id(&self) -> [u8; 16] {
self.application_id
}
pub(crate) const fn message(&self, index: u8) -> DeveloperDataId {
DeveloperDataId { developer: *self, index }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Dev {
pub(crate) index: u8,
pub(crate) file: u32,
}
impl Dev {
#[must_use]
pub const fn field<T: Scalar, K: Kind>(
self,
num: u8,
name: &str,
) -> FieldDescription<'_, T, K> {
FieldDescription::new(self, num, name, T::SIZE)
}
#[must_use]
pub const fn string<K: Kind>(
self,
num: u8,
name: &str,
max_len: u8,
) -> FieldDescription<'_, str, K> {
let size = match max_len.checked_add(1) {
Some(size) => size,
None => 0,
};
FieldDescription::new(self, num, name, size)
}
}
pub struct FieldDescription<'a, T: ?Sized, K> {
pub(crate) dev: Dev,
pub(crate) num: u8,
pub(crate) name: &'a str,
pub(crate) units: &'a str,
pub(crate) scale: Option<u8>,
pub(crate) offset: Option<i8>,
pub(crate) native: Option<u8>,
pub(crate) size: u8,
kind: Marker<T, K>,
}
impl<'a, T: ?Sized, K> FieldDescription<'a, T, K> {
const fn new(dev: Dev, num: u8, name: &'a str, size: u8) -> Self {
Self {
dev,
num,
name,
units: "",
scale: None,
offset: None,
native: None,
size,
kind: PhantomData,
}
}
#[must_use]
pub const fn units(mut self, units: &'a str) -> Self {
self.units = units;
self
}
#[must_use]
pub const fn scale(mut self, scale: u8) -> Self {
self.scale = Some(scale);
self
}
#[must_use]
pub const fn offset(mut self, offset: i8) -> Self {
self.offset = Some(offset);
self
}
#[must_use]
pub const fn native_field(mut self, num: u8) -> Self {
self.native = Some(num);
self
}
}
impl<T: ?Sized, K> fmt::Debug for FieldDescription<'_, T, K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FieldDescription")
.field("num", &self.num)
.field("name", &self.name)
.finish_non_exhaustive()
}
}
type Marker<T, K> = PhantomData<(fn() -> *const T, fn() -> K)>;
mod sealed {
use super::{Encoded, KindId};
pub trait Encode {
const BASE: u8;
fn encode(v: <Self as super::DevType>::Value<'_>) -> Encoded<'_>
where
Self: super::DevType;
}
pub trait Number: Encode {
const SIZE: u8;
fn le(v: Self) -> [u8; 8];
}
pub trait Belongs {
const KIND: KindId;
}
}
pub trait DevType: sealed::Encode {
type Value<'a>: Copy;
}
pub trait Scalar: DevType + sealed::Number {}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Encoded<'a>(pub(crate) Repr<'a>);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Repr<'a> {
Number([u8; 8]),
Str(&'a str),
}
macro_rules! scalar {
($($ty:ty => $base:ident, $nan:expr;)*) => {$(
impl DevType for $ty {
type Value<'a> = $ty;
}
impl sealed::Encode for $ty {
const BASE: u8 = base::$base;
fn encode(v: <Self as DevType>::Value<'_>) -> Encoded<'_> {
Encoded(Repr::Number(sealed::Number::le(v)))
}
}
impl Scalar for $ty {}
impl sealed::Number for $ty {
#[allow(clippy::cast_possible_truncation, reason = "a number type is at most 8 bytes")]
const SIZE: u8 = core::mem::size_of::<$ty>() as u8;
fn le(v: Self) -> [u8; 8] {
let mut le = [0xFF; 8];
let is_nan: fn($ty) -> bool = $nan;
if !is_nan(v) {
for (slot, byte) in le.iter_mut().zip(v.to_le_bytes()) {
*slot = byte;
}
}
le
}
}
)*};
}
scalar! {
u8 => UINT8, |_| false;
i8 => SINT8, |_| false;
u16 => UINT16, |_| false;
i16 => SINT16, |_| false;
u32 => UINT32, |_| false;
i32 => SINT32, |_| false;
u64 => UINT64, |_| false;
i64 => SINT64, |_| false;
f32 => FLOAT32, f32::is_nan;
f64 => FLOAT64, f64::is_nan;
}
impl DevType for str {
type Value<'a> = &'a str;
}
impl sealed::Encode for str {
const BASE: u8 = base::STRING;
fn encode(v: <Self as DevType>::Value<'_>) -> Encoded<'_> {
Encoded(Repr::Str(v))
}
}
pub trait Kind: sealed::Belongs {}
pub trait Summary: Kind {}
#[derive(Debug)]
pub enum InRecord {}
#[derive(Debug)]
pub enum InLap {}
#[derive(Debug)]
pub enum InSession {}
impl Kind for InRecord {}
impl Kind for InLap {}
impl Kind for InSession {}
impl sealed::Belongs for InRecord {
const KIND: KindId = KindId::Record;
}
impl sealed::Belongs for InLap {
const KIND: KindId = KindId::Lap;
}
impl sealed::Belongs for InSession {
const KIND: KindId = KindId::Session;
}
impl Summary for InLap {}
impl Summary for InSession {}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KindId {
Record,
Lap,
Session,
}
impl KindId {
pub(crate) const fn global(self) -> u16 {
match self {
Self::Record => RECORD_GLOBAL,
Self::Lap => Lap::GLOBAL,
Self::Session => Session::GLOBAL,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Handle {
pub slot: u8,
pub dev: u8,
pub num: u8,
pub base: u8,
pub size: u8,
pub kind: KindId,
pub file: u32,
}
pub struct DevField<T: ?Sized, K> {
pub(crate) handle: Handle,
kind: Marker<T, K>,
}
pub type RecordField<T> = DevField<T, InRecord>;
pub type LapField<T> = DevField<T, InLap>;
pub type SessionField<T> = DevField<T, InSession>;
impl<T: ?Sized, K> DevField<T, K> {
pub(crate) const fn new(handle: Handle) -> Self {
Self { handle, kind: PhantomData }
}
}
impl<T: ?Sized, K> Clone for DevField<T, K> {
fn clone(&self) -> Self {
*self
}
}
impl<T: ?Sized, K> Copy for DevField<T, K> {}
impl<T: ?Sized, K> PartialEq for DevField<T, K> {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}
impl<T: ?Sized, K> Eq for DevField<T, K> {}
impl<T: ?Sized, K> fmt::Debug for DevField<T, K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DevField")
.field("developer_data_index", &self.handle.dev)
.field("field_definition_number", &self.handle.num)
.finish_non_exhaustive()
}
}
impl<T: DevType + ?Sized> RecordField<T> {
#[must_use]
pub fn value<'a>(&self, value: T::Value<'a>) -> Extra<'a> {
Extra(ExtraRepr::Dev(self.handle, T::encode(value)))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Extra<'a>(pub(crate) ExtraRepr<'a>);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ExtraRepr<'a> {
Dev(Handle, Encoded<'a>),
Raw(RawField<'a>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct DeveloperDataId {
developer: Developer,
index: u8,
}
impl Message for DeveloperDataId {
const GLOBAL: u16 = 207;
const FIELDS: &'static [FieldDef] = &[
field(1, 16, base::BYTE), field(2, 2, base::UINT16), field(3, 1, base::UINT8), field(4, 4, base::UINT32), ];
fn timestamp(&self) -> Option<Timestamp> {
None
}
fn encode(&self, out: &mut dyn FieldOut, _: u16) {
let d = &self.developer;
out.bytes(&d.application_id);
out.opt_u16(d.manufacturer.map(Manufacturer::get));
out.u8(self.index);
out.opt_u32(d.application_version);
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Description<'a> {
pub handle: Handle,
pub name: &'a str,
pub units: &'a str,
pub scale: Option<u8>,
pub offset: Option<i8>,
pub native: Option<u8>,
}
impl Message for Description<'_> {
const GLOBAL: u16 = 206;
#[allow(clippy::cast_possible_truncation, reason = "both fit a byte")]
const FIELDS: &'static [FieldDef] = &[
field(0, 1, base::UINT8), field(1, 1, base::UINT8), field(2, 1, base::UINT8), field(3, FIELD_NAME_MAX as u8 + 1, base::STRING), field(6, 1, base::UINT8), field(7, 1, base::SINT8), field(8, UNITS_MAX as u8 + 1, base::STRING), field(14, 2, base::UINT16), field(15, 1, base::UINT8), ];
fn timestamp(&self) -> Option<Timestamp> {
None
}
fn encode(&self, out: &mut dyn FieldOut, _: u16) {
let h = &self.handle;
out.u8(h.dev);
out.u8(h.num);
out.u8(h.base);
out.str(self.name);
out.opt_u8(self.scale);
out.opt_i8(self.offset);
out.str(self.units);
out.u16(h.kind.global());
out.opt_u8(self.native);
}
}