oxideav_vp8/error.rs
1//! Public error surface for `oxideav-vp8`.
2//!
3//! `Vp8Error` is the **single public error type** downstream consumers
4//! build their own `From<oxideav_vp8::error::Vp8Error>` adapters against.
5//! Notably:
6//!
7//! * `oxideav-webp`'s lossy VP8 path: `From<oxideav_vp8::error::Vp8Error>
8//! for WebpError` collapses 1-1 (variants intentionally share names so
9//! the adapter is a single `match`).
10//! * Any other in-tree codec that wraps a VP8 substream (none right now;
11//! reserved for future).
12//!
13//! ## Shape
14//!
15//! Four variants, chosen to mirror the workspace's `WebpError` /
16//! `oxideav_core::Error` shape exactly:
17//!
18//! * [`InvalidData`](Vp8Error::InvalidData) — the bitstream was
19//! syntactically wrong (truncated, bad token, out-of-range header
20//! field, …). Carries a free-form message.
21//! * [`Unsupported`](Vp8Error::Unsupported) — the bitstream is valid VP8
22//! per RFC 6386 but uses a feature this build does not implement
23//! (deferred per the README ladder). Carries a message that names the
24//! feature.
25//! * [`Eof`](Vp8Error::Eof) — the decode/encode pipeline reached the end
26//! of the input stream cleanly (used by the `oxideav_core::Decoder` /
27//! `Encoder` adapters; see [`crate::decoder::Vp8Decoder`]).
28//! * [`NeedMore`](Vp8Error::NeedMore) — the pipeline needs another
29//! packet/frame before it can advance (`send_packet` then
30//! `receive_frame`).
31//!
32//! ## Standalone build
33//!
34//! This module is reachable with `default-features = false`. It does not
35//! pull in `oxideav-core` — every variant is built from `std` primitives
36//! only — so an embedded image / video pipeline that wants the VP8
37//! bitstream layer without the runtime registry still gets a usable
38//! public error type.
39
40use core::fmt;
41
42/// Crate-wide public error surface. See the [module docs](self) for the
43/// design rationale and the four-variant shape.
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub enum Vp8Error {
46 /// The bitstream was syntactically malformed: a truncated partition,
47 /// a token that escapes its tree, a header field out of its
48 /// RFC 6386-permitted range, etc. The message is free-form and meant
49 /// for human consumption.
50 InvalidData(String),
51 /// The bitstream is valid per RFC 6386 but exercises a feature this
52 /// build does not implement yet (e.g. an interframe arriving at the
53 /// stateless [`crate::decode_vp8`] entry point). The message names
54 /// the unsupported feature.
55 Unsupported(String),
56 /// The decode/encode pipeline has consumed every packet it was
57 /// given and there is nothing more to emit. Surfaced through the
58 /// `oxideav_core::Decoder` / `Encoder` adapter when `flush` has
59 /// run and the queue is empty.
60 Eof,
61 /// The decode/encode pipeline needs another input before it can
62 /// produce output. Surfaced through `receive_frame` /
63 /// `receive_packet` after a `send_*` / `flush` boundary when the
64 /// next stage of the pipeline is data-starved.
65 NeedMore,
66}
67
68impl Vp8Error {
69 /// Convenience constructor — accepts anything that converts to
70 /// `String`, so a `&'static str` literal works without a cast.
71 pub fn invalid(msg: impl Into<String>) -> Self {
72 Vp8Error::InvalidData(msg.into())
73 }
74
75 /// Convenience constructor — accepts anything that converts to
76 /// `String`.
77 pub fn unsupported(msg: impl Into<String>) -> Self {
78 Vp8Error::Unsupported(msg.into())
79 }
80}
81
82impl fmt::Display for Vp8Error {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 match self {
85 Vp8Error::InvalidData(msg) => write!(f, "oxideav-vp8: invalid data: {msg}"),
86 Vp8Error::Unsupported(msg) => write!(f, "oxideav-vp8: unsupported: {msg}"),
87 Vp8Error::Eof => f.write_str("oxideav-vp8: end of stream"),
88 Vp8Error::NeedMore => f.write_str("oxideav-vp8: need more input"),
89 }
90 }
91}
92
93impl std::error::Error for Vp8Error {}
94
95/// Crate-local convenience alias — every public entry point that can
96/// fail returns this.
97pub type Result<T> = core::result::Result<T, Vp8Error>;
98
99// ───── Adapters that fold the existing sub-error types into `Vp8Error`.
100
101impl From<crate::decoder::DecodeError> for Vp8Error {
102 fn from(e: crate::decoder::DecodeError) -> Self {
103 match e {
104 crate::decoder::DecodeError::Unsupported(msg) => Vp8Error::Unsupported(msg.to_string()),
105 other => Vp8Error::InvalidData(other.to_string()),
106 }
107 }
108}
109
110impl From<crate::Error> for Vp8Error {
111 fn from(e: crate::Error) -> Self {
112 match e {
113 crate::Error::NotImplemented => {
114 Vp8Error::Unsupported("requested operation not implemented".to_string())
115 }
116 }
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use super::*;
123
124 #[test]
125 fn invalid_helper_accepts_static_str() {
126 let e = Vp8Error::invalid("boom");
127 assert_eq!(e, Vp8Error::InvalidData("boom".to_string()));
128 }
129
130 #[test]
131 fn unsupported_helper_accepts_owned_string() {
132 let s = String::from("interframe");
133 let e = Vp8Error::unsupported(s);
134 assert_eq!(e, Vp8Error::Unsupported("interframe".to_string()));
135 }
136
137 #[test]
138 fn display_uses_crate_prefix() {
139 let e = Vp8Error::InvalidData("truncated partition".into());
140 let rendered = format!("{e}");
141 assert!(rendered.starts_with("oxideav-vp8:"));
142 assert!(rendered.contains("truncated partition"));
143 }
144
145 #[test]
146 fn eof_and_need_more_have_no_payload() {
147 // These are unit variants — guard against an accidental future
148 // payload addition (which would be a breaking change for
149 // downstream `From` matches).
150 let _eof = Vp8Error::Eof;
151 let _need = Vp8Error::NeedMore;
152 }
153
154 #[test]
155 fn is_a_std_error() {
156 fn assert_std_error<E: std::error::Error + Send + Sync + 'static>() {}
157 assert_std_error::<Vp8Error>();
158 }
159}