mp3_duration/
error.rs

1use std::io;
2use std::time::Duration;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6#[error("{} at offset {} (0x{1:X}); measured duration up to here: {:?}",
7        .kind, .offset, .at_duration)]
8pub struct MP3DurationError {
9    #[source]
10    pub kind: ErrorKind,
11    pub offset: usize,
12    pub at_duration: Duration,
13}
14
15#[derive(Debug, Error)]
16pub enum ErrorKind {
17    #[error("Invalid MPEG version")]
18    ForbiddenVersion,
19    #[error("Invalid MPEG Layer (0)")]
20    ForbiddenLayer,
21    #[error("Invalid bitrate bits: {0} (0b{0:b})", .bitrate)]
22    InvalidBitrate { bitrate: u8 },
23    #[error("Invalid sampling rate bits: {0} (0b{0:b})", .sampling_rate)]
24    InvalidSamplingRate { sampling_rate: u8 },
25    #[error("Unexpected frame, header 0x{:X}", .header)]
26    UnexpectedFrame { header: u32 },
27    #[error("Unexpected end of file")]
28    UnexpectedEOF,
29    #[error("MPEG frame too short")]
30    MPEGFrameTooShort,
31    #[error("Unexpected IO Error: {0}")]
32    IOError(#[source] io::Error),
33}
34
35impl From<io::Error> for ErrorKind {
36    fn from(e: io::Error) -> Self {
37        match e.kind() {
38            io::ErrorKind::UnexpectedEof => ErrorKind::UnexpectedEOF,
39            _ => ErrorKind::IOError(e),
40        }
41    }
42}