rusty_rules/
error.rs

1/// Represents possible errors that can occur during rules parsing.
2#[derive(thiserror::Error, Debug)]
3pub enum Error {
4    /// Covers general JSON parsing issues, such as missing fields or incorrect types.
5    #[error("{0}")]
6    Json(String),
7
8    /// Triggered when a fetcher specified in the JSON rule is invalid.
9    #[error("error in '{name}' fetcher: {error}")]
10    Fetcher { name: String, error: String },
11
12    /// Triggered when an error in the matcher occurs when parsing it.
13    #[error("error in '{fetcher}' matcher: {error}")]
14    Matcher { fetcher: String, error: String },
15
16    /// Triggered when an operator specified in the JSON rule isn’t registered in the engine.
17    #[error("unknown operator '{0}'")]
18    UnknownOperator(String),
19
20    /// Triggered when an error in the operator occurs when parsing it.
21    #[error("error in '{name}' operator: {error}")]
22    Operator { name: String, error: String },
23
24    #[error(transparent)]
25    Regex(#[from] regex::Error),
26
27    #[error(transparent)]
28    IpAddress(#[from] std::net::AddrParseError),
29
30    #[error(transparent)]
31    IpSubnet(#[from] ipnet::AddrParseError),
32}
33
34impl Error {
35    pub(crate) fn json(error: impl ToString) -> Self {
36        Error::Json(error.to_string())
37    }
38
39    pub(crate) fn fetcher(name: &str, error: impl ToString) -> Self {
40        Error::Fetcher {
41            name: name.to_string(),
42            error: error.to_string(),
43        }
44    }
45
46    pub(crate) fn matcher(fetcher: &str, error: impl ToString) -> Self {
47        Error::Matcher {
48            fetcher: fetcher.to_string(),
49            error: error.to_string(),
50        }
51    }
52
53    pub(crate) fn operator(name: &str, error: impl ToString) -> Self {
54        Error::Operator {
55            name: name.to_string(),
56            error: error.to_string(),
57        }
58    }
59}