forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB2ea3bfcTahinli3 months ago999 commits

error.rs89 lines, 2.8 KiBHistoryRawDownload
1//! Family-wide error type.
2
3use std::fmt;
4
5/// Result alias used by every crate in the family.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Everything that can go wrong in a demuxer, decoder, encoder or muxer.
9///
10/// The taxonomy is deliberately small: callers branch on "give me more data"
11/// (`NeedMore`), "the stream ended" (`Eof`), "this input is broken" (`Corrupt`)
12/// and "we do not implement this" (`Unsupported`). Every `Unsupported` carries
13/// both *what* was refused and *why* — a refusal string without a reason is a
14/// bug report waiting to happen, not a diagnosis.
15#[derive(Debug)]
16pub enum Error {
17 /// Not enough data buffered yet; feed more and retry the same call.
18 NeedMore,
19 /// End of stream reached; no further data will ever arrive.
20 Eof,
21 /// A capability this build genuinely does not have.
22 Unsupported {
23 /// The construct that was refused, e.g. "HE-AAC SBR".
24 what: String,
25 /// Why it is refused, e.g. "SBR resampler not implemented".
26 why: String,
27 },
28 /// The bitstream violates its own format rules.
29 Corrupt {
30 /// Where and how, e.g. "H.264 SPS: log2_max_frame_num_minus4 = 13".
31 context: String,
32 },
33 /// Underlying I/O failure.
34 Io(std::io::Error),
35}
36
37impl Error {
38 /// Build an [`Error::Unsupported`] from any two string-likes.
39 pub fn unsupported(what: impl Into<String>, why: impl Into<String>) -> Self {
40 Error::Unsupported {
41 what: what.into(),
42 why: why.into(),
43 }
44 }
45
46 /// Build an [`Error::Corrupt`] from any string-like.
47 pub fn corrupt(context: impl Into<String>) -> Self {
48 Error::Corrupt {
49 context: context.into(),
50 }
51 }
52
53 /// True for [`Error::NeedMore`] — the streaming "try again later" contract.
54 pub fn is_need_more(&self) -> bool {
55 matches!(self, Error::NeedMore)
56 }
57
58 /// True for [`Error::Eof`].
59 pub fn is_eof(&self) -> bool {
60 matches!(self, Error::Eof)
61 }
62}
63
64impl fmt::Display for Error {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 match self {
67 Error::NeedMore => write!(f, "need more data"),
68 Error::Eof => write!(f, "end of stream"),
69 Error::Unsupported { what, why } => write!(f, "unsupported: {what} ({why})"),
70 Error::Corrupt { context } => write!(f, "corrupt bitstream: {context}"),
71 Error::Io(e) => write!(f, "io error: {e}"),
72 }
73 }
74}
75
76impl std::error::Error for Error {
77 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
78 match self {
79 Error::Io(e) => Some(e),
80 _ => None,
81 }
82 }
83}
84
85impl From<std::io::Error> for Error {
86 fn from(e: std::io::Error) -> Self {
87 Error::Io(e)
88 }
89}