summary refs log tree commit diff stats
path: root/src/pattern.rs
blob: 2b99163ddef33f1190389f5dad43640524671581 (plain) (blame)
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
use crate::PatternTypes;
use crate::RefOwn;
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> {
    consts: PatternConstants<T>,
}

impl<T: PatternTypes> Pattern<T> {
    pub fn compile(s: &str, defs: Option<()>/*TODO*/) -> Result<Self, PatternError> {
        Ok(Self {
            consts: parse(s, defs)?
        })
    }

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