use crate::{
conversion::Conversion,
identifier::{Id, IsProtocol},
payload::{Data, IsDataUnit, Name, Pdu},
protocol::j1939::identifier::J1939,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Message<P: IsProtocol, U: IsDataUnit> {
id: Id<P>,
pdu: Pdu<U>,
}
impl Message<J1939, Data> {
#[inline]
#[must_use]
pub fn from_parts(id: Id<J1939>, pdu: Pdu<Data>) -> Self {
Self { id, pdu }
}
#[inline]
#[must_use]
pub fn into_parts(self) -> (Id<J1939>, Pdu<Data>) {
(self.id, self.pdu)
}
#[inline]
pub fn try_from_bits(hex_id: u32, hex_pdu: u64) -> Result<Self, anyhow::Error> {
let id = Id::<J1939>::from_bits(hex_id);
let pdu = Pdu::<Data>::try_from_bits(hex_pdu)?;
Ok(Self { id, pdu })
}
#[inline]
pub fn try_from_hex(hex_id: &str, hex_pdu: &str) -> Result<Self, anyhow::Error> {
let id = Id::<J1939>::try_from_hex(hex_id)?;
let pdu = Pdu::<Data>::try_from_hex(hex_pdu)?;
Ok(Self { id, pdu })
}
#[inline]
#[must_use]
pub fn from_bits(hex_id: u32, hex_pdu: u64) -> Self {
let id = Id::<J1939>::from_bits(hex_id);
let pdu = Pdu::<Data>::from_bits(hex_pdu);
Self { id, pdu }
}
#[inline]
#[must_use]
pub fn from_hex(hex_id: &str, hex_pdu: &str) -> Self {
let id = Id::<J1939>::from_hex(hex_id);
let pdu = Pdu::<Data>::from_hex(hex_pdu);
Self { id, pdu }
}
#[inline]
#[must_use]
pub fn id(&self) -> Id<J1939> {
self.id
}
#[inline]
#[must_use]
pub fn pdu(&self) -> Pdu<Data> {
self.pdu
}
}
impl Message<J1939, Name> {
#[inline]
#[must_use]
pub fn from_parts(id: Id<J1939>, pdu: Pdu<Name>) -> Self {
Self { id, pdu }
}
#[inline]
#[must_use]
pub fn into_parts(self) -> (Id<J1939>, Pdu<Name>) {
(self.id, self.pdu)
}
#[inline]
pub fn try_from_bits(hex_id: u32, hex_pdu: u64) -> Result<Self, anyhow::Error> {
let id = Id::<J1939>::try_from_bits(hex_id)?;
let pdu = Pdu::<Name>::try_from_bits(hex_pdu)?;
Ok(Self { id, pdu })
}
#[inline]
pub fn try_from_hex(hex_id: &str, hex_pdu: &str) -> Result<Self, anyhow::Error> {
let id = Id::<J1939>::try_from_hex(hex_id)?;
let pdu = Pdu::<Name>::try_from_hex(hex_pdu)?;
Ok(Self { id, pdu })
}
#[inline]
#[must_use]
pub fn from_bits(hex_id: u32, hex_pdu: u64) -> Self {
let id = Id::<J1939>::from_bits(hex_id);
let pdu = Pdu::<Name>::from_bits(hex_pdu);
Self { id, pdu }
}
#[inline]
#[must_use]
pub fn from_hex(hex_id: &str, hex_pdu: &str) -> Self {
let id = Id::<J1939>::from_hex(hex_id);
let pdu = Pdu::<Name>::from_hex(hex_pdu);
Self { id, pdu }
}
#[inline]
#[must_use]
pub fn id(&self) -> Id<J1939> {
self.id
}
#[inline]
#[must_use]
pub fn pdu(&self) -> Pdu<Name> {
self.pdu
}
}
impl From<Message<J1939, Data>> for Message<J1939, Name> {
fn from(value: Message<J1939, Data>) -> Self {
Self {
id: value.id(),
pdu: value.pdu().into(),
}
}
}
impl From<Message<J1939, Name>> for Message<J1939, Data> {
fn from(value: Message<J1939, Name>) -> Self {
Self {
id: value.id(),
pdu: value.pdu().into(),
}
}
}