Skip to main content

stix_matcher/
result.rs

1//! The outcome of a match.
2
3/// The result of evaluating a pattern against a set of observations.
4#[derive(Debug, Clone, PartialEq, Eq, Default)]
5pub struct MatchResult {
6    matched: bool,
7    matched_observations: Vec<usize>,
8}
9
10impl MatchResult {
11    /// A non-match (no observations).
12    pub fn no_match() -> Self {
13        MatchResult {
14            matched: false,
15            matched_observations: Vec::new(),
16        }
17    }
18
19    /// A match, recording the indices of the observations that satisfied the pattern.
20    pub fn matched(observations: Vec<usize>) -> Self {
21        MatchResult {
22            matched: true,
23            matched_observations: observations,
24        }
25    }
26
27    /// Whether the pattern matched.
28    pub fn is_match(&self) -> bool {
29        self.matched
30    }
31
32    /// Indices (into the input observation list) that participated in the match.
33    pub fn observations(&self) -> &[usize] {
34        &self.matched_observations
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn no_match_is_false() {
44        let r = MatchResult::no_match();
45        assert!(!r.is_match());
46        assert!(r.observations().is_empty());
47    }
48
49    #[test]
50    fn matched_records_observations() {
51        let r = MatchResult::matched(vec![0, 2]);
52        assert!(r.is_match());
53        assert_eq!(r.observations(), &[0, 2]);
54    }
55}