Skip to main content

rsigma_eval/
error.rs

1//! Evaluation-specific error types.
2
3use thiserror::Error;
4
5/// Errors that can occur during rule compilation or evaluation.
6#[derive(Debug, Error)]
7#[non_exhaustive]
8pub enum EvalError {
9    /// A regex pattern failed to compile.
10    #[error("invalid regex pattern: {0}")]
11    InvalidRegex(#[from] regex::Error),
12
13    /// A CIDR pattern failed to parse.
14    #[error("invalid CIDR: {0}")]
15    InvalidCidr(#[from] ipnet::AddrParseError),
16
17    /// A base64 operation failed.
18    #[error("base64 encoding error: {0}")]
19    Base64(String),
20
21    /// A detection referenced in a condition was not found.
22    #[error("unknown detection identifier: {0}")]
23    UnknownDetection(String),
24
25    /// A modifier combination is invalid.
26    #[error("invalid modifier combination: {0}")]
27    InvalidModifiers(String),
28
29    /// A value type is incompatible with the modifier.
30    #[error("incompatible value for modifier: {0}")]
31    IncompatibleValue(String),
32
33    /// A numeric value was expected but not found.
34    #[error("expected numeric value: {0}")]
35    ExpectedNumeric(String),
36
37    /// A processing pipeline could not be parsed, or failed while it was
38    /// applied to a rule (including a deliberate `rule_failure` or
39    /// `detection_item_failure` transformation).
40    #[error("pipeline error: {0}")]
41    Pipeline(String),
42
43    /// A parser error propagated during compilation.
44    #[error("parser error: {0}")]
45    Parser(#[from] rsigma_parser::SigmaParserError),
46
47    /// A correlation rule compilation or evaluation error.
48    #[error("correlation error: {0}")]
49    CorrelationError(String),
50
51    /// A rule referenced by a correlation was not found.
52    #[error("unknown rule reference: {0}")]
53    UnknownRuleRef(String),
54
55    /// A cycle was detected in correlation rule references.
56    #[error("correlation cycle detected: {0}")]
57    CorrelationCycle(String),
58
59    /// An error from the IR lowering pipeline that has no more specific mapping.
60    #[error("IR lowering error: {0}")]
61    Ir(String),
62
63    /// A HIR cache (de)serialization error.
64    #[error("HIR cache error: {0}")]
65    HirCache(String),
66}
67
68impl From<rsigma_ir::CacheError> for EvalError {
69    fn from(err: rsigma_ir::CacheError) -> Self {
70        EvalError::HirCache(err.to_string())
71    }
72}
73
74impl From<rsigma_ir::IrError> for EvalError {
75    fn from(err: rsigma_ir::IrError) -> Self {
76        use rsigma_ir::IrError;
77        match err {
78            IrError::UnknownDetection(name) => EvalError::UnknownDetection(name),
79            IrError::InvalidModifiers(msg) => EvalError::InvalidModifiers(msg),
80            IrError::IncompatibleValue(msg) => EvalError::IncompatibleValue(msg),
81            IrError::ExpectedNumeric(msg) => EvalError::ExpectedNumeric(msg),
82            IrError::Parser(e) => EvalError::Parser(e),
83            other => EvalError::Ir(other.to_string()),
84        }
85    }
86}
87
88/// Convenience result type.
89pub type Result<T> = std::result::Result<T, EvalError>;