Skip to main content

st_protocol/
error.rs

1//! Protocol error types
2
3/// Protocol-specific errors
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum ProtocolError {
6    /// Invalid or unknown verb byte
7    InvalidVerb(u8),
8    /// Frame is too short to be valid
9    FrameTooShort,
10    /// Frame exceeds maximum size
11    FrameTooLarge,
12    /// Missing END marker
13    MissingEndMarker,
14    /// Invalid escape sequence
15    InvalidEscape,
16    /// Payload decode error
17    PayloadError,
18    /// Invalid address format
19    InvalidAddress,
20    /// Authentication required
21    AuthRequired,
22    /// Authentication failed
23    AuthFailed,
24    /// Invalid auth block structure
25    InvalidAuthBlock,
26    /// Insufficient privileges
27    InsufficientPrivileges,
28    /// Session expired or invalid
29    InvalidSession,
30    /// Unknown host in cache
31    UnknownHost(u8),
32    /// I/O error (std only)
33    #[cfg(feature = "std")]
34    Io(String),
35}
36
37impl core::fmt::Display for ProtocolError {
38    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
39        match self {
40            ProtocolError::InvalidVerb(b) => write!(f, "invalid verb byte: 0x{:02X}", b),
41            ProtocolError::FrameTooShort => write!(f, "frame too short"),
42            ProtocolError::FrameTooLarge => write!(f, "frame exceeds maximum size"),
43            ProtocolError::MissingEndMarker => write!(f, "missing END marker (0x00)"),
44            ProtocolError::InvalidEscape => write!(f, "invalid escape sequence"),
45            ProtocolError::PayloadError => write!(f, "payload decode error"),
46            ProtocolError::InvalidAddress => write!(f, "invalid address format"),
47            ProtocolError::AuthRequired => write!(f, "authentication required"),
48            ProtocolError::AuthFailed => write!(f, "authentication failed"),
49            ProtocolError::InvalidAuthBlock => write!(f, "invalid auth block structure"),
50            ProtocolError::InsufficientPrivileges => write!(f, "insufficient privileges"),
51            ProtocolError::InvalidSession => write!(f, "invalid or expired session"),
52            ProtocolError::UnknownHost(idx) => write!(f, "unknown host at index {}", idx),
53            #[cfg(feature = "std")]
54            ProtocolError::Io(msg) => write!(f, "I/O error: {}", msg),
55        }
56    }
57}
58
59#[cfg(feature = "std")]
60impl std::error::Error for ProtocolError {}
61
62/// Result type for protocol operations
63pub type ProtocolResult<T> = Result<T, ProtocolError>;