use std::{io, fmt::Display, error};
use crate::cpal_abstraction::SampleType;
pub type Error<T> = Result<T, PlayError>;
#[derive(Debug)]
pub enum PlayError {
TimeOutOfBounds,
FileNotAccessible(io::Error),
WrongFileType,
DeviceIoError(String, Option<Box<dyn error::Error + 'static>>),
DeviceDoesNotSupportAudioSettings(Vec<AudioSettings>, Option<Box<dyn error::Error + 'static>>),
DeviceDoesNotExist{ name: String },
StreamIoError(String, Option<Box<dyn error::Error + 'static>>),
Unsupported(String),
}
impl Display for PlayError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::TimeOutOfBounds => f.write_str("the time specified is out of bounds"),
Self::FileNotAccessible(_) => f.write_str("there was an error while trying to access the file"),
Self::WrongFileType => f.write_str("file was of the wrong file type"),
Self::DeviceDoesNotExist{ name: n } => f.write_str(&format!("the device '{n}' does not exist")),
Self::DeviceIoError(c, _) => f.write_str(&format!("the device had an issue with io because {c}")),
Self::DeviceDoesNotSupportAudioSettings(s, _) => f.write_str(&format!("the device had an issue with config because {s:?} is/are not supported")),
Self::StreamIoError(s, _) => f.write_str(&format!("error while communicating with stream: {s}")),
Self::Unsupported(e) => f.write_str(&format!("ez_audi does not support '{}'", e)),
}
}
}
impl error::Error for PlayError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::TimeOutOfBounds => None,
Self::FileNotAccessible(e) => Some(e),
Self::WrongFileType => None,
Self::DeviceDoesNotExist{ .. } => None,
Self::DeviceIoError(_, s) => {
match s {
Some(s) => Some(&**s.clone()),
None => None,
}
},
Self::DeviceDoesNotSupportAudioSettings(_, s) => {
match s {
Some(s) => Some(&**s.clone()),
None => None,
}
},
Self::StreamIoError(_, s) => {
match s {
Some(s) => Some(&**s.clone()),
None => None,
}
},
Self::Unsupported(_) => None,
}
}
}
impl From<io::Error> for PlayError {
fn from(value: io::Error) -> Self {
Self::FileNotAccessible(value)
}
}
impl From<cpal::PlayStreamError> for PlayError {
fn from(value: cpal::PlayStreamError) -> Self {
Self::StreamIoError("failed play stream".to_string(), Some(Box::new(value)))
}
}
impl From<cpal::PauseStreamError> for PlayError {
fn from(value: cpal::PauseStreamError) -> Self {
Self::StreamIoError("failed pause stream".to_string(), Some(Box::new(value)))
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum AudioSettings {
SampleType(Option<SampleType>),
SampleRate(u32),
Channels(u32),
Combinaison,
}
impl Display for AudioSettings {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::SampleType(st) => f.write_str(&format!("the sample type {:?}", st)),
Self::SampleRate(r) => f.write_str(&format!("the sample rate of {:?}", r)),
Self::Channels(c) => f.write_str(&format!("the channel count of {:?}", c)),
Self::Combinaison => f.write_str("the combinaison of settings")
}
}
}