|
| 1 | +use std::collections::TryReserveError; |
| 2 | +use std::fmt::Display; |
| 3 | + |
| 4 | +pub type Result<T> = std::result::Result<T, AudioError>; |
| 5 | + |
| 6 | +#[derive(Debug)] |
| 7 | +pub enum AudioError { |
| 8 | + // File data related errors |
| 9 | + /// Attempting to read/write an abnormally large amount of data |
| 10 | + TooMuchData, |
| 11 | + /// Expected the data to be a different size than provided |
| 12 | + /// |
| 13 | + /// This occurs when the size of an item is written as one value, but that size is either too |
| 14 | + /// big or small to be valid within the bounds of that item. |
| 15 | + // TODO: Should probably have context |
| 16 | + SizeMismatch, |
| 17 | + |
| 18 | + /// Errors that arise while decoding text |
| 19 | + TextDecode(&'static str), |
| 20 | + /// Arises when an atom contains invalid data |
| 21 | + BadAtom(&'static str), |
| 22 | + |
| 23 | + // Conversions for external errors |
| 24 | + /// Unable to convert bytes to a String |
| 25 | + StringFromUtf8(std::string::FromUtf8Error), |
| 26 | + /// Unable to convert bytes to a str |
| 27 | + StrFromUtf8(std::str::Utf8Error), |
| 28 | + /// Represents all cases of [`std::io::Error`]. |
| 29 | + Io(std::io::Error), |
| 30 | + /// Represents all cases of [`std::fmt::Error`]. |
| 31 | + Fmt(std::fmt::Error), |
| 32 | + /// Failure to allocate enough memory |
| 33 | + Alloc(TryReserveError), |
| 34 | + /// This should **never** be encountered |
| 35 | + Infallible(std::convert::Infallible), |
| 36 | +} |
| 37 | + |
| 38 | +impl Display for AudioError { |
| 39 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 40 | + match self { |
| 41 | + // Conversions |
| 42 | + AudioError::StringFromUtf8(err) => write!(f, "{err}"), |
| 43 | + AudioError::StrFromUtf8(err) => write!(f, "{err}"), |
| 44 | + AudioError::Io(err) => write!(f, "{err}"), |
| 45 | + AudioError::Fmt(err) => write!(f, "{err}"), |
| 46 | + AudioError::Alloc(err) => write!(f, "{err}"), |
| 47 | + AudioError::Infallible(_) => write!(f, "A expected condition was not upheld"), |
| 48 | + |
| 49 | + AudioError::TextDecode(message) => write!(f, "Text decoding: {message}"), |
| 50 | + AudioError::BadAtom(message) => write!(f, "MP4 Atom: {message}"), |
| 51 | + |
| 52 | + // Files |
| 53 | + AudioError::TooMuchData => write!( |
| 54 | + f, |
| 55 | + "Attempted to read/write an abnormally large amount of data" |
| 56 | + ), |
| 57 | + AudioError::SizeMismatch => write!( |
| 58 | + f, |
| 59 | + "Encountered an invalid item size, either too big or too small to be valid" |
| 60 | + ), |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl core::error::Error for AudioError {} |
| 66 | + |
| 67 | +impl From<std::io::Error> for AudioError { |
| 68 | + fn from(input: std::io::Error) -> Self { |
| 69 | + AudioError::Io(input) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl From<std::fmt::Error> for AudioError { |
| 74 | + fn from(input: std::fmt::Error) -> Self { |
| 75 | + AudioError::Fmt(input) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl From<std::string::FromUtf8Error> for AudioError { |
| 80 | + fn from(input: std::string::FromUtf8Error) -> Self { |
| 81 | + AudioError::StringFromUtf8(input) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl From<std::str::Utf8Error> for AudioError { |
| 86 | + fn from(input: std::str::Utf8Error) -> Self { |
| 87 | + AudioError::StrFromUtf8(input) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl From<TryReserveError> for AudioError { |
| 92 | + fn from(input: TryReserveError) -> Self { |
| 93 | + AudioError::Alloc(input) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl From<std::convert::Infallible> for AudioError { |
| 98 | + fn from(input: std::convert::Infallible) -> Self { |
| 99 | + AudioError::Infallible(input) |
| 100 | + } |
| 101 | +} |
0 commit comments