memlink_protocol/
error.rs1use alloc::string::String;
7use thiserror::Error;
8
9#[derive(Error, Debug, Clone, PartialEq, Eq)]
10pub enum ProtocolError {
11 #[error("invalid magic number: expected 0x4D4C4E4B, got 0x{0:08X}")]
12 InvalidMagic(u32),
13
14 #[error("unsupported protocol version: {0}")]
15 UnsupportedVersion(u8),
16
17 #[error("invalid message type: {0}")]
18 InvalidMessageType(u8),
19
20 #[error("payload too large: {0} bytes (max: {1} bytes)")]
21 PayloadTooLarge(usize, usize),
22
23 #[error("checksum mismatch: expected 0x{expected:08X}, got 0x{actual:08X}")]
24 ChecksumMismatch {
25 expected: u32,
26 actual: u32,
27 },
28
29 #[error("serialization failed: {0}")]
30 SerializationFailed(String),
31
32 #[error("buffer overflow: required {required} bytes, available {available} bytes")]
33 BufferOverflow {
34 required: usize,
35 available: usize,
36 },
37
38 #[error("invalid header: {0}")]
39 InvalidHeader(String),
40
41 #[error("invalid request ID: {0}")]
42 InvalidRequestId(u64),
43
44 #[error("unknown module ID: {0}")]
45 UnknownModule(u64),
46
47 #[error("unknown method hash: 0x{0:08X}")]
48 UnknownMethod(u32),
49
50 #[error("operation timed out after {0} ms")]
51 Timeout(u64),
52
53 #[error("connection closed: {0}")]
54 ConnectionClosed(String),
55
56 #[error("resource quota exceeded: {0}")]
57 QuotaExceeded(String),
58}
59
60pub type Result<T, E = ProtocolError> = core::result::Result<T, E>;
61
62impl ProtocolError {
63 pub fn is_recoverable(&self) -> bool {
64 matches!(
65 self,
66 ProtocolError::Timeout(_)
67 | ProtocolError::BufferOverflow { .. }
68 | ProtocolError::QuotaExceeded(_)
69 )
70 }
71
72 pub fn is_corruption(&self) -> bool {
73 matches!(
74 self,
75 ProtocolError::InvalidMagic(_)
76 | ProtocolError::ChecksumMismatch { .. }
77 | ProtocolError::InvalidHeader(_)
78 )
79 }
80
81 pub fn is_protocol_mismatch(&self) -> bool {
82 matches!(
83 self,
84 ProtocolError::InvalidMagic(_) | ProtocolError::UnsupportedVersion(_)
85 )
86 }
87}