pub mod charmap;
pub mod timestamp;
use std::collections::HashMap;
use crate::keybind::config::SettingValue;
pub use charmap::CharmapFilter;
pub use timestamp::TimestampFilter;
pub trait IoFilter {
fn enabled(&self) -> bool;
fn toggle(&mut self);
fn filter_out(&mut self, buf: &[u8]) -> Vec<u8> {
buf.to_vec()
}
fn filter_in(&mut self, buf: &[u8]) -> Vec<u8> {
buf.to_vec()
}
}
pub struct FilterChain {
timestamp_filter: TimestampFilter,
charmap_filter: CharmapFilter,
}
impl FilterChain {
pub fn new(settings: &HashMap<String, SettingValue>) -> Self {
let mut timestamp_filter = TimestampFilter::new();
timestamp_filter.configure(settings);
let mut charmap_filter = CharmapFilter::new();
charmap_filter.configure(settings);
FilterChain {
timestamp_filter,
charmap_filter,
}
}
pub fn toggle(&mut self, name: &str) -> bool {
match name {
timestamp::NAME => {
self.timestamp_filter.toggle();
true
}
charmap::NAME => {
self.charmap_filter.toggle();
true
}
_ => false,
}
}
pub fn filter_out(&mut self, buf: &[u8]) -> Vec<u8> {
let mut output = buf.to_vec();
if self.timestamp_filter.enabled() {
output = self.timestamp_filter.filter_out(&output);
}
if self.charmap_filter.enabled() {
output = self.charmap_filter.filter_out(&output);
}
output
}
pub fn filter_in(&mut self, buf: &[u8]) -> Vec<u8> {
let mut output = buf.to_vec();
if self.charmap_filter.enabled() {
output = self.charmap_filter.filter_in(&output);
}
output
}
}
impl Default for FilterChain {
fn default() -> Self {
Self::new(&HashMap::new())
}
}