use std::marker::PhantomData;
pub trait KeywordConverter<K> {
fn try_convert_keyword(&self, _text: &str) -> Option<K> {
return None;
}
fn try_convert_prefix(&self, _prefix: char) -> Option<K> {
return None;
}
}
pub struct StringKeywordConverter {
pub keywords: Vec<String>,
pub prefixes: Vec<char>,
}
impl StringKeywordConverter {
pub fn new() -> Self {
return Self{
keywords: Vec::new(),
prefixes: Vec::new(),
}
}
pub fn add_key(&mut self, text: &str) {
self.keywords.push(text.to_string());
}
pub fn add_prefix(&mut self, pfx: char) {
self.prefixes.push(pfx);
}
}
impl KeywordConverter<String> for StringKeywordConverter {
fn try_convert_keyword(&self, text: &str) -> Option<String> {
let s = text.to_string();
if self.keywords.len() == 0 {
return Some(s);
}
if self.keywords.contains(&s) {
return Some(s);
}
return None;
}
fn try_convert_prefix(&self, pfx: char) -> Option<String> {
if self.prefixes.contains(&pfx) {
return Some(pfx.to_string());
}
return None;
}
}
pub struct NullKeywordConverter<K> {
phantom_keyword: PhantomData<K>,
}
impl<K> NullKeywordConverter<K> {
pub fn new() -> Self {
return Self {
phantom_keyword: PhantomData,
}
}
}
impl<K> KeywordConverter<K> for NullKeywordConverter<K> {
fn try_convert_keyword(&self, _text: &str) -> Option<K> {
return None;
}
fn try_convert_prefix(&self, _pfx: char) -> Option<K> {
return None;
}
}