Skip to main content

chess_corners/
error.rs

1//! Top-level error type for the `chess-corners` facade.
2use crate::upscale::UpscaleError;
3use std::fmt;
4
5/// Errors returned by detection and heatmap entry points.
6///
7/// This type aggregates all failure modes reachable from the public
8/// API. The [`From`] implementation for [`UpscaleError`] lets callers
9/// propagate upscale failures with `?`.
10#[derive(Debug)]
11#[non_exhaustive]
12pub enum ChessError {
13    /// The supplied image slice length does not match `width * height`.
14    DimensionMismatch {
15        /// Expected length (`width * height`).
16        expected: usize,
17        /// Actual slice length.
18        actual: usize,
19    },
20    /// An upscale configuration or execution error.
21    Upscale(UpscaleError),
22    /// The configured refiner is not available on the single-scale ROI
23    /// detection path ([`Detector::detect_u8_roi`](crate::Detector::detect_u8_roi)
24    /// / [`Detector::detect_roi`](crate::Detector::detect_roi)). The ML
25    /// refiner runs a whole-frame model pipeline that the ROI path does
26    /// not carry; the refiner selection is never silently downgraded, so
27    /// this variant is returned instead. Select a built-in refiner
28    /// (center-of-mass, Förstner, or saddle-point) for ROI detection, or
29    /// use the whole-image [`Detector::detect_u8`](crate::Detector::detect_u8)
30    /// / [`Detector::detect`](crate::Detector::detect) entry points for ML
31    /// refinement.
32    #[cfg(feature = "ml-refiner")]
33    RoiRefinerUnsupported,
34}
35
36impl fmt::Display for ChessError {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Self::DimensionMismatch { expected, actual } => write!(
40                f,
41                "image buffer length mismatch: expected {expected} bytes (width*height), got {actual}"
42            ),
43            Self::Upscale(e) => write!(f, "upscale error: {e}"),
44            #[cfg(feature = "ml-refiner")]
45            Self::RoiRefinerUnsupported => write!(
46                f,
47                "configured refiner is not supported on the ROI detection path; \
48                 select center-of-mass, Förstner, or saddle-point, or use the \
49                 whole-image detect entry point"
50            ),
51        }
52    }
53}
54
55impl std::error::Error for ChessError {
56    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57        match self {
58            Self::Upscale(e) => Some(e),
59            _ => None,
60        }
61    }
62}
63
64impl From<UpscaleError> for ChessError {
65    fn from(e: UpscaleError) -> Self {
66        Self::Upscale(e)
67    }
68}