1#![allow(clippy::module_name_repetitions)]
2
3use std::num::TryFromIntError;
4
5use ps_buffer::BufferError;
6use thiserror::Error;
7
8#[derive(Error, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
10pub enum GFError {
11 #[error("Division by zero is undefined.")]
13 DivByZero,
14}
15
16#[derive(Error, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
19pub enum PolynomialFromSliceError {
20 #[error("Slice was {size} bytes; a polynomial over GF(256) holds at most 255 coefficients.")]
22 TooLong {
23 size: usize,
25 },
26}
27
28#[derive(Error, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
31pub enum PolynomialSetCoefficientsError {
32 #[error("Range {offset}..{end} exceeds maximum coefficient index 254.")]
34 OutOfBounds {
35 offset: u8,
37 end: usize,
39 },
40}
41
42#[derive(Error, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
44pub enum PolynomialMulError {
45 #[error("Result degree exceeds maximum of 254.")]
48 DegreeOverflow,
49}
50
51#[derive(Error, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
53pub enum PolynomialDivError {
54 #[error("Division by zero polynomial.")]
56 ZeroDivisor,
57 #[error(transparent)]
59 GFError(#[from] GFError),
60}
61
62#[derive(Error, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
64pub enum PolynomialXorError {
65 #[error("Coefficient iterator exceeded maximum length of 255.")]
67 TooManyCoefficients,
68}
69
70#[derive(Error, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
72pub enum EuclideanError {
73 #[error("Error-correction capability {t} exceeds the maximum of 127.")]
76 CapabilityTooHigh {
77 t: u8,
79 },
80 #[error(transparent)]
82 PolynomialDiv(#[from] PolynomialDivError),
83 #[error(transparent)]
85 PolynomialMul(#[from] PolynomialMulError),
86}
87
88#[derive(Error, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
91pub enum RSConstructorError {
92 #[error("Parity byte count {0} is odd; parity comprises two bytes per correctable error.")]
95 OddParityLength(usize),
96 #[error("Parity count must be <= 63.")]
99 ParityTooHigh,
100}
101
102#[derive(Error, Debug, Clone, PartialEq, Eq)]
105pub enum RSGenerateParityError {
106 #[error(transparent)]
108 Division(#[from] PolynomialDivError),
109 #[error(transparent)]
112 SetCoefficients(#[from] PolynomialSetCoefficientsError),
113}
114
115#[derive(Error, Debug, Clone, PartialEq, Eq)]
117pub enum RSEncodeError {
118 #[error(transparent)]
120 BufferError(#[from] BufferError),
121 #[error(transparent)]
123 RSGenerateParityError(#[from] RSGenerateParityError),
124}
125
126#[derive(Error, Debug, Clone, PartialEq, Eq)]
129pub enum RSComputeErrorsError {
130 #[error(transparent)]
132 GFError(#[from] GFError),
133 #[error(transparent)]
135 EuclideanError(#[from] EuclideanError),
136 #[error("Too many errors; input is unrecoverable.")]
138 TooManyErrors,
139 #[error("The error locator derivative evaluated to zero.")]
141 ZeroErrorLocatorDerivative,
142}
143
144#[derive(Error, Debug, Clone, PartialEq, Eq)]
147pub enum RSDecodeError {
148 #[error(transparent)]
150 BufferError(#[from] BufferError),
151 #[error("Input length {received} is less than the parity length {parity_bytes}.")]
154 InsufficientLength {
155 parity_bytes: u8,
157 received: usize,
159 },
160 #[error(transparent)]
162 RSComputeErrorsError(#[from] RSComputeErrorsError),
163 #[error(transparent)]
165 RSConstructorError(#[from] RSConstructorError),
166 #[error("Too many errors to correct. Error computation nevertheless returned a valid polynomial, which is unlikely. Usually you'll get RSComputeErrorsError(TooManyErrors) instead.")]
168 TooManyErrors,
169 #[error(transparent)]
171 TryFromIntError(#[from] TryFromIntError),
172}
173
174#[derive(Error, Debug, Clone, PartialEq, Eq)]
176pub enum EncodeError {
177 #[error(transparent)]
179 LongEccEncodeError(#[from] LongEccEncodeError),
180 #[error(transparent)]
182 RSConstructorError(#[from] RSConstructorError),
183 #[error(transparent)]
185 RSEncodeError(#[from] RSEncodeError),
186}
187
188#[derive(Error, Debug, Clone)]
190pub enum DecodeError {
191 #[error("Insufficient input bytes for parity count of {0}: {0} * 2 > {1}.")]
193 InsufficientParityBytes(u8, u8),
194 #[error(transparent)]
196 LongEccDecodeError(#[from] LongEccDecodeError),
197 #[error(transparent)]
199 RSConstructorError(#[from] RSConstructorError),
200 #[error(transparent)]
202 RSDecodeError(#[from] RSDecodeError),
203}
204
205#[derive(Error, Debug, Clone)]
207pub enum EccError {
208 #[error(transparent)]
210 EncodeError(#[from] EncodeError),
211 #[error(transparent)]
213 DecodeError(#[from] DecodeError),
214}
215
216#[derive(Error, Debug, Clone, PartialEq, Eq)]
218pub enum LongEccHeaderConstructorError {
219 #[error("Generating parity failed: {0}")]
221 GenerateParity(#[from] RSGenerateParityError),
222 #[error("Full length {0} does not match the derived codeword length {1}.")]
225 InvalidFullLength(u32, u64),
226 #[error("Message length {0} does not fit within full length {1}.")]
228 InvalidMessageLength(u32, u32),
229 #[error("Invalid parity count: {0}.")]
232 InvalidParityCount(u8),
233 #[error("Invalid segment-to-parity ratio: {0} <= 2 * {1}.")]
235 InvalidSegmentParityRatio(u8, u8),
236}
237
238#[derive(Error, Debug, Clone, PartialEq, Eq)]
241pub enum LongEccHeaderFromBytesError {
242 #[error("Incorrect magic number: {0:x}.")]
244 IncorrectMagic(u16),
245
246 #[error("Incorrect version number: {0}.")]
248 InvalidVersion(u8),
249
250 #[error("Header checksum incorrect.")]
252 IncorrectChecksum,
253
254 #[error("Header error correction failed: {0}")]
256 CorrectionFailed(#[from] RSDecodeError),
257
258 #[error("Message length {0} does not fit within full length {1}.")]
260 InvalidMessageLength(u32, u32),
261
262 #[error("Invalid segment-to-parity ratio: {0} <= 2 * {1}.")]
264 InvalidSegmentParityRatio(u8, u8),
265
266 #[error("Full length {0} does not match the derived codeword length {1}.")]
269 InvalidFullLength(u32, u64),
270}
271
272#[derive(Error, Debug, Clone, PartialEq, Eq)]
275pub enum LongEccHeaderFromByteSliceError {
276 #[error("Insufficient bytes for header: got {0}, need 32.")]
278 InsufficientBytes(usize),
279
280 #[error(transparent)]
282 FromBytes(#[from] LongEccHeaderFromBytesError),
283}
284
285#[derive(Error, Debug, Clone, PartialEq, Eq)]
287pub enum LongEccEncodeError {
288 #[error(transparent)]
290 BufferError(#[from] BufferError),
291 #[error("Parity {0} >= 64, which is too high.")]
294 InvalidParity(u8),
295 #[error("Invalid segment-to-parity ratio: {0} <= 2 * {1}.")]
297 InvalidSegmentParityRatio(u8, u8),
298 #[error("Long ECC header construction failed: {0}")]
300 LongEccHeaderCtor(#[from] LongEccHeaderConstructorError),
301 #[error(transparent)]
303 RSConstructorError(#[from] RSConstructorError),
304 #[error(transparent)]
306 RSGenerateParityError(#[from] RSGenerateParityError),
307 #[error(transparent)]
309 TryFromIntError(#[from] TryFromIntError),
310}
311
312#[derive(Error, Debug, Clone)]
314pub enum LongEccDecodeError {
315 #[error(transparent)]
317 BufferError(#[from] BufferError),
318 #[error("Fast validation failed: {0}")]
320 FastValidate(#[from] LongEccFastValidateError),
321 #[error("Integrity check failed after correction.")]
323 IntegrityCheckFailed,
324 #[error("Codeword is invalid.")]
326 InvalidCodeword,
327 #[error("Failed to decode header: {0}")]
329 HeaderDecode(#[from] LongEccHeaderFromByteSliceError),
330 #[error("Failed to read data bytes.")]
332 ReadDataError,
333 #[error("Failed to read parity bytes.")]
335 ReadParityError,
336 #[error(transparent)]
338 RSDecodeError(#[from] RSDecodeError),
339 #[error(transparent)]
341 TryFromIntError(#[from] TryFromIntError),
342}
343
344#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
345pub enum LongEccFastValidateError {
346 #[error("Header parsing failed: {0}")]
347 HeaderParse(#[from] crate::LongEccHeaderFromByteSliceError),
348 #[error("Integer conversion error: {0}")]
349 IntegerConversion(#[from] std::num::TryFromIntError),
350}