Skip to content

Commit

Permalink
Input: Implement StdError for DcaError, input::Error (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
vilgotf authored and FelixMcFelix committed May 19, 2021
1 parent ab48c70 commit e1fc041
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion src/input/error.rs
Original file line number Diff line number Diff line change
@@ -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`].
Expand Down Expand Up @@ -68,6 +69,48 @@ impl From<OpusError> 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
Expand All @@ -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
Expand Down

0 comments on commit e1fc041

Please sign in to comment.