reed_solomon_32/
err.rs

1enum UsageErrorCode {
2    InvalidEcc,
3    InvalidDataLen,
4    InvalidMessageLenForEcc,
5    InvalidCombinedLen,
6    InvalidSymbol,
7    InvalidErasePos,
8}
9
10pub struct UsageErrorMessage {
11    error_code: UsageErrorCode,
12}
13
14impl core::fmt::Debug for UsageErrorMessage {
15    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
16        match self.error_code {
17            UsageErrorCode::InvalidEcc =>
18                write!(f, "The number of Ecc symbols must be less than 31."),
19            UsageErrorCode::InvalidDataLen =>
20                write!(f, "The length of the input data or message is greater than 31 symbols."),
21            UsageErrorCode::InvalidMessageLenForEcc =>
22                write!(f, "The message buffer is shorter than the number of ECC symbols and thus cannot be valid."),
23            UsageErrorCode::InvalidCombinedLen =>
24                write!(f, "The combination of data and ECC symbols would create a message greater than the maximum of 31 symbols."),
25            UsageErrorCode::InvalidSymbol =>
26                write!(f, "Invalid symbol. All symbols must be be in the range [0, 31]."),
27            UsageErrorCode::InvalidErasePos =>
28                write!(f, "One of the erasure positions was greater than the message size."),
29        }
30    }
31}
32
33impl core::fmt::Display for UsageErrorMessage {
34    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
35        core::fmt::Debug::fmt(self, f)
36    }
37}
38
39/// An error that indicates that a parameter that was supplied to a function
40/// was invalid for that function.
41///
42/// Details on the exact error may be obtained by formatting the value.
43pub struct UsageError(pub UsageErrorMessage);
44
45impl core::fmt::Debug for UsageError{
46    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
47        write!(f, "Usage error: {}", self.0)
48    }
49}
50
51impl core::fmt::Display for UsageError {
52    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
53        core::fmt::Debug::fmt(self, f)
54    }
55}
56
57#[cfg(feature = "std")]
58impl std::error::Error for UsageError { }
59
60pub fn invalid_ecc() -> UsageError {
61    UsageError(UsageErrorMessage { error_code: UsageErrorCode::InvalidEcc })
62}
63
64pub fn invalid_data_len() -> UsageError {
65    UsageError(UsageErrorMessage { error_code: UsageErrorCode::InvalidDataLen })
66}
67
68pub fn invalid_data_len_for_ecc() -> UsageError {
69    UsageError(UsageErrorMessage { error_code: UsageErrorCode::InvalidMessageLenForEcc })
70}
71
72pub fn invalid_combined_len() -> UsageError {
73    UsageError(UsageErrorMessage { error_code: UsageErrorCode::InvalidCombinedLen })
74}
75
76pub fn invalid_symbol() -> UsageError {
77    UsageError(UsageErrorMessage { error_code: UsageErrorCode::InvalidSymbol })
78}
79
80pub fn invalid_erase_pos() -> UsageError {
81    UsageError(UsageErrorMessage { error_code: UsageErrorCode::InvalidErasePos })
82}
83
84/// And error occurred while attempting to correct a message.
85pub enum CorrectionError {
86    /// The message had too many errors and they could not be corrected
87    TooManyErrors,
88
89    /// An invalid parameter value was passed to the function. Format the
90    /// `UsageErrorMessage` in order to get more details about the error.
91    UsageError(UsageErrorMessage),
92}
93
94impl From<UsageError> for CorrectionError {
95    fn from(UsageError(message): UsageError) -> CorrectionError {
96        CorrectionError::UsageError(message)
97    }
98}
99
100impl core::fmt::Debug for CorrectionError {
101    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
102        match self {
103            CorrectionError::TooManyErrors =>
104                write!(f, "The message has too many errors and cannot be repaired"),
105            CorrectionError::UsageError(err) =>
106                write!(f, "Usage error: {}", err),
107        }
108    }
109}
110
111impl core::fmt::Display for CorrectionError {
112    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
113        core::fmt::Debug::fmt(self, f)
114    }
115}
116
117#[cfg(feature = "std")]
118impl std::error::Error for CorrectionError { }