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