1use std::{fmt, io};
2
3#[derive(Debug)]
6pub enum Error {
7 TooShort,
13
14 Utf8Error,
23 IncompleteRecord {
26 expected: usize,
27 actual: usize,
28 },
29 Io(io::Error),
32 UnexpectedSeparator {
35 expected: &'static [u8],
36 actual: Vec<u8>,
37 },
38 FieldOverflow {
41 field: &'static str,
42 size: usize,
43 actual: usize,
44 },
45 InvalidRange {
48 field: &'static str,
49 start: Vec<u8>,
50 end: Vec<u8>,
51 },
52 SequenceError {
55 fields: Vec<&'static str>,
56 previous: Vec<Vec<u8>>,
57 current: Vec<Vec<u8>>,
58 },
59 AlignmentError,
60 ParseError,
61}
62
63impl fmt::Display for Error {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 match self {
68 Error::TooShort => write!(f, "input data is too short for the fixed length"),
69 Error::Utf8Error => write!(f, "input data contains invalid UTF-8 sequence"),
70 Error::IncompleteRecord { expected, actual } => write!(
71 f,
72 "incomplete fixed record: expected {expected} bytes, got {actual} bytes"
73 ),
74 Error::Io(err) => write!(f, "I/O error while processing fixed record: {err}"),
75 Error::UnexpectedSeparator { expected, actual } => write!(
76 f,
77 "unexpected record separator: expected {expected:?}, got {actual:?}"
78 ),
79 Error::FieldOverflow {
80 field,
81 size,
82 actual,
83 } => write!(
84 f,
85 "field `{field}` is too wide for the fixed length: expected at most {size} bytes, got {actual} bytes"
86 ),
87 Error::InvalidRange { field, start, end } => write!(
88 f,
89 "invalid range for field `{field}`: start {start:?} is greater than end {end:?}"
90 ),
91 Error::SequenceError {
92 fields,
93 previous,
94 current,
95 } => write!(
96 f,
97 "record sequence is not ascending for fields {fields:?}: previous={previous:?}, current={current:?}"
98 ),
99 Error::AlignmentError => write!(f, "Alignment error"),
100 Error::ParseError => write!(f, "failed to parse field value"),
101 }
102 }
103}
104
105impl PartialEq for Error {
106 fn eq(&self, other: &Self) -> bool {
109 match (self, other) {
110 (Error::TooShort, Error::TooShort) => true,
111 (Error::Utf8Error, Error::Utf8Error) => true,
112 (
113 Error::IncompleteRecord {
114 expected: left_expected,
115 actual: left_actual,
116 },
117 Error::IncompleteRecord {
118 expected: right_expected,
119 actual: right_actual,
120 },
121 ) => left_expected == right_expected && left_actual == right_actual,
122 (Error::Io(left), Error::Io(right)) => left.kind() == right.kind(),
123 (
124 Error::UnexpectedSeparator {
125 expected: left_expected,
126 actual: left_actual,
127 },
128 Error::UnexpectedSeparator {
129 expected: right_expected,
130 actual: right_actual,
131 },
132 ) => left_expected == right_expected && left_actual == right_actual,
133 (
134 Error::FieldOverflow {
135 field: left_field,
136 size: left_size,
137 actual: left_actual,
138 },
139 Error::FieldOverflow {
140 field: right_field,
141 size: right_size,
142 actual: right_actual,
143 },
144 ) => {
145 left_field == right_field && left_size == right_size && left_actual == right_actual
146 }
147 (
148 Error::InvalidRange {
149 field: left_field,
150 start: left_start,
151 end: left_end,
152 },
153 Error::InvalidRange {
154 field: right_field,
155 start: right_start,
156 end: right_end,
157 },
158 ) => left_field == right_field && left_start == right_start && left_end == right_end,
159 (
160 Error::SequenceError {
161 fields: left_fields,
162 previous: left_previous,
163 current: left_current,
164 },
165 Error::SequenceError {
166 fields: right_fields,
167 previous: right_previous,
168 current: right_current,
169 },
170 ) => {
171 left_fields == right_fields
172 && left_previous == right_previous
173 && left_current == right_current
174 }
175 (Error::AlignmentError, Error::AlignmentError) => true,
176 (Error::ParseError, Error::ParseError) => true,
177 _ => false,
178 }
179 }
180}
181
182impl Eq for Error {}
183
184impl std::error::Error for Error {
185 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
188 match self {
189 Error::Io(err) => Some(err),
190 _ => None,
191 }
192 }
193}
194
195impl From<io::Error> for Error {
196 fn from(err: io::Error) -> Self {
199 Self::Io(err)
200 }
201}