1#[derive(Debug)]
6#[non_exhaustive]
7pub enum Error {
8 Io(std::io::Error),
9 InvalidBox,
11 NonZeroPadding,
13 InvalidFloat,
15 InvalidEnum {
17 name: &'static str,
18 value: u32,
19 },
20 ValidationFailed(&'static str),
22 ProfileConformance(&'static str),
24 CannotSkip,
27 NotAligned,
29}
30
31impl std::error::Error for Error {
32 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
33 match self {
34 Self::Io(e) => Some(e),
35 _ => None,
36 }
37 }
38}
39
40impl std::fmt::Display for Error {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 match self {
43 Self::Io(e) => {
44 write!(f, "I/O error: {e}")
45 }
46 Self::InvalidBox => write!(f, "invalid container"),
47 Self::NonZeroPadding => {
48 write!(f, "PadZeroToByte() read non-zero bits")
49 }
50 Self::InvalidFloat => {
51 write!(f, "F16() read NaN or Infinity")
52 }
53 Self::InvalidEnum { name, value } => {
54 write!(f, "Enum({name}) read invalid enum value of {value}")
55 }
56 Self::ValidationFailed(msg) => {
57 write!(f, "bitstream validation failed: {msg}")
58 }
59 Self::ProfileConformance(msg) => {
60 write!(f, "not supported by current profile: {msg}")
61 }
62 Self::CannotSkip => {
63 write!(f, "target bookmark already passed")
64 }
65 Self::NotAligned => {
66 write!(f, "bitstream is unaligned")
67 }
68 }
69 }
70}
71
72impl From<std::io::Error> for Error {
73 fn from(e: std::io::Error) -> Self {
74 Self::Io(e)
75 }
76}
77
78impl Error {
79 pub fn unexpected_eof(&self) -> bool {
80 if let Error::Io(e) = self {
81 return e.kind() == std::io::ErrorKind::UnexpectedEof;
82 }
83 false
84 }
85}
86
87pub type BitstreamResult<T> = std::result::Result<T, Error>;