use std::borrow::Borrow;
use std::collections::BTreeMap;
use crate::PatternTypes;
use crate::RefOwn;
use crate::Predicate;
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, P, O>(
input: &'s str,
preds: Option<BTreeMap<P, Box<Predicate<T>>>>,
objs: Option<BTreeMap<O, T::Own>>
) -> Result<Self, PatternError<'s>>
where
P: Borrow<str> + Ord,
O: Borrow<str> + Ord,
{
Ok(Self {
consts: parse(input, preds, objs)?
})
}
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")
}
}