Skip to main content

oxideav_mjpeg/
error.rs

1//! Crate-local error type used by `oxideav-mjpeg`'s standalone
2//! (no `oxideav-core`) public API.
3//!
4//! Defined as a small std-only enum so the crate can be built with the
5//! default `registry` feature off — i.e. without depending on
6//! `oxideav-core` at all. When the `registry` feature is on (the
7//! default) a `From<MjpegError> for oxideav_core::Error` impl is
8//! enabled in [`crate::registry`] so the `Decoder`/`Encoder` trait
9//! surface still interoperates cleanly.
10
11use core::fmt;
12
13/// `Result` alias scoped to `oxideav-mjpeg`. Standalone (no
14/// `oxideav-core`) callers see this; framework callers convert via the
15/// gated `From<MjpegError> for oxideav_core::Error` impl.
16pub type Result<T> = core::result::Result<T, MjpegError>;
17
18/// Crate-local error type for the JPEG decoder/encoder pipeline.
19///
20/// Variants mirror the subset of `oxideav_core::Error` the codec can
21/// hit. Transport (`Io`) and framework-specific (`FormatNotFound`,
22/// `CodecNotFound`) errors are intentionally absent — they originate
23/// in callers that are already linking `oxideav-core`.
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub enum MjpegError {
26    /// The bitstream is malformed (bad marker, truncated segment,
27    /// invalid Huffman code length, etc.).
28    InvalidData(String),
29    /// The bitstream uses a feature this codec doesn't implement
30    /// (hierarchical SOF, progressive arithmetic, etc.) or the encoder
31    /// was asked to emit a frame in a format it doesn't support.
32    Unsupported(String),
33    /// Catch-all for misuse errors that aren't bitstream-level
34    /// (e.g. trait-API contract violations).
35    Other(String),
36    /// End of stream — no more packets / frames forthcoming.
37    Eof,
38    /// More input is required before another frame can be produced
39    /// (decoder) or another packet can be flushed (encoder).
40    NeedMore,
41}
42
43impl MjpegError {
44    /// Construct an [`MjpegError::InvalidData`] from a stringy message.
45    pub fn invalid(msg: impl Into<String>) -> Self {
46        Self::InvalidData(msg.into())
47    }
48
49    /// Construct an [`MjpegError::Unsupported`] from a stringy message.
50    pub fn unsupported(msg: impl Into<String>) -> Self {
51        Self::Unsupported(msg.into())
52    }
53
54    /// Construct an [`MjpegError::Other`] from a stringy message.
55    pub fn other(msg: impl Into<String>) -> Self {
56        Self::Other(msg.into())
57    }
58}
59
60impl fmt::Display for MjpegError {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match self {
63            Self::InvalidData(s) => write!(f, "invalid data: {s}"),
64            Self::Unsupported(s) => write!(f, "unsupported: {s}"),
65            Self::Other(s) => write!(f, "other: {s}"),
66            Self::Eof => write!(f, "end of stream"),
67            Self::NeedMore => write!(f, "need more data"),
68        }
69    }
70}
71
72impl std::error::Error for MjpegError {}