qrcode_barcode_scanner/
error.rs1use miette::Diagnostic;
7use thiserror::Error;
8
9#[derive(Debug, Error, Diagnostic)]
11#[non_exhaustive]
12pub enum ScanError {
13 #[error("unsupported or corrupt image: {details}")]
15 #[diagnostic(code("QRS-001"), help("supported formats: PNG, JPEG, WebP, GIF"))]
16 InvalidImage {
17 details: String,
19 },
20
21 #[error("image {width}x{height} exceeds limit {max_dimension}")]
23 #[diagnostic(
24 code("QRS-002"),
25 help("downscale the image or raise Limits::max_dimension")
26 )]
27 DimensionsExceeded {
28 width: u32,
30 height: u32,
32 max_dimension: u32,
34 },
35
36 #[error("pixel count for {width}x{height} exceeds limit {max_pixels}")]
38 #[diagnostic(code("QRS-003"), help("raise Limits::max_pixels if intentional"))]
39 PixelOverflow {
40 width: u32,
42 height: u32,
44 max_pixels: u64,
46 },
47
48 #[error("buffer length {got} does not match expected {expected}")]
50 #[diagnostic(
51 code("QRS-004"),
52 help("rgba8 expects w*h*4 bytes; luma8 expects w*h bytes")
53 )]
54 BufferMismatch {
55 got: usize,
57 expected: usize,
59 },
60
61 #[error("scan cancelled")]
63 #[diagnostic(code("QRS-005"))]
64 Cancelled,
65
66 #[error("invalid option `{option}`: {reason}")]
69 #[diagnostic(
70 code("QRS-006"),
71 help("each option's domain is in spec/02-profiles.md; fix the call, not the input")
72 )]
73 InvalidOption {
74 option: String,
76 reason: String,
78 },
79}
80
81impl ScanError {
82 #[must_use]
84 pub fn code(&self) -> &'static str {
85 match self {
86 Self::InvalidImage { .. } => "QRS-001",
87 Self::DimensionsExceeded { .. } => "QRS-002",
88 Self::PixelOverflow { .. } => "QRS-003",
89 Self::BufferMismatch { .. } => "QRS-004",
90 Self::Cancelled => "QRS-005",
91 Self::InvalidOption { .. } => "QRS-006",
92 }
93 }
94
95 pub(crate) fn invalid_option(option: &str, reason: impl Into<String>) -> Self {
97 Self::InvalidOption {
98 option: option.to_owned(),
99 reason: reason.into(),
100 }
101 }
102
103 #[must_use]
106 pub fn is_transient(&self) -> bool {
107 false
108 }
109}
110
111pub type Result<T> = std::result::Result<T, ScanError>;
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 fn all_variants() -> Vec<ScanError> {
119 vec![
120 ScanError::InvalidImage {
121 details: "not png".into(),
122 },
123 ScanError::DimensionsExceeded {
124 width: 12_000,
125 height: 9_000,
126 max_dimension: 10_000,
127 },
128 ScanError::PixelOverflow {
129 width: 4_000_000,
130 height: 4_000_000,
131 max_pixels: 64_000_000,
132 },
133 ScanError::BufferMismatch {
134 got: 11,
135 expected: 16,
136 },
137 ScanError::Cancelled,
138 ScanError::InvalidOption {
139 option: "budget_ms".into(),
140 reason: "above the maximum".into(),
141 },
142 ]
143 }
144
145 #[test]
146 fn codes_are_unique_and_well_formed() {
147 let mut codes: Vec<&'static str> = all_variants().iter().map(ScanError::code).collect();
148 for code in &codes {
149 assert!(
150 code.starts_with("QRS-") && code.len() == 7,
151 "malformed code: {code}"
152 );
153 }
154 let total = codes.len();
155 codes.sort_unstable();
156 codes.dedup();
157 assert_eq!(codes.len(), total, "duplicate QRS codes");
158 }
159
160 #[test]
161 fn wire_code_matches_miette_diagnostic_code() {
162 use miette::Diagnostic;
163 for err in all_variants() {
164 let diagnostic = err.code().to_string();
165 let miette_code = Diagnostic::code(&err)
166 .map(|c| c.to_string())
167 .unwrap_or_default();
168 assert_eq!(diagnostic, miette_code, "wire/miette mismatch for {err}");
169 }
170 }
171
172 #[test]
173 fn expected_wire_codes_pinned() {
174 let pinned: Vec<&str> = all_variants().iter().map(ScanError::code).collect();
175 assert_eq!(
176 pinned,
177 vec![
178 "QRS-001", "QRS-002", "QRS-003", "QRS-004", "QRS-005", "QRS-006"
179 ],
180 "wire codes are a published contract — never renumber"
181 );
182 }
183
184 #[test]
185 fn no_variant_is_transient_today() {
186 for err in all_variants() {
187 assert!(!err.is_transient(), "{err} wrongly marked transient");
188 }
189 }
190
191 #[test]
192 fn display_carries_structured_fields() {
193 let err = ScanError::DimensionsExceeded {
194 width: 12_000,
195 height: 9_000,
196 max_dimension: 10_000,
197 };
198 let msg = err.to_string();
199 assert!(
200 msg.contains("12000") && msg.contains("9000") && msg.contains("10000"),
201 "{msg}"
202 );
203
204 let err = ScanError::BufferMismatch {
205 got: 11,
206 expected: 16,
207 };
208 let msg = err.to_string();
209 assert!(msg.contains("11") && msg.contains("16"), "{msg}");
210 }
211}