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
use std::convert::Infallible;
use std::fmt::Display;
use std::fmt::Formatter;
use crate::rules::errors::ErrorKind::IncompatibleError;
use crate::rules::parser::{ParserError, Span};
#[derive(Debug)]
pub struct Error(pub ErrorKind);
impl Error {
pub fn new(kind: ErrorKind) -> Error {
Error(kind)
}
}
fn error_kind_msg(kind: &ErrorKind) -> String {
match kind {
ErrorKind::JsonError(err) => {
format!("Error parsing incoming JSON context {}", err)
}
ErrorKind::YamlError(err) => {
format!("Error parsing incoming YAML context {}", err)
}
ErrorKind::FormatError(fmt) => {
format!("Formatting error when writing {}", fmt)
}
ErrorKind::IoError(io) => {
format!("I/O error when reading {}", io)
}
ErrorKind::ParseError(err) => {
format!("Parser Error when parsing {}", err)
}
ErrorKind::RegexError(err) => {
format!("Regex expression parse error for rules file {}", err)
}
ErrorKind::MissingProperty(err) => {
format!("Could not evaluate clause for a rule with missing property for incoming context {}", err)
}
ErrorKind::MissingVariable(err) => {
format!(
"Variable assignment could not be resolved in rule file or incoming context {}",
err
)
}
ErrorKind::MultipleValues(err) => {
format!(
"Conflicting rule or variable assignments inside the same scope {}",
err
)
}
ErrorKind::IncompatibleRetrievalError(err) => {
format!(
"Types or variable assignments have incompatible types to retrieve {}",
err
)
}
IncompatibleError(err) => {
format!("Types or variable assignments are incompatible {}", err)
}
ErrorKind::NotComparable(err) => {
format!(
"Comparing incoming context with literals or dynamic results wasn't possible {}",
err
)
}
ErrorKind::ConversionError(_ignore) => "Could not convert in JSON value object".to_string(),
ErrorKind::Errors(all) => {
let vec = all.iter().map(error_kind_msg).collect::<Vec<String>>();
format!("{:?}", &vec)
}
ErrorKind::RetrievalError(err) => {
format!(
"Could not retrieve data from incoming context. Error = {}",
err
)
}
ErrorKind::MissingValue(err) => {
format!(
"There was no variable or value object to resolve. Error = {}",
err
)
}
ErrorKind::FileNotFoundError(path) => {
format!("The path {} does not exist", path)
}
}
}
fn error_kind_fmt(kind: &ErrorKind, f: &mut Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}", error_kind_msg(kind))
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
error_kind_fmt(&self.0, f)
}
}
#[derive(Debug)]
pub enum ErrorKind {
JsonError(serde_json::Error),
YamlError(serde_yaml::Error),
FormatError(std::fmt::Error),
IoError(std::io::Error),
ParseError(String),
RegexError(regex::Error),
MissingProperty(String),
MissingValue(String),
RetrievalError(String),
MissingVariable(String),
MultipleValues(String),
IncompatibleRetrievalError(String),
IncompatibleError(String),
NotComparable(String),
ConversionError(Infallible),
Errors(Vec<ErrorKind>),
FileNotFoundError(String),
}
impl From<std::fmt::Error> for Error {
fn from(e: std::fmt::Error) -> Self {
Error::new(ErrorKind::FormatError(e))
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error(ErrorKind::JsonError(err))
}
}
impl From<serde_yaml::Error> for Error {
fn from(err: serde_yaml::Error) -> Self {
Error(ErrorKind::YamlError(err))
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::new(ErrorKind::IoError(err))
}
}
impl From<regex::Error> for Error {
fn from(err: regex::Error) -> Self {
Error(ErrorKind::RegexError(err))
}
}
impl From<Infallible> for Error {
fn from(err: Infallible) -> Self {
Error(ErrorKind::ConversionError(err))
}
}
impl<'a> From<nom::Err<(Span<'a>, nom::error::ErrorKind)>> for Error {
fn from(err: nom::Err<(Span<'a>, nom::error::ErrorKind)>) -> Self {
let msg = match err {
nom::Err::Incomplete(_) => "More bytes required for parsing".to_string(),
nom::Err::Failure((s, _k)) | nom::Err::Error((s, _k)) => {
let span = s as Span;
format!(
"Error parsing file {} at line {} at column {}, remaining {}",
span.extra,
span.location_line(),
span.get_utf8_column(),
*span.fragment()
)
}
};
Error(ErrorKind::ParseError(msg))
}
}
impl<'a> From<nom::Err<ParserError<'a>>> for Error {
fn from(err: nom::Err<ParserError<'a>>) -> Self {
let msg = match err {
nom::Err::Failure(e) | nom::Err::Error(e) => format!("Parsing Error {}", e),
nom::Err::Incomplete(_) => "More bytes required for parsing".to_string(),
};
Error(ErrorKind::ParseError(msg))
}
}
impl serde::ser::Error for Error {
fn custom<T>(msg: T) -> Self
where
T: Display,
{
Error::new(IncompatibleError(msg.to_string()))
}
}
impl serde::ser::StdError for Error {}