Skip to main content

cu29_logstream/
error.rs

1use alloc::string::{String, ToString};
2use core::fmt::{Display, Formatter};
3
4pub type Result<T> = core::result::Result<T, Error>;
5
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub enum Error {
8    InvalidConfig(&'static str),
9    ObjectTooLarge { actual: u64, maximum: u64 },
10    TooManyRecords { actual: usize, maximum: usize },
11    TooManySessions { maximum: usize },
12    BufferTooSmall { needed: usize, available: usize },
13    TruncatedPacket,
14    InvalidMagic,
15    UnknownLane(u8),
16    UnknownRecordKind(u8),
17    UnknownFecScheme(u8),
18    UnknownSymbolKind(u8),
19    InvalidFecMetadata(&'static str),
20    InvalidFragment(&'static str),
21    InconsistentObject,
22    TruncatedRecord,
23    RecordDigestMismatch,
24    Codec(String),
25    Transport(&'static str),
26}
27
28impl Display for Error {
29    fn fmt(&self, formatter: &mut Formatter<'_>) -> core::fmt::Result {
30        match self {
31            Self::InvalidConfig(message) => write!(formatter, "invalid configuration: {message}"),
32            Self::ObjectTooLarge { actual, maximum } => {
33                write!(formatter, "object has {actual} bytes; maximum is {maximum}")
34            }
35            Self::TooManyRecords { actual, maximum } => {
36                write!(
37                    formatter,
38                    "stream has {actual} records; maximum is {maximum}"
39                )
40            }
41            Self::TooManySessions { maximum } => {
42                write!(
43                    formatter,
44                    "session router exceeds its {maximum}-session limit"
45                )
46            }
47            Self::BufferTooSmall { needed, available } => {
48                write!(
49                    formatter,
50                    "buffer needs {needed} bytes; only {available} available"
51                )
52            }
53            Self::TruncatedPacket => formatter.write_str("truncated logstream packet"),
54            Self::InvalidMagic => formatter.write_str("invalid logstream magic"),
55            Self::UnknownLane(value) => write!(formatter, "unknown logstream lane {value}"),
56            Self::UnknownRecordKind(value) => write!(formatter, "unknown record kind {value}"),
57            Self::UnknownFecScheme(value) => write!(formatter, "unknown FEC scheme {value}"),
58            Self::UnknownSymbolKind(value) => write!(formatter, "unknown FEC symbol kind {value}"),
59            Self::InvalidFecMetadata(message) => {
60                write!(formatter, "invalid FEC metadata: {message}")
61            }
62            Self::InvalidFragment(message) => write!(formatter, "invalid fragment: {message}"),
63            Self::InconsistentObject => formatter.write_str("packet belongs to a different object"),
64            Self::TruncatedRecord => formatter.write_str("truncated semantic record"),
65            Self::RecordDigestMismatch => formatter.write_str("semantic record digest mismatch"),
66            Self::Codec(message) => write!(formatter, "semantic codec failed: {message}"),
67            Self::Transport(message) => write!(formatter, "datagram transport failed: {message}"),
68        }
69    }
70}
71
72impl From<cu_fec::Error> for Error {
73    fn from(error: cu_fec::Error) -> Self {
74        Self::Codec(error.to_string())
75    }
76}
77
78impl From<cu_fec::WireError> for Error {
79    fn from(error: cu_fec::WireError) -> Self {
80        Self::Codec(error.to_string())
81    }
82}
83
84#[cfg(feature = "std")]
85impl std::error::Error for Error {}