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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! Error and Result types for the pipelines crate
use log::{debug, error, info, trace, warn};

use std::ffi::OsString;
use std::fmt::Debug;
use std::io;
use std::num::TryFromIntError;
use std::path::PathBuf;
use std::sync::PoisonError;
use thiserror::Error as ThisError;

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

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

    #[error("Xvc ECS Error: {source}")]
    EcsError {
        #[from]
        source: xvc_ecs::error::Error,
    },

    #[error("Xvc Core Error: {source}")]
    CoreError {
        #[from]
        source: xvc_core::error::Error,
    },

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

    #[error("Walker Error: {source}")]
    WalkerError {
        #[from]
        source: xvc_walker::error::Error,
    },

    #[error("Cannot infer format from file extension: {extension:?}")]
    CannotInferFormatFromExtension { extension: OsString },
    #[error("Format specification for input (stdin) required.")]
    FormatSpecificationRequired,
    #[error("Process Error - stdout: {stdout}\nstderr: {stderr}")]
    ProcessError { stdout: String, stderr: String },
    #[error("Process Exec Error: {source}")]
    ProcessExecError {
        #[from]
        source: subprocess::PopenError,
    },
    #[error("Invalid regular expression: {regex}")]
    InvalidRegexFormat { regex: String },
    //
    #[error("Invalid lines definition: {line}")]
    InvalidLinesFormat { line: String },
    //
    #[error("Step {step} not found in pipeline")]
    StepNotFoundInPipeline { step: String },
    #[error("[E1004] Json Serialization Error: {source}")]
    JsonError {
        #[from]
        source: serde_json::Error,
    },
    #[error("Encountered NULL value in JSON map")]
    JsonNullValueForKey { key: String },
    //
    #[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("[E2001] Step with name '{step_name}' already found in {pipeline_name}")]
    StepAlreadyFoundInPipeline {
        step_name: String,
        pipeline_name: String,
    },
    #[error("[E2002] Stage with name already found")]
    StepRequiresName,
    #[error("[E2003] The command xvc {command} requires subcommand.")]
    RequiresSubCommand { command: String },
    #[error("[E2004] Requires xvc repository.")]
    RequiresXvcRepository,
    #[error("Pipeline {name} already found")]
    PipelineAlreadyFound { name: String },
    #[error("Pipeline {name} is not found")]
    NoPipelinesFound { name: String },
    #[error("Pipeline Steps Contain Cycle")]
    PipelineStepsContainCycle { pipeline: String, step: String },
    #[error("Cannot delete last pipeline")]
    CannotDeleteLastPipeline,
    #[error("Cannot delete default pipeline: {name}")]
    CannotDeleteDefaultPipeline { name: String },
    //
    #[error("Pipeline cannot depend to itself")]
    PipelineCannotDependToItself,

    #[error("Step cannot depend to itself")]
    StepCannotDependToItself,

    #[error("Internal Error: Content Digest for Pipeline Dependencies is not available. ")]
    NoContentDigestForPipelines,

    #[error("Internal Error: Content Digest for Step Dependencies is not available. ")]
    NoContentDigestForSteps,
    //
    #[error("I/O Error: {source}")]
    IoError {
        #[from]
        source: io::Error,
    },

    #[error("Unicode/UTF-8 Error: {source:?}")]
    UnicodeError {
        #[from]
        source: std::string::FromUtf8Error,
    },
    #[error("Poison Error: {cause:?}")]
    PoisonError { cause: String },
    #[error("Path not found: {path:?}")]
    PathNotFound { path: OsString },
    #[error("Path has no modification time: {path:?}")]
    PathHasNoModificationTime { path: OsString },
    #[error("Internal Error: XvcDependencyComparisonError in Pipelines")]
    XvcDependencyComparisonError,
    #[error("Missing value for key: {key}")]
    KeyNotFound { key: String },
    //
    #[error("Missing value for key: {key} in {path}")]
    KeyNotFoundInDocument { key: String, path: PathBuf },

    #[error("Pattern Error: {source}")]
    PatternError {
        #[from]
        source: glob::PatternError,
    },

    #[error("URL Request Error: {source}")]
    UrlRequestError {
        #[from]
        source: reqwest::Error,
    },

    #[error("Invalid Parameter Format: {param} ")]
    InvalidParameterFormat { param: String },

    #[error("Unsupported param file format: {path:?} ")]
    UnsupportedParamFileFormat { path: OsString },

    #[error("Crossbeam Send Error for Type: {t:?} {cause:?}")]
    CrossbeamSendError { t: String, cause: String },
    #[error("Crossbeam Recv Error: {source}")]
    CrossbeamRecvError {
        #[from]
        source: crossbeam_channel::RecvError,
    },

    #[error("Cannot find Pipeline: {name}")]
    CannotFindPipeline { name: String },

    #[error("Cannot parse url: {source}")]
    CannotParseUrl {
        #[from]
        source: url::ParseError,
    },

    #[error("Try Receive Error: {source}")]
    TryReceiveError {
        #[from]
        source: crossbeam_channel::TryRecvError,
    },

    #[error("Cannot cast from: {source}")]
    TryFromIntError {
        #[from]
        source: TryFromIntError,
    },
}

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<T: Debug> From<PoisonError<T>> for Error {
    fn from(e: PoisonError<T>) -> Self {
        Error::PoisonError {
            cause: e.to_string(),
        }
    }
}

impl<T: Debug> From<&PoisonError<T>> for Error {
    fn from(e: &PoisonError<T>) -> Self {
        Error::PoisonError {
            cause: e.to_string(),
        }
    }
}
impl Error {
    /// Log the error and return it
    pub fn debug(self) -> Self {
        debug!("{}", self);
        self
    }
    /// Log the error and return it
    pub fn trace(self) -> Self {
        trace!("{}", self);
        self
    }
    /// Log the error and return it
    pub fn warn(self) -> Self {
        warn!("{}", self);
        self
    }
    /// Log the error and return it
    pub fn error(self) -> Self {
        error!("{}", self);
        self
    }
    /// Log the error and return it
    pub fn info(self) -> Self {
        info!("{}", self);
        self
    }
    /// Panic with the error
    pub fn panic(self) -> Self {
        panic!("{}", self);
    }
}

/// The result type for xvc pipeline crate
pub type Result<T> = std::result::Result<T, Error>;