1use std::error;
11use std::fmt;
12use std::io;
13use std::result;
14
15#[non_exhaustive]
17#[derive(Debug)]
18pub enum SeekErrorKind {
19 Unseekable,
21 ForwardOnly,
23 OutOfRange,
25 InvalidTrack,
27}
28
29impl SeekErrorKind {
30 fn as_str(&self) -> &'static str {
31 match *self {
32 SeekErrorKind::Unseekable => "stream is not seekable",
33 SeekErrorKind::ForwardOnly => "stream can only be seeked forward",
34 SeekErrorKind::OutOfRange => "requested seek timestamp is out-of-range for stream",
35 SeekErrorKind::InvalidTrack => "invalid track id",
36 }
37 }
38}
39
40#[non_exhaustive]
42#[derive(Debug)]
43pub enum Error {
44 IoError(std::io::Error),
46 DecodeError(&'static str),
48 SeekError(SeekErrorKind),
50 Unsupported(&'static str),
52 LimitError(&'static str),
55 ResetRequired,
57}
58
59impl fmt::Display for Error {
60 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 match *self {
62 Error::IoError(ref err) => err.fmt(f),
63 Error::DecodeError(msg) => {
64 write!(f, "malformed stream: {msg}")
65 }
66 Error::SeekError(ref kind) => {
67 write!(f, "seek error: {}", kind.as_str())
68 }
69 Error::Unsupported(feature) => {
70 write!(f, "unsupported feature: {feature}")
71 }
72 Error::LimitError(constraint) => {
73 write!(f, "limit reached: {constraint}")
74 }
75 Error::ResetRequired => {
76 write!(f, "decoder needs to be reset")
77 }
78 }
79 }
80}
81
82impl std::error::Error for Error {
83 fn cause(&self) -> Option<&dyn error::Error> {
84 match *self {
85 Error::IoError(ref err) => Some(err),
86 Error::DecodeError(_) => None,
87 Error::SeekError(_) => None,
88 Error::Unsupported(_) => None,
89 Error::LimitError(_) => None,
90 Error::ResetRequired => None,
91 }
92 }
93}
94
95impl From<io::Error> for Error {
96 fn from(err: io::Error) -> Error {
97 Error::IoError(err)
98 }
99}
100
101pub type Result<T> = result::Result<T, Error>;
102
103pub fn decode_error<T>(desc: &'static str) -> Result<T> {
105 Err(Error::DecodeError(desc))
106}
107
108pub fn seek_error<T>(kind: SeekErrorKind) -> Result<T> {
110 Err(Error::SeekError(kind))
111}
112
113pub fn unsupported_error<T>(feature: &'static str) -> Result<T> {
115 Err(Error::Unsupported(feature))
116}
117
118pub fn limit_error<T>(constraint: &'static str) -> Result<T> {
120 Err(Error::LimitError(constraint))
121}
122
123pub fn reset_error<T>() -> Result<T> {
125 Err(Error::ResetRequired)
126}