summary refs log blame commit diff stats
path: root/src/pattern.rs
blob: 8c57f00c9256f9eb058dc069c3b0621c56e792cc (plain) (tree)





















                                                                                                                                                      
use crate::PatternTypes;
use crate::errors::PatternError;
use crate::parser::parse;
use crate::vm::Matcher;
use crate::vm::PatternConstants;
use crate::vm::MAX_CALLS;

pub struct Pattern<T: PatternTypes> {
    constants: PatternConstants<T>,
}

impl<T: PatternTypes> Pattern<T> {
    pub fn compile(s: &str) -> Result<Self, PatternError> {
        Ok(Self {
            constants: parse(s)?
        })
    }

    pub fn attempt_match<'a, 'b>(&'a self, value: &'b T::Value) -> Matcher<'a, 'b, T> {
        Matcher::new(value, &self.constants, self.constants.protos.len() - 1, MAX_CALLS).ok().expect("datafu internal error: MAX_CALLS must not be 0")
    }
}