use lewton::VorbisError;
use std::fmt::{Display, Error, Formatter};
#[derive(Debug)]
pub enum DecoderError {
Wav,
Ogg(lewton::VorbisError),
}
#[derive(Debug)]
pub enum SoundError {
Io(std::io::Error),
NoBackend,
FailedToInitializeDevice(String),
InvalidHeader,
UnsupportedFormat,
PoisonedMutex,
MathError(String),
StreamingBufferAlreadyInUse,
DecoderError(DecoderError),
BufferFailedToLoad,
BufferIsNotLoaded,
}
impl From<std::io::Error> for SoundError {
fn from(e: std::io::Error) -> Self {
SoundError::Io(e)
}
}
impl<'a, T> From<std::sync::PoisonError<std::sync::MutexGuard<'a, T>>> for SoundError {
fn from(_: std::sync::PoisonError<std::sync::MutexGuard<'a, T>>) -> Self {
SoundError::PoisonedMutex
}
}
impl From<lewton::VorbisError> for SoundError {
fn from(ve: VorbisError) -> Self {
SoundError::DecoderError(DecoderError::Ogg(ve))
}
}
impl Display for SoundError {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match self {
SoundError::Io(io) => write!(f, "io error: {}", io),
SoundError::NoBackend => write!(f, "no backend implemented for current platform"),
SoundError::FailedToInitializeDevice(reason) => {
write!(f, "failed to initialize device. reason: {}", reason)
}
SoundError::InvalidHeader => write!(f, "invalid header of sound file"),
SoundError::UnsupportedFormat => write!(f, "unsupported format of sound file"),
SoundError::PoisonedMutex => write!(f, "attempt to use poisoned mutex"),
SoundError::MathError(reason) => {
write!(f, "math error has occurred. reason: {}", reason)
}
SoundError::StreamingBufferAlreadyInUse => {
write!(f, "streaming buffer in already in use")
}
SoundError::DecoderError(de) => write!(f, "internal decoder error: {:?}", de),
SoundError::BufferFailedToLoad => write!(f, "a buffer failed to load"),
SoundError::BufferIsNotLoaded => write!(f, "a buffer is not loaded yet"),
}
}
}
impl std::error::Error for SoundError {}