ndn_packet/
error.rs

1#[cfg(not(feature = "std"))]
2use alloc::string::String;
3
4use ndn_tlv::TlvError;
5
6#[derive(Debug)]
7pub enum PacketError {
8    Tlv(TlvError),
9    UnknownPacketType(u64),
10    MalformedPacket(String),
11}
12
13impl From<TlvError> for PacketError {
14    fn from(e: TlvError) -> Self {
15        PacketError::Tlv(e)
16    }
17}
18
19impl core::fmt::Display for PacketError {
20    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21        match self {
22            PacketError::Tlv(e) => write!(f, "TLV error: {e}"),
23            PacketError::UnknownPacketType(t) => write!(f, "unknown packet type {t:#x}"),
24            PacketError::MalformedPacket(msg) => write!(f, "malformed packet: {msg}"),
25        }
26    }
27}
28
29impl core::error::Error for PacketError {
30    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
31        match self {
32            PacketError::Tlv(e) => Some(e),
33            _ => None,
34        }
35    }
36}