1use lewton::VorbisError;
4use std::fmt::{Display, Error, Formatter};
5
6#[derive(Debug)]
8pub enum DecoderError {
9 Wav,
11
12 Ogg(lewton::VorbisError),
14}
15
16#[derive(Debug)]
18pub enum SoundError {
19 Io(std::io::Error),
21
22 NoBackend,
24
25 FailedToInitializeDevice(String),
27
28 InvalidHeader,
30
31 UnsupportedFormat,
33
34 PoisonedMutex,
37
38 MathError(String),
41
42 StreamingBufferAlreadyInUse,
46
47 DecoderError(DecoderError),
50
51 BufferFailedToLoad,
53
54 BufferIsNotLoaded,
56}
57
58impl From<std::io::Error> for SoundError {
59 fn from(e: std::io::Error) -> Self {
60 SoundError::Io(e)
61 }
62}
63
64impl<'a, T> From<std::sync::PoisonError<std::sync::MutexGuard<'a, T>>> for SoundError {
65 fn from(_: std::sync::PoisonError<std::sync::MutexGuard<'a, T>>) -> Self {
66 SoundError::PoisonedMutex
67 }
68}
69
70impl From<lewton::VorbisError> for SoundError {
71 fn from(ve: VorbisError) -> Self {
72 SoundError::DecoderError(DecoderError::Ogg(ve))
73 }
74}
75
76impl Display for SoundError {
77 fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
78 match self {
79 SoundError::Io(io) => write!(f, "io error: {}", io),
80 SoundError::NoBackend => write!(f, "no backend implemented for current platform"),
81 SoundError::FailedToInitializeDevice(reason) => {
82 write!(f, "failed to initialize device. reason: {}", reason)
83 }
84 SoundError::InvalidHeader => write!(f, "invalid header of sound file"),
85 SoundError::UnsupportedFormat => write!(f, "unsupported format of sound file"),
86 SoundError::PoisonedMutex => write!(f, "attempt to use poisoned mutex"),
87 SoundError::MathError(reason) => {
88 write!(f, "math error has occurred. reason: {}", reason)
89 }
90 SoundError::StreamingBufferAlreadyInUse => {
91 write!(f, "streaming buffer in already in use")
92 }
93 SoundError::DecoderError(de) => write!(f, "internal decoder error: {:?}", de),
94 SoundError::BufferFailedToLoad => write!(f, "a buffer failed to load"),
95 SoundError::BufferIsNotLoaded => write!(f, "a buffer is not loaded yet"),
96 }
97 }
98}
99
100impl std::error::Error for SoundError {}