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}
23
24impl fmt::Display for ChessError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::DimensionMismatch { expected, actual } => write!(
28                f,
29                "image buffer length mismatch: expected {expected} bytes (width*height), got {actual}"
30            ),
31            Self::Upscale(e) => write!(f, "upscale error: {e}"),
32        }
33    }
34}
35
36impl std::error::Error for ChessError {
37    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
38        match self {
39            Self::Upscale(e) => Some(e),
40            _ => None,
41        }
42    }
43}
44
45impl From<UpscaleError> for ChessError {
46    fn from(e: UpscaleError) -> Self {
47        Self::Upscale(e)
48    }
49}