symphonium/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::error::Error;
use std::fmt;

#[derive(Debug)]
pub enum LoadError {
    FileNotFound(std::io::Error),
    UnkownFormat(symphonia::core::errors::Error),
    NoTrackFound,
    NoChannelsFound,
    UnkownChannelFormat(usize),
    FileTooLarge(usize),
    CouldNotCreateDecoder(symphonia::core::errors::Error),
    ErrorWhileDecoding(symphonia::core::errors::Error),
    UnexpectedErrorWhileDecoding(Box<dyn Error>),
    #[cfg(feature = "resampler")]
    ErrorWhileResampling(rubato::ResampleError),
}

impl Error for LoadError {}

impl fmt::Display for LoadError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use LoadError::*;

        match self {
            FileNotFound(e) => write!(f, "File not found | {}", e),
            UnkownFormat(e) => write!(f, "Format not supported | {}", e),
            NoTrackFound => write!(f, "No default track found"),
            NoChannelsFound => write!(f, "No channels found"),
            UnkownChannelFormat(n_channels) => {
                write!(f, "Unkown channel format | {} channels found", n_channels)
            }
            FileTooLarge(max_bytes) => {
                write!(f, "File is too large | maximum is {} bytes", max_bytes)
            }
            CouldNotCreateDecoder(e) => write!(f, "Failed to create decoder | {}", e),
            ErrorWhileDecoding(e) => write!(f, "Error while decoding | {}", e),
            UnexpectedErrorWhileDecoding(e) => write!(f, "Unexpected error while decoding | {}", e),
            #[cfg(feature = "resampler")]
            ErrorWhileResampling(e) => write!(f, "Error while resampling | {}", e),
        }
    }
}

impl From<std::io::Error> for LoadError {
    fn from(e: std::io::Error) -> Self {
        Self::FileNotFound(e)
    }
}

#[cfg(feature = "resampler")]
impl From<rubato::ResampleError> for LoadError {
    fn from(e: rubato::ResampleError) -> Self {
        Self::ErrorWhileResampling(e)
    }
}