Skip to content

Commit

Permalink
remove thiserror dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluis committed Oct 17, 2024
1 parent a7f67b3 commit 309c8a9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ minimp3_fixed = { version = "0.5.4", optional = true}
symphonia = { version = "0.5.4", optional = true, default-features = false }
crossbeam-channel = { version = "0.5.8", optional = true }

thiserror = "1.0.49"
rand = { version = "0.8.5", features = ["small_rng"], optional = true }
tracing = { version = "0.1.40", optional = true }

Expand Down
49 changes: 43 additions & 6 deletions src/decoder/symphonia.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::time::Duration;
use core::fmt;
use core::time::Duration;
use symphonia::{
core::{
audio::{AudioBufferRef, SampleBuffer, SignalSpec},
Expand Down Expand Up @@ -209,21 +210,57 @@ impl Source for SymphoniaDecoder {
}

/// Error returned when the try_seek implementation of the symphonia decoder fails.
#[derive(Debug, thiserror::Error)]
#[derive(Debug)]
pub enum SeekError {
/// Could not get next packet while refining seek position
#[error("Could not get next packet while refining seek position: {0:?}")]
Refining(symphonia::core::errors::Error),
/// Format reader failed to seek
#[error("Format reader failed to seek: {0:?}")]
BaseSeek(symphonia::core::errors::Error),
/// Decoding failed retrying on the next packet failed
#[error("Decoding failed retrying on the next packet failed: {0:?}")]
Retrying(symphonia::core::errors::Error),
/// Decoding failed on multiple consecutive packets
#[error("Decoding failed on multiple consecutive packets: {0:?}")]
Decoding(symphonia::core::errors::Error),
}
impl fmt::Display for SeekError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SeekError::Refining(err) => {
write!(
f,
"Could not get next packet while refining seek position: {:?}",
err
)
}
SeekError::BaseSeek(err) => {
write!(f, "Format reader failed to seek: {:?}", err)
}
SeekError::Retrying(err) => {
write!(
f,
"Decoding failed retrying on the next packet failed: {:?}",
err
)
}
SeekError::Decoding(err) => {
write!(
f,
"Decoding failed on multiple consecutive packets: {:?}",
err
)
}
}
}
}
impl std::error::Error for SeekError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
SeekError::Refining(err) => Some(err),
SeekError::BaseSeek(err) => Some(err),
SeekError::Retrying(err) => Some(err),
SeekError::Decoding(err) => Some(err),
}
}
}

impl SymphoniaDecoder {
/// Note frame offset must be set after
Expand Down
49 changes: 41 additions & 8 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Sources of sound and various filters.

use std::time::Duration;
use core::fmt;
use core::time::Duration;

use cpal::FromSample;

Expand Down Expand Up @@ -585,28 +586,60 @@ where
/// Occurs when try_seek fails because the underlying decoder has an error or
/// does not support seeking.
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
#[derive(Debug)]
pub enum SeekError {
/// On of the underlying sources does not support seeking
#[error("Seeking is not supported by source: {underlying_source}")]
/// One of the underlying sources does not support seeking
NotSupported {
/// The source that did not support seek
underlying_source: &'static str,
},
#[cfg(feature = "symphonia")]
/// The symphonia decoder ran into an issue
#[error("Error seeking: {0}")]
SymphoniaDecoder(#[from] crate::decoder::symphonia::SeekError),
SymphoniaDecoder(crate::decoder::symphonia::SeekError),
#[cfg(feature = "wav")]
#[error("Error seeking in wav source: {0}")]
/// The hound (wav) decoder ran into an issue
HoundDecoder(std::io::Error),
// Prefer adding an enum variant to using this. Its meant for end users their
// own try_seek implementations
/// Any other error probably in a custom Source
#[error("An error occurred")]
Other(Box<dyn std::error::Error + Send>),
}
impl fmt::Display for SeekError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SeekError::NotSupported { underlying_source } => {
write!(
f,
"Seeking is not supported by source: {}",
underlying_source
)
}
#[cfg(feature = "symphonia")]
SeekError::SymphoniaDecoder(err) => write!(f, "Error seeking: {}", err),
#[cfg(feature = "wav")]
SeekError::HoundDecoder(err) => write!(f, "Error seeking in wav source: {}", err),
SeekError::Other(_) => write!(f, "An error occurred"),
}
}
}
impl std::error::Error for SeekError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
SeekError::NotSupported { .. } => None,
#[cfg(feature = "symphonia")]
SeekError::SymphoniaDecoder(err) => Some(err),
#[cfg(feature = "wav")]
SeekError::HoundDecoder(err) => Some(err),
SeekError::Other(err) => Some(err.as_ref()),
}
}
}
impl From<crate::decoder::symphonia::SeekError> for SeekError {
#[allow(deprecated)]
fn from(source: crate::decoder::symphonia::SeekError) -> Self {
SeekError::SymphoniaDecoder(source)
}
}

impl SeekError {
/// Will the source remain playing at its position before the seek or is it
Expand Down

0 comments on commit 309c8a9

Please sign in to comment.