1use crate::error::LnmpError;
4
5#[derive(Debug, Clone, PartialEq)]
7pub enum BinaryError {
8 UnsupportedVersion {
10 found: u8,
12 supported: Vec<u8>,
14 },
15
16 InvalidFID {
18 fid: u16,
20 reason: String,
22 },
23
24 InvalidTypeTag {
26 tag: u8,
28 },
29
30 InvalidValue {
32 field_id: u16,
34 type_tag: u8,
36 reason: String,
38 },
39
40 TrailingData {
42 bytes_remaining: usize,
44 },
45
46 CanonicalViolation {
48 reason: String,
50 },
51
52 UnexpectedEof {
54 expected: usize,
56 found: usize,
58 },
59
60 InvalidVarInt {
62 reason: String,
64 },
65
66 InvalidUtf8 {
68 field_id: u16,
70 },
71
72 TextFormatError {
74 source: LnmpError,
76 },
77
78 NestingDepthExceeded {
80 depth: usize,
82 max: usize,
84 },
85
86 NestedStructureNotSupported,
88
89 RecordSizeExceeded {
91 size: usize,
93 max: usize,
95 },
96
97 InvalidNestedStructure {
99 reason: String,
101 },
102 UnsupportedFeature {
104 feature: String,
106 },
107 DeltaError {
109 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}