pub struct OwnedFilter(Vec<String>);
impl OwnedFilter {
pub fn new(list: Vec<String>) -> Self {
Self(list)
}
pub fn empty() -> Self {
Self(Vec::new())
}
pub fn add_word(&mut self, word: String) {
self.0.push(word);
}
pub fn add_vec(&mut self, vec: Vec<String>) {
self.0.extend(vec);
}
pub fn add_slice<T: AsRef<str>>(&mut self, slice: &[T]) {
self.0.extend(slice.iter().map(|s| s.as_ref().to_string()));
}
pub fn word_list(&self) -> &[String] {
&self.0
}
pub fn remove_word(&mut self, word: &str) {
self.0.retain(|s| s != word);
}
pub fn filter_string(&self, s: &str) -> Option<&str> {
crate::filter::filter_string(s, &self.0)
}
}
impl AsRef<[String]> for OwnedFilter {
fn as_ref(&self) -> &[String] {
self.word_list()
}
}
impl Default for OwnedFilter {
fn default() -> Self {
Self::empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_owned_filter() {
let mut filter = OwnedFilter::empty();
filter.add_word("hello".to_string());
filter.add_vec(vec![
"world".to_string(),
"this".to_string(),
"is".to_string(),
]);
filter.add_slice(&["a", "test"]);
assert_eq!(
filter.word_list(),
&["hello", "world", "this", "is", "a", "test"]
);
}
}