Skip to main content

audio_code/
error.rs

1//! Error type used throughout the engine.
2
3use std::fmt;
4
5/// Everything that can go wrong inside the engine.
6#[derive(Debug)]
7pub enum Error {
8    /// A file could not be read.
9    Io(std::io::Error),
10    /// A sound file could not be decoded.
11    Decode(String),
12    /// The audio device could not be opened.
13    Audio(String),
14    /// The window or event loop could not be created.
15    Window(String),
16}
17
18/// Shorthand for `std::result::Result<T, audio_code::Error>`.
19pub type Result<T> = std::result::Result<T, Error>;
20
21impl fmt::Display for Error {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Error::Io(e) => write!(f, "I/O error: {e}"),
25            Error::Decode(e) => write!(f, "could not decode sound: {e}"),
26            Error::Audio(e) => write!(f, "audio device error: {e}"),
27            Error::Window(e) => write!(f, "window error: {e}"),
28        }
29    }
30}
31
32impl std::error::Error for Error {
33    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
34        match self {
35            Error::Io(e) => Some(e),
36            _ => None,
37        }
38    }
39}
40
41impl From<std::io::Error> for Error {
42    fn from(e: std::io::Error) -> Self {
43        Error::Io(e)
44    }
45}