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
extern crate alloc;

use alloc::boxed::Box;

use ross_protocol::packet::Packet;

use crate::extractor::{Extractor, ExtractorError};
use crate::filter::{Filter, FilterError};
use crate::state::StateManager;

#[derive(Debug)]
pub enum MatcherError {
    ExtractorError(ExtractorError),
    FilterError(FilterError),
}

#[derive(Debug)]
pub struct Matcher {
    pub extractor: Box<dyn Extractor>,
    pub filter: Box<dyn Filter>,
}

impl Matcher {
    pub fn do_match(&mut self, packet: &Packet, state_manager: &mut StateManager) -> Result<bool, MatcherError> {
        let value = self.extractor.extract(packet).map_err(|err| MatcherError::ExtractorError(err))?;
        let result = self.filter.filter(&value, state_manager).map_err(|err| MatcherError::FilterError(err))?;

        Ok(result)
    }
}