use miette::Diagnostic;
use thiserror::Error;
#[derive(Debug, Error, Diagnostic)]
#[non_exhaustive]
pub enum ScanError {
#[error("unsupported or corrupt image: {details}")]
#[diagnostic(code("QRS-001"), help("supported formats: PNG, JPEG, WebP, GIF"))]
InvalidImage {
details: String,
},
#[error("image {width}x{height} exceeds limit {max_dimension}")]
#[diagnostic(
code("QRS-002"),
help("downscale the image or raise Limits::max_dimension")
)]
DimensionsExceeded {
width: u32,
height: u32,
max_dimension: u32,
},
#[error("pixel count for {width}x{height} exceeds limit {max_pixels}")]
#[diagnostic(code("QRS-003"), help("raise Limits::max_pixels if intentional"))]
PixelOverflow {
width: u32,
height: u32,
max_pixels: u64,
},
#[error("buffer length {got} does not match expected {expected}")]
#[diagnostic(
code("QRS-004"),
help("rgba8 expects w*h*4 bytes; luma8 expects w*h bytes")
)]
BufferMismatch {
got: usize,
expected: usize,
},
#[error("scan cancelled")]
#[diagnostic(code("QRS-005"))]
Cancelled,
#[error("invalid option `{option}`: {reason}")]
#[diagnostic(
code("QRS-006"),
help("each option's domain is in spec/02-profiles.md; fix the call, not the input")
)]
InvalidOption {
option: String,
reason: String,
},
}
impl ScanError {
#[must_use]
pub fn code(&self) -> &'static str {
match self {
Self::InvalidImage { .. } => "QRS-001",
Self::DimensionsExceeded { .. } => "QRS-002",
Self::PixelOverflow { .. } => "QRS-003",
Self::BufferMismatch { .. } => "QRS-004",
Self::Cancelled => "QRS-005",
Self::InvalidOption { .. } => "QRS-006",
}
}
pub(crate) fn invalid_option(option: &str, reason: impl Into<String>) -> Self {
Self::InvalidOption {
option: option.to_owned(),
reason: reason.into(),
}
}
#[must_use]
pub fn is_transient(&self) -> bool {
false
}
}
pub type Result<T> = std::result::Result<T, ScanError>;
#[cfg(test)]
mod tests {
use super::*;
fn all_variants() -> Vec<ScanError> {
vec![
ScanError::InvalidImage {
details: "not png".into(),
},
ScanError::DimensionsExceeded {
width: 12_000,
height: 9_000,
max_dimension: 10_000,
},
ScanError::PixelOverflow {
width: 4_000_000,
height: 4_000_000,
max_pixels: 64_000_000,
},
ScanError::BufferMismatch {
got: 11,
expected: 16,
},
ScanError::Cancelled,
ScanError::InvalidOption {
option: "budget_ms".into(),
reason: "above the maximum".into(),
},
]
}
#[test]
fn codes_are_unique_and_well_formed() {
let mut codes: Vec<&'static str> = all_variants().iter().map(ScanError::code).collect();
for code in &codes {
assert!(
code.starts_with("QRS-") && code.len() == 7,
"malformed code: {code}"
);
}
let total = codes.len();
codes.sort_unstable();
codes.dedup();
assert_eq!(codes.len(), total, "duplicate QRS codes");
}
#[test]
fn wire_code_matches_miette_diagnostic_code() {
use miette::Diagnostic;
for err in all_variants() {
let diagnostic = err.code().to_string();
let miette_code = Diagnostic::code(&err)
.map(|c| c.to_string())
.unwrap_or_default();
assert_eq!(diagnostic, miette_code, "wire/miette mismatch for {err}");
}
}
#[test]
fn expected_wire_codes_pinned() {
let pinned: Vec<&str> = all_variants().iter().map(ScanError::code).collect();
assert_eq!(
pinned,
vec![
"QRS-001", "QRS-002", "QRS-003", "QRS-004", "QRS-005", "QRS-006"
],
"wire codes are a published contract — never renumber"
);
}
#[test]
fn no_variant_is_transient_today() {
for err in all_variants() {
assert!(!err.is_transient(), "{err} wrongly marked transient");
}
}
#[test]
fn display_carries_structured_fields() {
let err = ScanError::DimensionsExceeded {
width: 12_000,
height: 9_000,
max_dimension: 10_000,
};
let msg = err.to_string();
assert!(
msg.contains("12000") && msg.contains("9000") && msg.contains("10000"),
"{msg}"
);
let err = ScanError::BufferMismatch {
got: 11,
expected: 16,
};
let msg = err.to_string();
assert!(msg.contains("11") && msg.contains("16"), "{msg}");
}
}