mod heapless_message;
mod iter;
mod iter_raw;
mod iter_record;
mod raw_message;
pub mod record;
use crate::{
error::ImplementationError,
tag::message::record::{DecodeRecord, Record},
trace::MaybeDebug,
};
use core::{
any::type_name,
fmt,
fmt::{Debug, Display, Formatter},
};
pub use heapless_message::*;
pub use iter::*;
pub use iter_raw::*;
pub use iter_record::*;
pub use raw_message::*;
#[cfg(any(feature = "std", feature = "alloc"))]
mod message;
#[cfg(any(feature = "std", feature = "alloc"))]
pub use message::*;
#[derive(Debug)]
pub struct NotDecodedError(pub &'static str);
impl Display for NotDecodedError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "unsupported operation: {} does not contain decoded records", self.0)
}
}
pub trait MessageRecords<'m>: Sized + MaybeDebug {
type Error: ImplementationError;
const DECODED: bool;
fn iter_records<'i>(&'i self) -> Iter<'m, 'i>;
fn records(&self) -> Result<&[Record<'m>], Self::Error> {
let name: &'static str = type_name::<Self>();
if Self::DECODED {
unimplemented!("missing implementation of MessageRecords::records for {}", name)
} else {
Err(Self::Error::custom(NotDecodedError(name)))
}
}
fn push_record(&mut self, _rec: Record<'m>) -> Result<(), Self::Error> {
let name: &'static str = type_name::<Self>();
if Self::DECODED {
unimplemented!("missing implementation of MessageRecords::push_record for {}", name)
} else {
Err(Self::Error::custom(NotDecodedError(name)))
}
}
#[inline]
fn len(&self) -> usize {
if let Ok(slice) = self.records() {
slice.len()
} else {
self.iter_records().count()
}
}
#[inline]
fn is_empty(&self) -> bool {
if let Ok(slice) = self.records() {
slice.is_empty()
} else {
self.iter_records().next().is_none()
}
}
}
pub trait DecodeMessage<'m>: Sized + MessageRecords<'m> {
fn decode_message(bytes: &'m [u8]) -> Result<Self, Self::Error>;
}