use std::collections::HashMap;
use crate::{MatchTable, MatchTableType, Matcher, ProcessType, SimpleMatcher};
#[derive(Default)]
pub struct SimpleMatcherBuilder<'a> {
word_map: HashMap<ProcessType, HashMap<u32, &'a str>>,
}
impl<'a> SimpleMatcherBuilder<'a> {
pub fn new() -> Self {
Self {
word_map: HashMap::new(),
}
}
pub fn add_word(mut self, process_type: ProcessType, word_id: u32, word: &'a str) -> Self {
let bucket = self.word_map.entry(process_type).or_default();
bucket.insert(word_id, word);
self
}
pub fn build(self) -> SimpleMatcher {
SimpleMatcher::new(&self.word_map)
}
}
pub struct MatchTableBuilder<'a> {
table_id: u32,
match_table_type: MatchTableType,
word_list: Vec<&'a str>,
exemption_process_type: ProcessType,
exemption_word_list: Vec<&'a str>,
}
impl<'a> MatchTableBuilder<'a> {
pub fn new(table_id: u32, match_table_type: MatchTableType) -> Self {
Self {
table_id,
match_table_type,
word_list: Vec::new(),
exemption_process_type: ProcessType::None,
exemption_word_list: Vec::new(),
}
}
pub fn add_word(mut self, word: &'a str) -> Self {
self.word_list.push(word);
self
}
pub fn add_words(mut self, words: impl IntoIterator<Item = &'a str>) -> Self {
self.word_list.extend(words);
self
}
pub fn exemption_process_type(mut self, process_type: ProcessType) -> Self {
self.exemption_process_type = process_type;
self
}
pub fn add_exemption_word(mut self, word: &'a str) -> Self {
self.exemption_word_list.push(word);
self
}
pub fn add_exemption_words(mut self, words: impl IntoIterator<Item = &'a str>) -> Self {
self.exemption_word_list.extend(words);
self
}
pub fn build(self) -> MatchTable<'a> {
MatchTable {
table_id: self.table_id,
match_table_type: self.match_table_type,
word_list: self.word_list,
exemption_process_type: self.exemption_process_type,
exemption_word_list: self.exemption_word_list,
}
}
}
#[derive(Default)]
pub struct MatcherBuilder<'a> {
table_map: HashMap<u32, Vec<MatchTable<'a>>>,
}
impl<'a> MatcherBuilder<'a> {
pub fn new() -> Self {
Self {
table_map: HashMap::new(), }
}
pub fn add_table(mut self, match_id: u32, table: MatchTable<'a>) -> Self {
self.table_map.entry(match_id).or_default().push(table);
self
}
pub fn build(self) -> Matcher {
Matcher::new(&self.table_map)
}
}