|
54 | 54 | //! std::fs::write("photo_modified.jpg", modified).unwrap(); |
55 | 55 | //! ``` |
56 | 56 |
|
57 | | -use thiserror::Error; |
58 | | - |
59 | 57 | // ── Public error type ───────────────────────────────────────────────────────── |
60 | 58 |
|
61 | 59 | /// Errors returned by this crate. |
62 | | -#[derive(Debug, Error)] |
| 60 | +#[derive(Debug)] |
63 | 61 | pub enum DctError { |
64 | 62 | /// The input does not start with a JPEG SOI marker (`0xFF 0xD8`). |
65 | | - #[error("not a JPEG file")] |
66 | 63 | NotJpeg, |
67 | 64 |
|
68 | 65 | /// The input was truncated mid-marker or mid-entropy-stream. |
69 | | - #[error("truncated JPEG data")] |
70 | 66 | Truncated, |
71 | 67 |
|
72 | 68 | /// The entropy-coded data contains an invalid Huffman symbol or an |
73 | 69 | /// unexpected structure. |
74 | | - #[error("corrupt or malformed JPEG entropy stream")] |
75 | 70 | CorruptEntropy, |
76 | 71 |
|
77 | 72 | /// The JPEG uses a feature this crate does not support (e.g. progressive |
78 | 73 | /// scan, lossless, or arithmetic coding). |
79 | | - #[error("unsupported JPEG variant: {0}")] |
80 | 74 | Unsupported(String), |
81 | 75 |
|
82 | 76 | /// A required marker or table is missing from the JPEG (e.g. no SOF, no |
83 | 77 | /// SOS, or a scan references a Huffman table that was not defined). |
84 | | - #[error("missing required JPEG structure: {0}")] |
85 | 78 | Missing(String), |
86 | 79 |
|
87 | 80 | /// The `JpegCoefficients` passed to [`write_coefficients`] is not |
88 | 81 | /// compatible with the JPEG (wrong number of components, wrong block |
89 | 82 | /// count, wrong component index). |
90 | | - #[error("coefficient data is incompatible with this JPEG: {0}")] |
91 | 83 | Incompatible(String), |
92 | 84 | } |
93 | 85 |
|
| 86 | +impl core::fmt::Display for DctError { |
| 87 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 88 | + match self { |
| 89 | + DctError::NotJpeg => f.write_str("not a JPEG file"), |
| 90 | + DctError::Truncated => f.write_str("truncated JPEG data"), |
| 91 | + DctError::CorruptEntropy => f.write_str("corrupt or malformed JPEG entropy stream"), |
| 92 | + DctError::Unsupported(s) => write!(f, "unsupported JPEG variant: {}", s), |
| 93 | + DctError::Missing(s) => write!(f, "missing required JPEG structure: {}", s), |
| 94 | + DctError::Incompatible(s) => { |
| 95 | + write!(f, "coefficient data is incompatible with this JPEG: {}", s) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl std::error::Error for DctError {} |
| 102 | + |
94 | 103 | // ── Public types ────────────────────────────────────────────────────────────── |
95 | 104 |
|
96 | 105 | /// Metadata for a single image component, as read from the SOF marker. |
|
0 commit comments