lnmp_codec/binary/
error.rs

1//! Error types for LNMP binary format operations.
2
3use crate::error::LnmpError;
4
5/// Error type for binary format operations
6#[derive(Debug, Clone, PartialEq)]
7pub enum BinaryError {
8    /// Unsupported protocol version
9    UnsupportedVersion {
10        /// Version byte that was found
11        found: u8,
12        /// List of supported versions
13        supported: Vec<u8>,
14    },
15
16    /// Invalid field identifier
17    InvalidFID {
18        /// The invalid FID value
19        fid: u16,
20        /// Reason why it's invalid
21        reason: String,
22    },
23
24    /// Invalid type tag
25    InvalidTypeTag {
26        /// The invalid type tag byte
27        tag: u8,
28    },
29
30    /// Invalid value data
31    InvalidValue {
32        /// Field ID where the error occurred
33        field_id: u16,
34        /// Type tag of the value
35        type_tag: u8,
36        /// Reason why the value is invalid
37        reason: String,
38    },
39
40    /// Trailing data after frame
41    TrailingData {
42        /// Number of bytes remaining
43        bytes_remaining: usize,
44    },
45
46    /// Canonical form violation
47    CanonicalViolation {
48        /// Description of the violation
49        reason: String,
50    },
51
52    /// Insufficient data (unexpected end of input)
53    UnexpectedEof {
54        /// Number of bytes expected
55        expected: usize,
56        /// Number of bytes found
57        found: usize,
58    },
59
60    /// Invalid VarInt encoding
61    InvalidVarInt {
62        /// Reason why the VarInt is invalid
63        reason: String,
64    },
65
66    /// Invalid UTF-8 in string
67    InvalidUtf8 {
68        /// Field ID where the error occurred
69        field_id: u16,
70    },
71
72    /// Conversion error from text format
73    TextFormatError {
74        /// The underlying text format error
75        source: LnmpError,
76    },
77
78    /// Nesting depth exceeded (v0.5)
79    NestingDepthExceeded {
80        /// Current depth
81        depth: usize,
82        /// Maximum allowed depth
83        max: usize,
84    },
85
86    /// Nested structure not supported (v0.4 compatibility)
87    NestedStructureNotSupported,
88
89    /// Record size exceeded (v0.5)
90    RecordSizeExceeded {
91        /// Current size in bytes
92        size: usize,
93        /// Maximum allowed size
94        max: usize,
95    },
96
97    /// Invalid nested structure (v0.5)
98    InvalidNestedStructure {
99        /// Reason why the structure is invalid
100        reason: String,
101    },
102    /// Unsupported feature flag (future v0.5 functionality)
103    UnsupportedFeature {
104        /// Name of the unsupported feature
105        feature: String,
106    },
107    /// Delta encoder/decoder related failure
108    DeltaError {
109        /// Reason describing the delta error
110        reason: String,
111    },
112}
113
114impl std::fmt::Display for BinaryError {
115    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116        match self {
117            BinaryError::UnsupportedVersion { found, supported } => {
118                write!(
119                    f,
120                    "Unsupported version 0x{:02X}, supported versions: {:?}",
121                    found, supported
122                )
123            }
124            BinaryError::InvalidFID { fid, reason } => {
125                write!(f, "Invalid FID {}: {}", fid, reason)
126            }
127            BinaryError::InvalidTypeTag { tag } => {
128                write!(f, "Invalid type tag: 0x{:02X}", tag)
129            }
130            BinaryError::InvalidValue {
131                field_id,
132                type_tag,
133                reason,
134            } => {
135                write!(
136                    f,
137                    "Invalid value for field {} (type tag 0x{:02X}): {}",
138                    field_id, type_tag, reason
139                )
140            }
141            BinaryError::TrailingData { bytes_remaining } => {
142                write!(f, "Trailing data: {} bytes remaining", bytes_remaining)
143            }
144            BinaryError::CanonicalViolation { reason } => {
145                write!(f, "Canonical form violation: {}", reason)
146            }
147            BinaryError::UnexpectedEof { expected, found } => {
148                write!(
149                    f,
150                    "Unexpected end of input: expected {} bytes, found {}",
151                    expected, found
152                )
153            }
154            BinaryError::InvalidVarInt { reason } => {
155                write!(f, "Invalid VarInt: {}", reason)
156            }
157            BinaryError::InvalidUtf8 { field_id } => {
158                write!(f, "Invalid UTF-8 in field {}", field_id)
159            }
160            BinaryError::TextFormatError { source } => {
161                write!(f, "Text format error: {}", source)
162            }
163            BinaryError::NestingDepthExceeded { depth, max } => {
164                write!(
165                    f,
166                    "Nesting depth exceeded: depth {} exceeds maximum {}",
167                    depth, max
168                )
169            }
170            BinaryError::NestedStructureNotSupported => {
171                write!(f, "Nested structures not supported in v0.4 binary format")
172            }
173            BinaryError::RecordSizeExceeded { size, max } => {
174                write!(
175                    f,
176                    "Record size exceeded: size {} bytes exceeds maximum {} bytes",
177                    size, max
178                )
179            }
180            BinaryError::InvalidNestedStructure { reason } => {
181                write!(f, "Invalid nested structure: {}", reason)
182            }
183            BinaryError::UnsupportedFeature { feature } => {
184                write!(f, "Unsupported binary feature: {}", feature)
185            }
186            BinaryError::DeltaError { reason } => {
187                write!(f, "Delta error: {}", reason)
188            }
189        }
190    }
191}
192
193impl std::error::Error for BinaryError {}
194
195impl From<LnmpError> for BinaryError {
196    fn from(err: LnmpError) -> Self {
197        BinaryError::TextFormatError { source: err }
198    }
199}
200
201impl From<crate::binary::delta::DeltaError> for BinaryError {
202    fn from(err: crate::binary::delta::DeltaError) -> Self {
203        BinaryError::DeltaError {
204            reason: format!("{}", err),
205        }
206    }
207}