fyrox_sound/
error.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Contains all possible errors that can occur in the engine.
22
23use lewton::VorbisError;
24use std::fmt::{Display, Error, Formatter};
25
26/// Decoder specific error.
27#[derive(Debug)]
28pub enum DecoderError {
29    /// WAV specific decoder error.
30    Wav,
31
32    /// Ogg/vorbis (lewton) specific error.
33    Ogg(lewton::VorbisError),
34}
35
36/// Generic error enumeration for each error in this engine.
37#[derive(Debug)]
38pub enum SoundError {
39    /// Generic input error.
40    Io(std::io::Error),
41
42    /// No backend is provided for current OS.
43    NoBackend,
44
45    /// Unable to initialize device, exact reason stored in inner value.
46    FailedToInitializeDevice(String),
47
48    /// Invalid header of sound file.
49    InvalidHeader,
50
51    /// Unsupported format of sound file.
52    UnsupportedFormat,
53
54    /// It means that some thread panicked while holding a MutexGuard, the data mutex
55    /// protected can be corrupted.
56    PoisonedMutex,
57
58    /// An error occurred during math calculations, i.e. there was an attempt to
59    /// normalize a vector with length `|v| == 0.0`.
60    MathError(String),
61
62    /// You tried to create a source with streaming buffer that is currently being
63    /// used by some other source. This is wrong because only one source can play
64    /// sound from streaming buffer.
65    StreamingBufferAlreadyInUse,
66
67    /// Decoder specific error, can occur in the decoder by any reason (invalid format,
68    /// insufficient data, etc.). Exact reason stored in inner value.
69    DecoderError(DecoderError),
70
71    /// A buffer is invalid (for example it is LoadError state)
72    BufferFailedToLoad,
73
74    /// A buffer is not loaded yet, consider to `await` it before use.
75    BufferIsNotLoaded,
76}
77
78impl From<std::io::Error> for SoundError {
79    fn from(e: std::io::Error) -> Self {
80        SoundError::Io(e)
81    }
82}
83
84impl<'a, T> From<std::sync::PoisonError<std::sync::MutexGuard<'a, T>>> for SoundError {
85    fn from(_: std::sync::PoisonError<std::sync::MutexGuard<'a, T>>) -> Self {
86        SoundError::PoisonedMutex
87    }
88}
89
90impl From<lewton::VorbisError> for SoundError {
91    fn from(ve: VorbisError) -> Self {
92        SoundError::DecoderError(DecoderError::Ogg(ve))
93    }
94}
95
96impl Display for SoundError {
97    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
98        match self {
99            SoundError::Io(io) => write!(f, "io error: {io}"),
100            SoundError::NoBackend => write!(f, "no backend implemented for current platform"),
101            SoundError::FailedToInitializeDevice(reason) => {
102                write!(f, "failed to initialize device. reason: {reason}")
103            }
104            SoundError::InvalidHeader => write!(f, "invalid header of sound file"),
105            SoundError::UnsupportedFormat => write!(f, "unsupported format of sound file"),
106            SoundError::PoisonedMutex => write!(f, "attempt to use poisoned mutex"),
107            SoundError::MathError(reason) => {
108                write!(f, "math error has occurred. reason: {reason}")
109            }
110            SoundError::StreamingBufferAlreadyInUse => {
111                write!(f, "streaming buffer in already in use")
112            }
113            SoundError::DecoderError(de) => write!(f, "internal decoder error: {de:?}"),
114            SoundError::BufferFailedToLoad => write!(f, "a buffer failed to load"),
115            SoundError::BufferIsNotLoaded => write!(f, "a buffer is not loaded yet"),
116        }
117    }
118}
119
120impl std::error::Error for SoundError {}