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    /// A network server could not be started.
17    Net(String),
18}
19
20/// Shorthand for `std::result::Result<T, audio_code::Error>`.
21pub type Result<T> = std::result::Result<T, Error>;
22
23impl fmt::Display for Error {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Error::Io(e) => write!(f, "I/O error: {e}"),
27            Error::Decode(e) => write!(f, "could not decode sound: {e}"),
28            Error::Audio(e) => write!(f, "audio device error: {e}"),
29            Error::Window(e) => write!(f, "window error: {e}"),
30            Error::Net(e) => write!(f, "network error: {e}"),
31        }
32    }
33}
34
35impl std::error::Error for Error {
36    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
37        match self {
38            Error::Io(e) => Some(e),
39            _ => None,
40        }
41    }
42}
43
44impl From<std::io::Error> for Error {
45    fn from(e: std::io::Error) -> Self {
46        Error::Io(e)
47    }
48}