Skip to main content

puremp3/
error.rs

1///! Error types related to MP3 decoding.
2use std::{fmt, io};
3
4/// Error that can be raised during MP3 decoding.
5#[derive(Debug)]
6pub enum Error {
7    /// An error during the MP3 decoding process.
8    Mp3Error(Mp3Error),
9
10    // An IO error reading the underlying stream.
11    IoError(io::Error),
12}
13
14impl fmt::Display for Error {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        match self {
17            Error::Mp3Error(e) => write!(f, "MP3 Error: {}", e),
18            Error::IoError(e) => write!(f, "IO Error: {}", e),
19        }
20    }
21}
22
23impl std::error::Error for Error {
24    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
25        match self {
26            Error::Mp3Error(e) => Some(e),
27            Error::IoError(e) => Some(e),
28        }
29    }
30}
31
32#[derive(Debug)]
33pub enum Mp3Error {
34    /// Invalid or unknown data was encountered when reading the stream.
35    InvalidData(&'static str),
36
37    /// An unsupported MP3 feature is used in this MP3 stream.
38    Unsupported(&'static str),
39}
40
41impl fmt::Display for Mp3Error {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        match self {
44            Mp3Error::InvalidData(s) => write!(f, "Invalid data: {}", s),
45            Mp3Error::Unsupported(s) => write!(f, "Unsupported: {}", s),
46        }
47    }
48}
49
50impl std::error::Error for Mp3Error {}
51
52impl From<Mp3Error> for Error {
53    fn from(error: Mp3Error) -> Self {
54        Error::Mp3Error(error)
55    }
56}
57
58impl From<io::Error> for Error {
59    fn from(error: io::Error) -> Self {
60        Error::IoError(error)
61    }
62}