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
use std::error::Error;
use std::fmt::{self, Display};

/// The error type used by the game engine.
#[derive(Debug)]
pub enum EngineError {
    Animation(String),
    ResourceFormat(String),
    ResourceIndex(String),
    ResourceMissing(String),
    ResourceType(String),
    MathError(String),
    Network(String),
    Misc(String),
    FileError(String),
}

impl Display for EngineError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            EngineError::Animation(e) => write!(f, "AnimationError: {}", e),
            EngineError::ResourceFormat(e) => write!(f, "ResourceError: {}", e),
            EngineError::ResourceIndex(e) => write!(f, "ResourceError: {}", e),
            EngineError::ResourceMissing(e) => write!(f, "ResourceError: {}", e),
            EngineError::ResourceType(e) => write!(f, "ResourceError: {}", e),
            EngineError::Misc(e) => write!(f, "EngineError: {}", e),
            EngineError::Network(e) => write!(f, "NetworkError: {}", e),
            EngineError::MathError(e) => write!(f, "MathError: {}", e),
            EngineError::FileError(e) => write!(f, "FileError: {}", e),
        }
    }
}

impl Error for EngineError {}

macro_rules! derive_from {
    ($type:ty, $kind:ident) => {
        impl From<$type> for EngineError {
            fn from(error: $type) -> Self {
                EngineError::$kind(format!("{}", error))
            }
        }
    };
}

derive_from!(&str, Misc);
derive_from!(String, Misc);
derive_from!(image::error::ImageError, ResourceType);
derive_from!(spine::skeleton::error::SkeletonError, ResourceFormat);
derive_from!(spine::atlas::AtlasError, ResourceFormat);
derive_from!(std::io::Error, FileError);