goliath_rule/error.rs
1//! Error types.
2
3use goliath_sigma::YamlError;
4use thiserror::Error;
5
6use crate::mapping::LogSourceSelector;
7
8/// A field path that cannot be parsed.
9#[non_exhaustive]
10#[derive(Debug, Clone, PartialEq, Eq, Error)]
11pub enum PathError {
12 /// The path is empty.
13 #[error("field path is empty")]
14 Empty,
15
16 /// A segment between dots is empty.
17 #[error("field path `{path}` has an empty segment at position {position}")]
18 EmptySegment {
19 /// The path as written.
20 path: String,
21 /// The zero-based index of the empty segment.
22 position: usize,
23 },
24}
25
26/// Text that is not an IP address or network.
27#[derive(Debug, Clone, PartialEq, Eq, Error)]
28#[error("`{0}` is not an IP address or network")]
29pub struct NetworkError(pub String);
30
31/// A mapping set that cannot be loaded, or cannot resolve a rule's log source.
32#[derive(Debug, Clone, PartialEq, Eq, Error)]
33#[non_exhaustive]
34pub enum MappingError {
35 /// The file is not acceptable YAML, or does not have the shape of a
36 /// mapping set.
37 #[error(transparent)]
38 Yaml(#[from] YamlError),
39
40 /// An entry's selector names no key, so it would apply to every rule.
41 #[error("mapping {index} selects every log source; name at least one key")]
42 EmptySelector {
43 /// The zero-based index of the entry.
44 index: usize,
45 },
46
47 /// A field is mapped to an empty list of paths.
48 #[error("mapping {index} maps field `{field}` to no path")]
49 NoPaths {
50 /// The zero-based index of the entry.
51 index: usize,
52 /// The field name.
53 field: String,
54 },
55
56 /// Two entries have the same selector, so neither can be chosen.
57 #[error("mappings {earlier} and {index} have the same log source selector")]
58 DuplicateSelector {
59 /// The zero-based index of the first entry.
60 earlier: usize,
61 /// The zero-based index of the repeated entry.
62 index: usize,
63 },
64
65 /// No entry applies to a rule's log source.
66 #[error("no mapping for log source ({logsource})")]
67 NoMapping {
68 /// The log source the rule declared.
69 logsource: LogSourceSelector,
70 },
71
72 /// Two equally specific entries apply to a rule's log source.
73 #[error("more than one mapping applies equally to log source ({logsource})")]
74 Ambiguous {
75 /// The log source the rule declared.
76 logsource: LogSourceSelector,
77 },
78}
79
80/// A rule that cannot be resolved exactly.
81#[derive(Debug, Clone, PartialEq, Eq, Error)]
82#[non_exhaustive]
83pub enum ResolveError {
84 /// No mapping applies to the rule's log source.
85 #[error(transparent)]
86 Mapping(#[from] MappingError),
87
88 /// The mapping for the rule's log source does not map a field it uses.
89 ///
90 /// Refused rather than treated as absent: the rule would load and never
91 /// fire, and the gap would be invisible.
92 #[error("field `{field}` has no mapping for log source ({logsource})")]
93 UnmappedField {
94 /// The field name as the rule wrote it.
95 field: String,
96 /// The rule's log source.
97 logsource: LogSourceSelector,
98 },
99
100 /// A modifier whose meaning depends on configuration this resolution
101 /// does not have.
102 #[error("field `{field}` uses `{modifier}`, which is not supported")]
103 UnsupportedModifier {
104 /// The field name.
105 field: String,
106 /// The modifier.
107 modifier: &'static str,
108 },
109
110 /// A value that cannot mean anything under its modifiers.
111 #[error("field `{field}`: {reason}")]
112 InvalidValue {
113 /// The field name.
114 field: String,
115 /// Why.
116 reason: &'static str,
117 },
118
119 /// A value expands into more variants than a rule may carry.
120 #[error("field `{field}` expands into {count} variants")]
121 TooManyVariants {
122 /// The field name.
123 field: String,
124 /// How many variants it would produce.
125 count: usize,
126 },
127
128 /// A keyword that is not text.
129 #[error("a keyword must be text, found {found}")]
130 UnsupportedKeyword {
131 /// What was found.
132 found: &'static str,
133 },
134}