diff --git a/src/input/error.rs b/src/input/error.rs index f2082f8..01682e8 100644 --- a/src/input/error.rs +++ b/src/input/error.rs @@ -1,8 +1,9 @@ //! Errors caused by input creation. use audiopus::Error as OpusError; +use core::fmt; use serde_json::{Error as JsonError, Value}; -use std::{io::Error as IoError, process::Output}; +use std::{error::Error as StdError, io::Error as IoError, process::Output}; use streamcatcher::CatcherError; /// An error returned when creating a new [`Input`]. @@ -68,6 +69,48 @@ impl From for Error { } } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::Dca(e) => write!(f, "{}", e), + Error::Io(e) => write!(f, "{}", e), + Error::Json { + error, + parsed_text: _, + } => write!(f, "{}", error), + Error::Opus(e) => write!(f, "{}", e), + Error::Metadata => write!(f, "Failed to extract metadata"), + Error::Stdout => write!(f, "Failed to create stdout"), + Error::Streams => write!(f, "Error while checking if path is stereo"), + Error::Streamcatcher(e) => write!(f, "{}", e), + Error::YouTubeDlProcessing(_) => write!(f, "Processing JSON from youtube-dl failed"), + Error::YouTubeDlRun(_) => write!(f, "youtube-dl encountered an error"), + Error::YouTubeDlUrl(_) => write!(f, "Missing url field in JSON"), + } + } +} + +impl StdError for Error { + fn source(&self) -> Option<&(dyn StdError + 'static)> { + match self { + Error::Dca(e) => Some(e), + Error::Io(e) => Some(e), + Error::Json { + error, + parsed_text: _, + } => Some(error), + Error::Opus(e) => Some(e), + Error::Metadata => None, + Error::Stdout => None, + Error::Streams => None, + Error::Streamcatcher(e) => Some(e), + Error::YouTubeDlProcessing(_) => None, + Error::YouTubeDlRun(_) => None, + Error::YouTubeDlUrl(_) => None, + } + } +} + /// An error returned from the [`dca`] method. /// /// [`dca`]: crate::input::dca @@ -86,6 +129,30 @@ pub enum DcaError { Opus(OpusError), } +impl fmt::Display for DcaError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + DcaError::IoError(e) => write!(f, "{}", e), + DcaError::InvalidHeader => write!(f, "Invalid DCA JSON header"), + DcaError::InvalidMetadata(e) => write!(f, "{}", e), + DcaError::InvalidSize(e) => write!(f, "Invalid metadata block size: {}", e), + DcaError::Opus(e) => write!(f, "{}", e), + } + } +} + +impl StdError for DcaError { + fn source(&self) -> Option<&(dyn StdError + 'static)> { + match self { + DcaError::IoError(e) => Some(e), + DcaError::InvalidHeader => None, + DcaError::InvalidMetadata(e) => Some(e), + DcaError::InvalidSize(_) => None, + DcaError::Opus(e) => Some(e), + } + } +} + /// Convenience type for fallible return of [`Input`]s. /// /// [`Input`]: crate::input::Input