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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Error messages for Xvc core crate
use log::{debug, error, info, trace, warn};
use xvc_config::error::Error as XvcConfigError;
use xvc_ecs::error::Error as XvcEcsError;
use xvc_walker::error::Error as XvcWalkerError;

use sled::Error as SledError;
use std::ffi::OsString;
use std::fmt::Debug;
use std::io;
use std::path::PathBuf;
use thiserror::Error as ThisError;

#[allow(missing_docs)]
#[derive(ThisError, Debug)]
pub enum Error {
    #[error("Sorry. {0} is not implemented yet")]
    Todo(&'static str),

    #[error("General Xvc Core Error: {msg}")]
    GeneralError { msg: String },

    #[error("General Xvc Core Error: {source}")]
    AnyhowError {
        #[from]
        source: anyhow::Error,
    },

    #[error("ECS Error: {source}")]
    EcsError {
        #[from]
        source: XvcEcsError,
    },
    #[error("Walker Error: {source}")]
    WalkerError {
        #[from]
        source: XvcWalkerError,
    },

    #[error("Config Error: {source}")]
    ConfigError {
        #[from]
        source: XvcConfigError,
    },

    #[error("File System Walk Error: {error}")]
    FSWalkerError { error: String },
    #[error("Cannot find XVC Root: {path}")]
    CannotFindXvcRoot { path: PathBuf },
    #[error("Cannot nest XVC repositories: {path}")]
    CannotNestXvcRepositories { path: PathBuf },
    #[error("Regex Error: {source}")]
    RegexError {
        #[from]
        source: regex::Error,
    },
    #[error("System Time Error: {source}")]
    SystemTimeError {
        #[from]
        source: std::time::SystemTimeError,
    },
    #[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("TOML Serialization Error: {source}")]
    TomlSerializationError {
        #[from]
        source: toml::ser::Error,
    },

    #[error("TOML Deserialization Error: {source}")]
    TomlDeserializationError {
        #[from]
        source: toml::de::Error,
    },

    #[error("Yaml Error: {source}")]
    YamlError {
        #[from]
        source: serde_yaml::Error,
    },
    #[error("Encountered NULL value in YAML map")]
    YamlNullValueForKey { key: String },
    #[error("I/O Error: {source}")]
    IoError {
        #[from]
        source: io::Error,
    },
    #[error("Unicode/UTF-8 Error: {cause:?}")]
    UnicodeError { cause: OsString },
    #[error("Glob Error: {source}")]
    GlobError {
        #[from]
        source: glob::GlobError,
    },
    //
    #[error("Glob Pattern Error: {source}")]
    GlobPatternError {
        #[from]
        source: glob::PatternError,
    },
    #[error("Path strip prefix error: {source}")]
    StringPrefixError {
        #[from]
        source: std::path::StripPrefixError,
    },
    #[error("Crossbeam Send Error for Type: {t:?} {cause:?}")]
    CrossbeamSendError { t: String, cause: String },
    #[error("Relative Path Conversion Error: {source}")]
    RelativePathError {
        #[from]
        source: relative_path::FromPathError,
    },
    #[error("Error in Key/Value Store: {source}")]
    SledError {
        #[from]
        source: SledError,
    },
    #[error("Glob error: {source}")]
    GlobSetError {
        #[from]
        source: xvc_walker::globset::Error,
    },

    #[error("Cannot find parent path")]
    CannotFindParentPath { path: PathBuf },
}

impl<T> From<crossbeam_channel::SendError<T>> for Error
where
    T: Debug,
{
    fn from(e: crossbeam_channel::SendError<T>) -> Self {
        Error::CrossbeamSendError {
            t: format!("{:#?}", e.0),
            cause: e.to_string(),
        }
    }
}

impl From<Box<dyn std::any::Any + Send>> for Error {
    fn from(e: Box<dyn std::any::Any + Send>) -> Self {
        Error::GeneralError {
            msg: format!("{:?}", e),
        }
    }
}

impl Error {
    /// log error to debug
    pub fn debug(self) -> Self {
        debug!("{}", self);
        self
    }
    /// log error to trace
    pub fn trace(self) -> Self {
        trace!("{}", self);
        self
    }
    /// log error to warning
    pub fn warn(self) -> Self {
        warn!("{}", self);
        self
    }
    /// log error to error
    pub fn error(self) -> Self {
        error!("{}", self);
        self
    }
    /// log error to info
    pub fn info(self) -> Self {
        info!("{}", self);
        self
    }
    /// panic with this error
    pub fn panic(self) -> Self {
        panic!("{}", self);
    }
}

/// The result type that may return a xvc::core::Error
pub type Result<T> = std::result::Result<T, Error>;