xee_interpreter/pattern/
mode.rs1use ahash::{HashMap, HashMapExt};
2
3use xee_xpath_ast::Pattern;
4
5use crate::function;
6
7use super::pattern_lookup::PatternLookup;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub struct ModeId(usize);
11
12impl ModeId {
13 pub fn new(id: usize) -> Self {
14 ModeId(id)
15 }
16
17 pub fn get(&self) -> usize {
18 self.0
19 }
20}
21
22#[derive(Debug, Default)]
23pub struct ModeLookup<V: Clone> {
24 pub(crate) modes: HashMap<ModeId, PatternLookup<V>>,
25}
26
27impl<V: Clone> ModeLookup<V> {
28 pub(crate) fn new() -> Self {
29 Self {
30 modes: HashMap::new(),
31 }
32 }
33
34 pub(crate) fn lookup(
35 &self,
36 mode: ModeId,
37 mut matches: impl FnMut(&Pattern<function::InlineFunctionId>) -> bool,
38 ) -> Option<&V> {
39 let pattern_lookup = self.modes.get(&mode)?;
40 pattern_lookup.lookup(&mut matches)
41 }
42
43 pub fn add_rules(
44 &mut self,
45 mode: ModeId,
46 rules: Vec<(Pattern<function::InlineFunctionId>, V)>,
47 ) {
48 let pattern_lookup = self.modes.entry(mode).or_insert_with(PatternLookup::new);
49
50 pattern_lookup.add_rules(rules);
51 }
52}