use crate::{
payload::{Data, Name, Pdu},
protocol::{can2_a::identifier::IdCan2A, can2_b::identifier::IdCan2B},
};
if_alloc! {
use crate::alloc::string::String;
}
pub trait Conversion<T>
where
Self: Sized,
{
type Error;
fn from_bits(bits: T) -> Self;
fn from_hex(hex_str: &str) -> Self;
fn try_from_bits(bits: T) -> Result<Self, Self::Error>;
fn try_from_hex(hex_str: &str) -> Result<Self, Self::Error>;
fn into_bits(self) -> T;
#[cfg(feature = "alloc")]
fn into_hex(self) -> String;
}
impl From<Pdu<Data>> for Pdu<Name> {
fn from(value: Pdu<Data>) -> Self {
Self::from_bits(value.into_bits())
}
}
impl From<Pdu<Name>> for Pdu<Data> {
fn from(value: Pdu<Name>) -> Self {
Self::from_bits(value.into_bits())
}
}
impl From<IdCan2A> for IdCan2B {
fn from(value: IdCan2A) -> Self {
Self::from_bits(value.into_bits().into())
}
}
#[cfg(test)]
mod impl_tests {
use super::*;
#[test]
fn test_data_from() {
let name_a = Pdu::<Name>::from_hex("FFFF82DF1AFFFFFF");
let data_a = Pdu::<Data>::from(name_a);
assert_eq!(Pdu::<Data>::from_hex("FFFF82DF1AFFFFFF"), data_a);
}
#[test]
fn test_name_from() {
let data_a = Pdu::<Data>::from_hex("FFFF82DF1AFFFFFF");
let name_a = Pdu::<Name>::from(data_a);
assert_eq!(Pdu::<Name>::from_hex("FFFF82DF1AFFFFFF"), name_a);
}
#[test]
fn test_extended_from() {
let id_std_a = IdCan2A::from_hex("00F");
let id_ext_a = IdCan2B::from(id_std_a);
assert_eq!(IdCan2B::from_hex("0000000F"), id_ext_a);
}
}