xvc_ecs/
error.rs

1//! Error codes and messages for Xvc ECS
2//!
3//! You can use [Error] values for various types of errors.
4use log::{debug, error, info, trace, warn};
5
6use std::ffi::OsString;
7use std::fmt::Debug;
8use std::io;
9use std::num::ParseIntError;
10use thiserror::Error as ThisError;
11
12use crate::XvcEntity;
13
14#[allow(missing_docs)]
15#[derive(ThisError, Debug)]
16/// Error type for ECS.
17/// We allow missing docs, the names should be self-explanatory.
18pub enum Error {
19    #[error("Sorry. {0} is not implemented yet")]
20    Todo(&'static str),
21    #[error("System Time Error: {source}")]
22    SystemTimeError {
23        #[from]
24        source: std::time::SystemTimeError,
25    },
26    #[error("Cannot restore entity counter from: {path:?}")]
27    CannotRestoreEntityCounter { path: OsString },
28    #[error("Cannot restore store from: {path:?}")]
29    CannotRestoreStoreFromDirectory { path: OsString },
30
31    #[error("[E1002] MsgPack Serialization Error: {source}")]
32    MsgPackDecodeError {
33        #[from]
34        source: rmp_serde::decode::Error,
35    },
36    #[error("[E1003] MsgPack Serialization Error: {source}")]
37    MsgPackEncodeError {
38        #[from]
39        source: rmp_serde::encode::Error,
40    },
41    #[error("[E1004] Json Serialization Error: {source}")]
42    JsonError {
43        #[from]
44        source: serde_json::Error,
45    },
46    #[error("I/O Error: {source}")]
47    IoError {
48        #[from]
49        source: io::Error,
50    },
51    #[error("Cannot Parse Integer: {source:?}")]
52    CannotParseInteger {
53        #[from]
54        source: ParseIntError,
55    },
56    #[error("Missing value for key: {key}")]
57    KeyNotFound { key: String },
58    #[error("Key is already in the store: {key}")]
59    KeyAlreadyFound { store: String, key: String },
60    #[error("Multiple keys for value found: {value}")]
61    MultipleCorrespondingKeysFound { value: String },
62    #[error("Cannot find a related entity: {entity}")]
63    NoParentEntityFound { entity: XvcEntity },
64    #[error("More than one root entity found in an 1-N relation")]
65    MoreThanOneParentFound { entity: usize },
66    #[error("Cannot find key in store: {key}")]
67    CannotFindKeyInStore { key: String },
68    #[error("Internal Store Conversion Error")]
69    StoreConversionError,
70    #[error("Can initialize {object} only once")]
71    CanInitializeOnlyOnce { object: String },
72    #[error("Cannot find entity: {entity}")]
73    CannotFindEntityInStore { entity: XvcEntity },
74}
75
76impl Error {
77    /// print [DEBUG] message for [Error]
78    pub fn debug(self) -> Self {
79        debug!("{}", self);
80        self
81    }
82    /// print [TRACE] message for [Error]
83    pub fn trace(self) -> Self {
84        trace!("{}", self);
85        self
86    }
87    /// print [WARN] message for [Error]
88    pub fn warn(self) -> Self {
89        warn!("{}", self);
90        self
91    }
92    /// print [ERROR] message for [Error]
93    pub fn error(self) -> Self {
94        error!("{}", self);
95        self
96    }
97    /// print [INFO] message for [Error]
98    pub fn info(self) -> Self {
99        info!("{}", self);
100        self
101    }
102    /// print [PANIC] message for [Error] and exit!
103    pub fn panic(self) -> Self {
104        panic!("{}", self);
105    }
106}
107
108/// Result type for Xvc ECS functions that may return [Error]
109pub type Result<T> = std::result::Result<T, Error>;