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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! Error codes and messages for Xvc ECS
//!
//! You can use [Error] values for various types of errors.
use log::{debug, error, info, trace, warn};

use std::ffi::OsString;
use std::fmt::Debug;
use std::io;
use std::num::ParseIntError;
use thiserror::Error as ThisError;

#[allow(missing_docs)]
#[derive(ThisError, Debug)]
/// Error type for ECS.
/// We allow missing docs, the names should be self-explanatory.
pub enum Error {
    #[error("Sorry. {0} is not implemented yet")]
    Todo(&'static str),
    #[error("System Time Error: {source}")]
    SystemTimeError {
        #[from]
        source: std::time::SystemTimeError,
    },
    #[error("Cannot restore entity counter from: {path:?}")]
    CannotRestoreEntityCounter { path: OsString },
    #[error("Cannot restore store from: {path:?}")]
    CannotRestoreStoreFromDirectory { path: OsString },

    #[error("[E1002] MsgPack Serialization Error: {source}")]
    MsgPackDecodeError {
        #[from]
        source: rmp_serde::decode::Error,
    },
    #[error("[E1003] MsgPack Serialization Error: {source}")]
    MsgPackEncodeError {
        #[from]
        source: rmp_serde::encode::Error,
    },
    #[error("[E1004] Json Serialization Error: {source}")]
    JsonError {
        #[from]
        source: serde_json::Error,
    },
    #[error("I/O Error: {source}")]
    IoError {
        #[from]
        source: io::Error,
    },
    #[error("Cannot Parse Integer: {source:?}")]
    CannotParseInteger {
        #[from]
        source: ParseIntError,
    },
    #[error("Missing value for key: {key}")]
    KeyNotFound { key: String },
    #[error("Key is already in the store: {key}")]
    KeyAlreadyFound { store: String, key: String },
    #[error("Multiple keys for value found: {value}")]
    MultipleCorrespondingKeysFound { value: String },
    #[error("Cannot find a related entity: {entity}")]
    NoParentEntityFound { entity: usize },
    #[error("More than one root entity found in an 1-N relation")]
    MoreThanOneParentFound { entity: usize },
    #[error("Cannot find key in store: {key}")]
    CannotFindKeyInStore { key: usize },
    #[error("Internal Store Conversion Error")]
    StoreConversionError,
    #[error("Can initialize {object} only once")]
    CanInitializeOnlyOnce { object: String },
}

impl Error {
    /// print [DEBUG] message for [Error]
    pub fn debug(self) -> Self {
        debug!("{}", self);
        self
    }
    /// print [TRACE] message for [Error]
    pub fn trace(self) -> Self {
        trace!("{}", self);
        self
    }
    /// print [WARN] message for [Error]
    pub fn warn(self) -> Self {
        warn!("{}", self);
        self
    }
    /// print [ERROR] message for [Error]
    pub fn error(self) -> Self {
        error!("{}", self);
        self
    }
    /// print [INFO] message for [Error]
    pub fn info(self) -> Self {
        info!("{}", self);
        self
    }
    /// print [PANIC] message for [Error] and exit!
    pub fn panic(self) -> Self {
        panic!("{}", self);
    }
}

/// Result type for Xvc ECS functions that may return [Error]
pub type Result<T> = std::result::Result<T, Error>;