use std::collections::HashSet;
pub struct LetterFilter {
pub allowed: HashSet<char>,
pub focused: Option<char>,
}
impl LetterFilter {
pub fn new(allowed: &[char], focused: Option<char>) -> Self {
Self {
allowed: allowed.iter().copied().collect(),
focused,
}
}
#[inline]
pub fn is_allowed(&self, c: char) -> bool {
c == ' ' || self.allowed.contains(&c)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn space_is_always_allowed() {
let filter = LetterFilter::new(&['a', 'b'], None);
assert!(filter.is_allowed(' '));
}
#[test]
fn allowed_chars_pass() {
let filter = LetterFilter::new(&['e', 't', 'a'], None);
assert!(filter.is_allowed('e'));
assert!(filter.is_allowed('t'));
assert!(!filter.is_allowed('z'));
}
#[test]
fn focused_is_stored() {
let filter = LetterFilter::new(&['e', 't'], Some('t'));
assert_eq!(filter.focused, Some('t'));
}
}