1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! Engine error types.

use std::{
    error::Error as StdError,
    fmt::Result as FmtResult,
    fmt::{Display, Formatter},
    result::Result as StdResult,
};

use crate::{config::ConfigError, core, renderer, state::StateError};

/// Engine result type.
pub type Result<T> = StdResult<T, Error>;

/// Common error type.
#[derive(Debug)]
pub enum Error {
    /// Application error.
    Application,
    /// StateMachine error
    StateMachine(StateError),
    /// Asset management error.
    // Asset(AssetError),
    /// Configuration error.
    Config(ConfigError),
    /// Core error.
    Core(core::Error),
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Application => "Application error!",
            Error::Config(_) => "Configuration error!",
            Error::Core(_) => "Core error!",
            Error::StateMachine(_) => "StateMachine error!",
        }
    }

    fn cause(&self) -> Option<&dyn StdError> {
        match *self {
            Error::Config(ref e) => Some(e),
            _ => None,
        }
    }
}

impl Display for Error {
    fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
        match *self {
            Error::Application => write!(fmt, "Application initialization failed!"),
            Error::Config(ref e) => write!(fmt, "Configuration loading failed: {}", e),
            Error::Core(ref e) => write!(fmt, "System creation failed: {}", e),
            Error::StateMachine(ref e) => write!(fmt, "Error in state machine: {}", e),
        }
    }
}

impl From<core::Error> for Error {
    fn from(e: core::Error) -> Self {
        Error::Core(e)
    }
}

impl From<renderer::error::Error> for Error {
    fn from(err: renderer::error::Error) -> Self {
        Error::Core(core::Error::with_chain(err, "Renderer error"))
    }
}

impl From<ConfigError> for Error {
    fn from(err: ConfigError) -> Self {
        Error::Config(err)
    }
}