diff --git a/example_blocklist1.txt b/example_blocklist1.txt new file mode 100644 index 0000000..9abbe58 --- /dev/null +++ b/example_blocklist1.txt @@ -0,0 +1,5 @@ +example.com + +example.org + + diff --git a/example_mutelist1.txt b/example_mutelist1.txt new file mode 100644 index 0000000..f3ec5c9 --- /dev/null +++ b/example_mutelist1.txt @@ -0,0 +1,3 @@ + + +example.net diff --git a/src/list.rs b/src/list.rs index c3bcbf7..7a581ff 100644 --- a/src/list.rs +++ b/src/list.rs @@ -1,34 +1,16 @@ use std::collections::HashMap; -#[derive(Debug, Eq, PartialEq)] +mod source; +mod tests; + +pub use source::*; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum Action { Block, Silence, } -#[derive(Debug)] -pub struct Source { - pub actions: HashMap, - pub trust: u16, -} - -impl From> for Source { - fn from(map: HashMap) -> Self { - Self { - actions: map, - trust: 100, - } - } -} - -impl Source { - pub fn build(map: HashMap, trust: u16) -> Self { - let mut src = Self::from(map); - src.trust = trust; - src - } -} - #[derive(Debug, Default, Eq, PartialEq)] pub struct ActionTrust { pub block: u16, diff --git a/src/list/source.rs b/src/list/source.rs new file mode 100644 index 0000000..3afed2f --- /dev/null +++ b/src/list/source.rs @@ -0,0 +1,49 @@ +use std::{collections::HashMap, fs}; + +use super::*; + +#[derive(Debug, PartialEq, Eq)] +pub struct Source { + pub actions: HashMap, + pub trust: u16, +} + +impl Default for Source { + fn default() -> Self { + Self { + actions: HashMap::new(), + trust: 100, + } + } +} + +impl From> for Source { + fn from(map: HashMap) -> Self { + Self { + actions: map, + trust: 100, + } + } +} + +impl Source { + pub fn build(map: HashMap, trust: u16) -> Self { + let mut src = Self::from(map); + src.trust = trust; + src + } + + pub fn add_from_file(&mut self, path: &str, action: Action) -> &mut Self { + let contents = fs::read_to_string(path).unwrap(); + for host in contents.lines().filter(|line| !line.is_empty()) { + self.add_action(host, action); + } + + self + } + + fn add_action(&mut self, host: &str, action: Action) -> &mut Self { + self.actions.insert(host.to_string(), action); + self + } +} \ No newline at end of file diff --git a/src/tests.rs b/src/list/tests.rs similarity index 65% rename from src/tests.rs rename to src/list/tests.rs index 8bc3831..98bc145 100644 --- a/src/tests.rs +++ b/src/list/tests.rs @@ -3,7 +3,6 @@ use std::collections::HashMap; use super::*; -use list::*; #[test] fn add_action() { @@ -78,6 +77,21 @@ fn source_build_from_hashmap() { assert_eq!(src2.trust, 123); } +#[test] +fn source_add_from_file() { + let mut src = Source::default(); + src.add_from_file("example_blocklist1.txt", Action::Block) + .add_from_file("example_mutelist1.txt", Action::Silence); + + let test_src = Source::from(HashMap::from([ + (String::from("example.com"), Action::Block), + (String::from("example.org"), Action::Block), + (String::from("example.net"), Action::Silence), + ])); + + assert_eq!(test_src, src); +} + #[test] fn build_moderation_list() { let mut ml = ModerationList::default(); @@ -88,44 +102,43 @@ fn build_moderation_list() { (String::from("example.net"), Action::Block), ])); - let src2 = Source::from(HashMap::from([ - (String::from("example.com"), Action::Block), - (String::from("example.org"), Action::Block), - (String::from("example.net"), Action::Silence), - ])); + let mut src2 = Source::default(); + src2.add_from_file("example_blocklist1.txt", Action::Block) + .add_from_file("example_mutelist1.txt", Action::Silence); ml.add_source(src1).add_source(src2); - let test_ml = ModerationList( - HashMap::from([ - (String::from("example.com"), ActionTrust::from((200, 0))), - (String::from("example.org"), ActionTrust::from((100, 100))), - (String::from("example.net"), ActionTrust::from((100, 100))), - ]) - ); + let test_ml = ModerationList(HashMap::from([ + (String::from("example.com"), ActionTrust::from((200, 0))), + (String::from("example.org"), ActionTrust::from((100, 100))), + (String::from("example.net"), ActionTrust::from((100, 100))), + ])); assert_eq!(ml, test_ml); - let src3 = Source::build(HashMap::from([ - (String::from("example.com"), Action::Block), - (String::from("example.org"), Action::Silence), - ]), 200); + let src3 = Source::build( + HashMap::from([ + (String::from("example.com"), Action::Block), + (String::from("example.org"), Action::Silence), + ]), + 200, + ); - let src4 = Source::build(HashMap::from([ - (String::from("example.com"), Action::Block), - (String::from("example.net"), Action::Silence), - ]), 50); + let src4 = Source::build( + HashMap::from([ + (String::from("example.com"), Action::Block), + (String::from("example.net"), Action::Silence), + ]), + 50, + ); ml.add_source(src3).add_source(src4); - let test_ml = ModerationList( - HashMap::from([ - (String::from("example.com"), ActionTrust::from((450, 0))), - (String::from("example.org"), ActionTrust::from((100, 300))), - (String::from("example.net"), ActionTrust::from((100, 150))), - ]) - ); + let test_ml = ModerationList(HashMap::from([ + (String::from("example.com"), ActionTrust::from((450, 0))), + (String::from("example.org"), ActionTrust::from((100, 300))), + (String::from("example.net"), ActionTrust::from((100, 150))), + ])); assert_eq!(ml, test_ml); - } diff --git a/src/main.rs b/src/main.rs index da70c4c..673afcb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ // src/main.rs mod list; -mod tests; fn main() { println!("Hello, world!"); + // TODO argument parsing + // TODO logging + // TODO config file } \ No newline at end of file