rg3d_sound/
error.rs

1//! Contains all possible errors that can occur in the engine.
2
3use lewton::VorbisError;
4use std::fmt::{Display, Error, Formatter};
5
6/// Decoder specific error.
7#[derive(Debug)]
8pub enum DecoderError {
9    /// WAV specific decoder error.
10    Wav,
11
12    /// Ogg/vorbis (lewton) specific error.
13    Ogg(lewton::VorbisError),
14}
15
16/// Generic error enumeration for each error in this engine.
17#[derive(Debug)]
18pub enum SoundError {
19    /// Generic input error.
20    Io(std::io::Error),
21
22    /// No backend is provided for current OS.
23    NoBackend,
24
25    /// Unable to initialize device, exact reason stored in inner value.
26    FailedToInitializeDevice(String),
27
28    /// Invalid header of sound file.
29    InvalidHeader,
30
31    /// Unsupported format of sound file.
32    UnsupportedFormat,
33
34    /// It means that some thread panicked while holding a MutexGuard, the data mutex
35    /// protected can be corrupted.
36    PoisonedMutex,
37
38    /// An error occurred during math calculations, i.e. there was an attempt to
39    /// normalize a vector with length `|v| == 0.0`.
40    MathError(String),
41
42    /// You tried to create a source with streaming buffer that is currently being
43    /// used by some other source. This is wrong because only one source can play
44    /// sound from streaming buffer.
45    StreamingBufferAlreadyInUse,
46
47    /// Decoder specific error, can occur in the decoder by any reason (invalid format,
48    /// insufficient data, etc.). Exact reason stored in inner value.
49    DecoderError(DecoderError),
50
51    /// A buffer is invalid (for example it is LoadError state)
52    BufferFailedToLoad,
53
54    /// A buffer is not loaded yet, consider to `await` it before use.
55    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 {}