rnetmon/src/filter.rs

77 lines
2.2 KiB
Rust

use globset::{Glob, GlobMatcher};
pub use std::collections::HashMap;
use crate::message::*;
#[derive(Debug)]
pub struct Filter {
level: Level,
include_types: Vec<GlobMatcher>,
exclude_types: Vec<GlobMatcher>,
}
impl Filter {
pub fn new(config: &serde_yaml::Value) -> Self {
let include_types = match config.get("include_types") {
Some(v) => v
.as_sequence()
.expect("include_types must be a list of strings")
.into_iter()
.map(|x| {
Glob::new(x.as_str().expect("filter must be a list of strings"))
.expect("Invalid glob pattern")
.compile_matcher()
})
.collect(),
None => vec![],
};
let exclude_types = match config.get("exclude_types") {
Some(v) => v
.as_sequence()
.expect("exclude_types must be a list of strings")
.into_iter()
.map(|x| {
Glob::new(x.as_str().expect("filter must be a list of strings"))
.expect("Invalid glob pattern")
.compile_matcher()
})
.collect(),
None => vec![],
};
let level = match config.get("level") {
Some(l) => Level::try_from(l.as_str().expect("Level must be a string"))
.expect("Unknown level name"),
None => Level::Notice,
};
Filter {
level: level,
include_types: include_types,
exclude_types: exclude_types,
}
}
pub fn is_message_allowed(&self, message: &Message) -> bool {
if message.level < self.level {
return false;
}
for exc in &self.exclude_types {
if exc.is_match(&message.msg_type) {
return false;
}
}
if self.include_types.len() > 0 {
for exc in &self.include_types {
if exc.is_match(&message.msg_type) {
return true;
}
}
return false;
} else {
return true;
}
}
}