Skip to main content

opencv_rs_core/
error.rs

1//! Error types for the `opencv-rs` domain layer.
2//!
3//! Each port has its own typed error enum; [`OpenCvError`] is the umbrella
4//! type returned by high-level facades that may fail across any of them.
5
6use thiserror::Error;
7
8/// Convenience [`Result`] alias using [`OpenCvError`] as the error type.
9pub type Result<T> = std::result::Result<T, OpenCvError>;
10
11/// Errors produced by [`crate::VideoCapturePort`] and [`crate::VideoStream`].
12#[derive(Debug, Error)]
13pub enum VideoCaptureError {
14    /// The backend could not open the provided source path.
15    #[error("failed to open video source: {path}")]
16    OpenFailed {
17        /// The path (or URL) that the backend refused to open.
18        path: String,
19    },
20    /// An operation was invoked on a stream that is not currently open.
21    #[error("video stream is not open")]
22    NotOpen,
23    /// A frame read failed for reasons other than end-of-stream.
24    #[error("failed to read frame")]
25    ReadFailed,
26    /// The stream has been exhausted.
27    #[error("end of stream")]
28    EndOfStream,
29    /// The requested [`crate::Backend`] is not available on this platform.
30    #[error("unsupported backend")]
31    UnsupportedBackend,
32    /// Seeking to the requested position failed.
33    #[error("seek failed")]
34    SeekFailed,
35    /// The backend reported a non-positive or otherwise invalid FPS.
36    #[error("invalid fps property")]
37    InvalidFps,
38    /// A backend-specific error surfaced as a string.
39    #[error("backend error: {0}")]
40    Backend(String),
41}
42
43/// Errors produced by [`crate::ImageEncoderPort`].
44#[derive(Debug, Error)]
45pub enum ImageEncodingError {
46    /// The encoder failed for the given encoding kind.
47    #[error("encoding failed for kind {kind}")]
48    EncodeFailed {
49        /// Human-readable name of the encoding kind (e.g. `"jpeg"`).
50        kind: &'static str,
51    },
52    /// The encoder does not support the input's pixel format.
53    #[error("unsupported pixel format: {0:?}")]
54    UnsupportedPixelFormat(crate::PixelFormat),
55    /// A backend-specific error surfaced as a string.
56    #[error("backend error: {0}")]
57    Backend(String),
58}
59
60/// Errors produced by [`crate::ImageOpsPort`].
61#[derive(Debug, Error)]
62pub enum ImageOpsError {
63    /// Two inputs did not agree on `(width, height, channels)`.
64    #[error("dimension mismatch: lhs {lhs:?}, rhs {rhs:?}")]
65    DimensionMismatch {
66        /// Dimensions of the left-hand input as `(width, height, channels)`.
67        lhs: (u32, u32, u32),
68        /// Dimensions of the right-hand input as `(width, height, channels)`.
69        rhs: (u32, u32, u32),
70    },
71    /// The operation does not support the input's pixel format.
72    #[error("unsupported pixel format: {0:?}")]
73    UnsupportedPixelFormat(crate::PixelFormat),
74    /// The operation does not support the requested source/destination conversion.
75    #[error("unsupported conversion: {src:?} -> {dst:?}")]
76    UnsupportedConversion {
77        /// Source pixel format.
78        src: crate::PixelFormat,
79        /// Destination pixel format.
80        dst: crate::PixelFormat,
81    },
82    /// A parameter was out of range or otherwise invalid.
83    #[error("invalid parameter: {0}")]
84    InvalidParameter(&'static str),
85    /// The input buffer was empty where a non-empty one was required.
86    #[error("empty input")]
87    EmptyInput,
88    /// A backend-specific error surfaced as a string.
89    #[error("backend error: {0}")]
90    Backend(String),
91}
92
93/// Umbrella error type spanning every port in the domain.
94#[derive(Debug, Error)]
95pub enum OpenCvError {
96    /// A [`VideoCaptureError`] occurred.
97    #[error(transparent)]
98    VideoCapture(#[from] VideoCaptureError),
99    /// An [`ImageEncodingError`] occurred.
100    #[error(transparent)]
101    ImageEncoding(#[from] ImageEncodingError),
102    /// An [`ImageOpsError`] occurred.
103    #[error(transparent)]
104    ImageOps(#[from] ImageOpsError),
105}