1use std::fmt;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum EdidError {
9 InvalidLength {
11 expected: usize,
13 actual: usize,
15 },
16 InvalidHeader,
18 InvalidChecksum {
20 sum: u8,
22 },
23 InvalidBlockSequenceLength {
25 actual: usize,
27 },
28 ExtensionCountMismatch {
30 declared: usize,
32 actual: usize,
34 },
35 ExtensionIndexOutOfRange {
37 index: usize,
39 count: usize,
41 },
42 TooManyExtensions {
44 count: usize,
46 },
47 UnsupportedVersion {
49 major: u8,
51 minor: u8,
53 },
54}
55
56#[derive(Clone, Debug, PartialEq, Eq)]
58#[non_exhaustive]
59pub enum MetadataError {
60 NotBaseBlock,
62 InvalidManufacturerId,
64 InvalidManufacturerCharacter {
66 index: usize,
68 character: char,
70 },
71 InvalidManufactureYear {
73 year: u16,
75 },
76 InvalidManufactureWeek {
78 week: u8,
80 },
81 InvalidGamma {
83 value: u16,
85 },
86}
87impl fmt::Display for MetadataError {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 match self {
90 Self::NotBaseBlock => f.write_str("block is not an EDID base block"),
91 Self::InvalidManufacturerId => f.write_str("invalid EDID manufacturer ID"),
92 Self::InvalidManufacturerCharacter { index, character } => {
93 write!(
94 f,
95 "invalid manufacturer character {character:?} at index {index}"
96 )
97 }
98 Self::InvalidManufactureYear { year } => {
99 write!(f, "manufacture year {year} is outside the EDID range")
100 }
101 Self::InvalidManufactureWeek { week } => {
102 write!(f, "manufacture week {week} uses a reserved EDID encoding")
103 }
104 Self::InvalidGamma { value } => {
105 write!(f, "gamma value {value} is outside the EDID range")
106 }
107 }
108 }
109}
110
111impl std::error::Error for MetadataError {}
112
113#[derive(Clone, Debug, PartialEq, Eq)]
118#[non_exhaustive]
119pub enum MetadataWriteError {
120 NotBaseBlock,
122 InvalidChromaticityValue {
124 value: u16,
126 },
127 InvalidStandardTiming {
129 horizontal_pixels: u16,
131 refresh_rate_hz: u8,
133 },
134 ReservedStandardTimingEncoding,
136 InvalidReservedStandardTimingEncoding {
138 raw: [u8; 2],
140 },
141}
142
143impl fmt::Display for MetadataWriteError {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 match self {
146 Self::NotBaseBlock => f.write_str("block is not an EDID base block"),
147 Self::InvalidChromaticityValue { value } => {
148 write!(
149 f,
150 "chromaticity value {value} exceeds the 10-bit EDID range"
151 )
152 }
153 Self::InvalidStandardTiming {
154 horizontal_pixels,
155 refresh_rate_hz,
156 } => write!(
157 f,
158 "standard timing {horizontal_pixels} pixels at {refresh_rate_hz} Hz is not representable"
159 ),
160 Self::ReservedStandardTimingEncoding => {
161 f.write_str("standard timing collides with the EDID unused-slot encoding")
162 }
163 Self::InvalidReservedStandardTimingEncoding { raw } => write!(
164 f,
165 "reserved standard timing encoding {raw:02X?} is a valid mode encoding"
166 ),
167 }
168 }
169}
170
171impl std::error::Error for MetadataWriteError {}
172
173#[derive(Clone, Debug, PartialEq, Eq)]
175#[non_exhaustive]
176pub enum DescriptorError {
177 SlotOutOfRange {
179 slot: usize,
181 slots: usize,
183 },
184 NotBaseBlock,
186 TextTooLong {
188 max: usize,
190 actual: usize,
192 },
193 NonAsciiText,
195 RangeOutOfBounds,
197 InvalidChromaticityCoordinate {
199 value: u16,
201 },
202 InvalidGamma {
204 value: u16,
206 },
207 StandardTimingError(MetadataWriteError),
209 InvalidRevision {
211 tag: u8,
213 revision: u8,
215 },
216 InvalidRangeExtension,
218 InvalidCvtTiming {
220 slot: usize,
222 reason: &'static str,
224 },
225}
226impl fmt::Display for DescriptorError {
227 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
228 match self {
229 Self::SlotOutOfRange { slot, slots } => {
230 write!(
231 f,
232 "descriptor slot {slot} is out of range ({} slots)",
233 slots
234 )
235 }
236 Self::NotBaseBlock => f.write_str("block is not an EDID base block"),
237 Self::TextTooLong { max, actual } => {
238 write!(f, "descriptor text is {actual} bytes, maximum is {max}")
239 }
240 Self::NonAsciiText => f.write_str("descriptor text must be ASCII"),
241 Self::RangeOutOfBounds => f.write_str("descriptor range field is out of bounds"),
242 Self::InvalidChromaticityCoordinate { value } => {
243 write!(
244 f,
245 "descriptor chromaticity coordinate {value} exceeds 10 bits"
246 )
247 }
248 Self::InvalidGamma { value } => {
249 write!(
250 f,
251 "descriptor gamma value {value} is outside the EDID range"
252 )
253 }
254 Self::StandardTimingError(err) => write!(f, "descriptor standard timing error: {err}"),
255 Self::InvalidRevision { tag, revision } => {
256 write!(
257 f,
258 "invalid revision {revision:#04X} for descriptor tag {tag:#04X}"
259 )
260 }
261 Self::InvalidRangeExtension => f.write_str("invalid range limits extension parameters"),
262 Self::InvalidCvtTiming { slot, reason } => {
263 write!(f, "invalid CVT 3-byte timing at slot {slot}: {reason}")
264 }
265 }
266 }
267}
268
269impl std::error::Error for DescriptorError {}
270
271#[derive(Clone, Copy, Debug, PartialEq, Eq)]
273pub enum DtdField {
274 HorizontalActive,
276 VerticalActive,
278 HorizontalBlanking,
280 VerticalBlanking,
282 HorizontalFrontPorch,
284 HorizontalSync,
286 VerticalFrontPorch,
288 VerticalSync,
290 HorizontalBorder,
292 VerticalBorder,
294 PixelClockKHz,
296}
297
298#[derive(Clone, Debug, PartialEq, Eq)]
300#[non_exhaustive]
301pub enum DtdError {
302 SlotOutOfRange {
304 slot: usize,
306 slots: usize,
308 },
309 InvalidField {
311 field: DtdField,
313 value: u32,
315 },
316 FieldOutOfRange {
318 field: DtdField,
320 value: u32,
322 max: u32,
324 },
325 ArithmeticOverflow,
327 NoAvailableSlot {
329 requested: usize,
331 available: usize,
333 },
334}
335impl fmt::Display for DtdError {
336 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
337 match self {
338 Self::SlotOutOfRange { slot, slots } => {
339 write!(f, "DTD slot {slot} is out of range ({} slots)", slots)
340 }
341 Self::InvalidField { field, value } => {
342 write!(f, "DTD field {field:?} has invalid value {value}")
343 }
344 Self::FieldOutOfRange { field, value, max } => {
345 write!(f, "DTD field {field:?} value {value} exceeds {max}")
346 }
347 Self::NoAvailableSlot {
348 requested,
349 available,
350 } => write!(
351 f,
352 "requested {requested} timings but only {available} DTD slots are available"
353 ),
354 Self::ArithmeticOverflow => f.write_str("DTD timing arithmetic overflowed"),
355 }
356 }
357}
358
359impl std::error::Error for DtdError {}
360
361#[derive(Clone, Debug, PartialEq, Eq)]
363#[non_exhaustive]
364pub enum SerializeError {
365 InvalidExistingLength {
367 actual: usize,
369 },
370 InvalidExistingBlock {
372 index: usize,
374 source: EdidError,
376 },
377 ExtensionCountMismatch {
379 declared: usize,
381 actual: usize,
383 },
384 TimingUnavailable {
386 index: usize,
388 },
389 TimingDoesNotFit {
391 index: usize,
393 },
394 NoDtdSlot {
396 index: usize,
398 },
399 InvalidTiming {
401 index: usize,
403 source: DtdError,
405 },
406 TooManyExtensions {
408 count: usize,
410 },
411}
412
413impl fmt::Display for SerializeError {
414 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
415 match self {
416 Self::InvalidExistingLength { actual } => {
417 write!(
418 f,
419 "existing EDID length {actual} is not a whole block sequence"
420 )
421 }
422 Self::InvalidExistingBlock { index, source } => {
423 write!(f, "existing EDID block {index} is invalid: {source}")
424 }
425 Self::ExtensionCountMismatch { declared, actual } => write!(
426 f,
427 "EDID declares {declared} extension blocks but supplied {actual}"
428 ),
429 Self::TooManyExtensions { count } => {
430 write!(f, "EDID contains too many extension blocks: {count}")
431 }
432 Self::TimingUnavailable { index } => {
433 write!(f, "resolution {index} could not produce a timing")
434 }
435 Self::TimingDoesNotFit { index } => {
436 write!(f, "resolution {index} does not fit an EDID DTD")
437 }
438 Self::NoDtdSlot { index } => {
439 write!(f, "no DTD slot is available for resolution {index}")
440 }
441 Self::InvalidTiming { index, source } => {
442 write!(f, "timing {index} is invalid: {source}")
443 }
444 }
445 }
446}
447impl std::error::Error for SerializeError {
448 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
449 match self {
450 Self::InvalidExistingBlock { source, .. } => Some(source),
451 Self::InvalidTiming { source, .. } => Some(source),
452 _ => None,
453 }
454 }
455}
456
457impl fmt::Display for EdidError {
458 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
459 match self {
460 Self::InvalidLength { expected, actual } => {
461 write!(
462 f,
463 "invalid EDID block length: expected {expected}, got {actual}"
464 )
465 }
466 Self::InvalidBlockSequenceLength { actual } => {
467 write!(f, "invalid EDID block sequence length: got {actual} bytes")
468 }
469 Self::InvalidHeader => f.write_str("invalid EDID base-block header"),
470 Self::InvalidChecksum { sum } => {
471 write!(f, "invalid EDID checksum: byte sum modulo 256 is {sum}")
472 }
473 Self::ExtensionCountMismatch { declared, actual } => write!(
474 f,
475 "EDID declares {declared} extension blocks but supplied {actual}"
476 ),
477 Self::ExtensionIndexOutOfRange { index, count } => write!(
478 f,
479 "extension index {index} is outside the {count}-extension list"
480 ),
481 Self::TooManyExtensions { count } => {
482 write!(f, "EDID contains too many extension blocks: {count}")
483 }
484 Self::UnsupportedVersion { major, minor } => {
485 write!(f, "unsupported EDID version {major}.{minor}")
486 }
487 }
488 }
489}
490
491impl std::error::Error for EdidError {}
492
493#[derive(Clone, Debug, PartialEq, Eq)]
495#[non_exhaustive]
496pub enum ModelineError {
497 InsufficientTokens,
499 InvalidNumber {
501 token: String,
503 },
504 InvalidGeometry(&'static str),
506 InvalidPixelClock,
508}
509
510impl fmt::Display for ModelineError {
511 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
512 match self {
513 Self::InsufficientTokens => f.write_str("Modeline string has insufficient tokens"),
514 Self::InvalidNumber { token } => {
515 write!(f, "invalid numeric token in Modeline: {token:?}")
516 }
517 Self::InvalidGeometry(reason) => write!(f, "invalid Modeline geometry: {reason}"),
518 Self::InvalidPixelClock => {
519 f.write_str("Modeline pixel clock must be positive and non-zero")
520 }
521 }
522 }
523}
524
525impl std::error::Error for ModelineError {}
526
527#[derive(Clone, Debug, PartialEq, Eq)]
529#[non_exhaustive]
530pub enum HexError {
531 InvalidHexCharacter {
533 offset: usize,
535 character: char,
537 },
538 OddLength {
540 length: usize,
542 },
543 InvalidLength {
545 bytes: usize,
547 },
548 EdidError(EdidError),
550}
551
552impl fmt::Display for HexError {
553 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
554 match self {
555 Self::InvalidHexCharacter { offset, character } => {
556 write!(f, "invalid hex character {character:?} at index {offset}")
557 }
558 Self::OddLength { length } => {
559 write!(f, "hex string has odd length ({length} nibbles)")
560 }
561 Self::InvalidLength { bytes } => {
562 write!(
563 f,
564 "decoded {bytes} bytes, expected a multiple of 128 (EDID block size)"
565 )
566 }
567 Self::EdidError(err) => write!(f, "EDID validation error: {err}"),
568 }
569 }
570}
571
572impl std::error::Error for HexError {
573 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
574 match self {
575 Self::EdidError(err) => Some(err),
576 _ => None,
577 }
578 }
579}