Clarify blocking terms
This commit is contained in:
parent
4e22338181
commit
51db6ea06e
30
src/main.rs
30
src/main.rs
|
@ -7,7 +7,7 @@ use glob::glob;
|
|||
use manip::{Limit, LimitList, MergedLimitList};
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
env_logger::init(); // TODO add more logging
|
||||
env_logger::init(); // TODO add more logging & actual error handling
|
||||
|
||||
// TODO utilize CLI
|
||||
// let _cli = cli::Cli::parse();
|
||||
|
@ -16,35 +16,35 @@ fn main() -> std::io::Result<()> {
|
|||
|
||||
let mut merged_list = MergedLimitList::new();
|
||||
|
||||
// Crawl /blocks for block lists -> glob pattern: "blocks/**/*.txt"
|
||||
// Crawl /suspend for suspend lists -> glob pattern: "suspend/**/*.txt"
|
||||
{
|
||||
let mut blocklists = vec![];
|
||||
let block_paths = glob("blocks/**/*.txt").expect("Bad glob pattern");
|
||||
let mut suspend_lists = vec![];
|
||||
let suspend_paths = glob("suspend/**/*.txt").expect("Bad glob pattern");
|
||||
|
||||
for path in block_paths.filter_map(|x| x.ok()) {
|
||||
blocklists.extend(LimitList::from_file(path));
|
||||
for path in suspend_paths.filter_map(|x| x.ok()) {
|
||||
suspend_lists.extend(LimitList::from_file(path));
|
||||
// TODO Check if path contains trust value as part of filename when building limitlist
|
||||
}
|
||||
|
||||
for list in blocklists {
|
||||
merged_list.add_limit_list(list, Limit::Block);
|
||||
for list in suspend_lists {
|
||||
merged_list.add_limit_list(list, Limit::Suspend);
|
||||
}
|
||||
}
|
||||
|
||||
// Crawl /silences for silence lists -> glob pattern: "mutes/**/*.txt"
|
||||
// Crawl /silence for silence lists -> glob pattern: "silence/**/*.txt"
|
||||
{
|
||||
let mut mutelists = vec![];
|
||||
let mute_paths = glob("mutes/**/*.txt").expect("Bad glob pattern");
|
||||
let mut silence_lists = vec![];
|
||||
let silence_paths = glob("silence/**/*.txt").expect("Bad glob pattern");
|
||||
|
||||
for path in mute_paths.filter_map(|x| x.ok()) {
|
||||
mutelists.extend(LimitList::from_file(path));
|
||||
for path in silence_paths.filter_map(|x| x.ok()) {
|
||||
silence_lists.extend(LimitList::from_file(path));
|
||||
// TODO Check if path contains trust value as part of filename when building limitlist
|
||||
}
|
||||
|
||||
for list in mutelists {
|
||||
for list in silence_lists {
|
||||
merged_list.add_limit_list(list, Limit::Silence);
|
||||
}
|
||||
}
|
||||
|
||||
merged_list.export_file("block.txt", "mute.txt", (50, 33))
|
||||
merged_list.export_file("suspend.txt", "silence.txt", (50, 33))
|
||||
}
|
||||
|
|
52
src/manip.rs
52
src/manip.rs
|
@ -9,7 +9,7 @@ use log::error;
|
|||
/// Specifies a limit type, whether block or silence
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Limit {
|
||||
Block,
|
||||
Suspend,
|
||||
Silence,
|
||||
}
|
||||
|
||||
|
@ -18,24 +18,24 @@ pub enum Limit {
|
|||
/// Wraps a host's weighted limits
|
||||
#[derive(Debug, Default, Eq, PartialEq)]
|
||||
pub struct LimitIndices {
|
||||
pub block: u16,
|
||||
pub suspend: u16,
|
||||
pub silence: u16,
|
||||
}
|
||||
|
||||
impl LimitIndices {
|
||||
pub fn add_limit(&mut self, limit: Limit, weight: u16) -> &mut Self {
|
||||
match limit {
|
||||
Limit::Block => self.block += weight,
|
||||
Limit::Suspend => self.suspend += weight,
|
||||
Limit::Silence => self.silence += weight,
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn normalize(&self, max: (u16, u16)) -> (u16, u16) {
|
||||
let block = (self.block as f32) / (max.0 as f32) * 100f32;
|
||||
let mute = (self.silence as f32) / (max.1 as f32) * 100f32;
|
||||
let suspend = (self.suspend as f32) / (max.0 as f32) * 100f32;
|
||||
let silence = (self.silence as f32) / (max.1 as f32) * 100f32;
|
||||
|
||||
(block as u16, mute as u16)
|
||||
(suspend as u16, silence as u16)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ impl From<(u16, u16)> for LimitIndices {
|
|||
/// testing.
|
||||
fn from(value: (u16, u16)) -> Self {
|
||||
Self {
|
||||
block: value.0,
|
||||
suspend: value.0,
|
||||
silence: value.1,
|
||||
}
|
||||
}
|
||||
|
@ -77,26 +77,26 @@ impl LimitList {
|
|||
/// A map of hosts (as strings) to their limit weights
|
||||
#[derive(Debug, Default, Eq, PartialEq)]
|
||||
pub struct MergedLimitList {
|
||||
pub hostmap: HashMap<String, LimitIndices>,
|
||||
pub hosts: HashMap<String, LimitIndices>,
|
||||
pub trusts: (u16, u16),
|
||||
}
|
||||
|
||||
impl MergedLimitList {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
hostmap: HashMap::new(),
|
||||
hosts: HashMap::new(),
|
||||
trusts: (0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_limit_list(&mut self, src: LimitList, limit_type: Limit) -> &mut Self {
|
||||
for host in src.hosts.into_iter() {
|
||||
let entry = self.hostmap.entry(host).or_default();
|
||||
let entry = self.hosts.entry(host).or_default();
|
||||
entry.add_limit(limit_type, src.trust);
|
||||
}
|
||||
|
||||
match limit_type {
|
||||
Limit::Block => self.trusts.0 += src.trust,
|
||||
Limit::Suspend => self.trusts.0 += src.trust,
|
||||
Limit::Silence => self.trusts.1 += src.trust,
|
||||
}
|
||||
|
||||
|
@ -105,35 +105,35 @@ impl MergedLimitList {
|
|||
|
||||
pub fn export_file(
|
||||
self,
|
||||
block_path: &str,
|
||||
mute_path: &str,
|
||||
indices: (u16, u16),
|
||||
suspend_path: &str,
|
||||
silence_path: &str,
|
||||
thresholds: (u16, u16),
|
||||
) -> std::io::Result<()> {
|
||||
if self.hostmap.is_empty() {
|
||||
if self.hosts.is_empty() {
|
||||
error!("Nothing to export!");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let (block_thresh, mute_thresh) = indices;
|
||||
let mut block_output: String = String::default();
|
||||
let mut mute_output: String = String::default();
|
||||
let (suspend_thresh, silence_thresh) = thresholds;
|
||||
let mut suspend_contents: String = String::default();
|
||||
let mut silence_contents: String = String::default();
|
||||
|
||||
for item in self.hostmap.into_iter() {
|
||||
let (block_trust, mute_trust) = item.1.normalize(self.trusts);
|
||||
for item in self.hosts.into_iter() {
|
||||
let (suspend_trust, silence_trust) = item.1.normalize(self.trusts);
|
||||
|
||||
// TODO What should the behavior be when an instance is blocked or
|
||||
// muted by most instances, but neither rises above the threshold?
|
||||
if block_trust >= block_thresh {
|
||||
block_output.push_str(&(item.0.clone() + "\n"));
|
||||
if suspend_trust >= suspend_thresh {
|
||||
suspend_contents.push_str(&(item.0.clone() + "\n"));
|
||||
}
|
||||
|
||||
if mute_trust >= mute_thresh {
|
||||
mute_output.push_str(&(item.0.clone() + "\n"));
|
||||
if silence_trust >= silence_thresh {
|
||||
silence_contents.push_str(&(item.0.clone() + "\n"));
|
||||
}
|
||||
}
|
||||
|
||||
fs::write(block_path, alphabetize_lines(block_output))?;
|
||||
fs::write(mute_path, alphabetize_lines(mute_output))?;
|
||||
fs::write(suspend_path, alphabetize_lines(suspend_contents))?;
|
||||
fs::write(silence_path, alphabetize_lines(silence_contents))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue