1use core::fmt;
8
9#[derive(Debug, Clone, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum Error {
13 Truncated(&'static str),
15 Malformed(&'static str),
17 Unsupported(&'static str),
19 MissingBox(&'static str),
21 MissingItem(u32),
23 MissingParameterSet(&'static str),
25 CyclicDerivation(u32),
27 PixelLimit {
31 pixels: u64,
33 max_pixels: u64,
35 },
36 #[cfg(feature = "std")]
39 Io(std::io::ErrorKind),
40 BoxTooLarge {
42 boxtype: [u8; 4],
44 size: u64,
46 },
47}
48
49impl Error {
50 fn fourcc(code: &[u8; 4]) -> &str {
52 core::str::from_utf8(code).unwrap_or("????")
53 }
54}
55
56impl fmt::Display for Error {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 match self {
59 Error::Truncated(what) => write!(f, "truncated file: ran out of bytes reading {what}"),
60 Error::Malformed(what) => write!(f, "malformed file: {what}"),
61 Error::Unsupported(what) => write!(f, "unsupported: {what}"),
62 Error::MissingBox(what) => write!(f, "required box `{what}` is missing"),
63 Error::MissingItem(id) => write!(f, "item {id} is referenced but not present"),
64 Error::MissingParameterSet(what) => {
65 write!(
66 f,
67 "the bitstream names a {what} that the file does not carry"
68 )
69 }
70 Error::CyclicDerivation(id) => {
71 write!(f, "item {id} derives from itself, directly or indirectly")
72 }
73 Error::PixelLimit { pixels, max_pixels } => write!(
74 f,
75 "image would be {pixels} pixels, above the {max_pixels} pixel limit"
76 ),
77 #[cfg(feature = "std")]
78 Error::Io(kind) => write!(f, "i/o error: {kind}"),
79 Error::BoxTooLarge { boxtype, size } => write!(
80 f,
81 "box `{}` declares an implausible size of {size} bytes",
82 Self::fourcc(boxtype)
83 ),
84 }
85 }
86}
87
88#[cfg(feature = "std")]
89impl std::error::Error for Error {}
90
91#[cfg(feature = "std")]
92impl From<std::io::Error> for Error {
93 fn from(e: std::io::Error) -> Error {
94 Error::Io(e.kind())
95 }
96}
97
98pub type Result<T> = core::result::Result<T, Error>;