Read from text files, reorganize tests
This commit is contained in:
parent
88a0b80e45
commit
1dd0083b5d
5
example_blocklist1.txt
Normal file
5
example_blocklist1.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
example.com
|
||||||
|
|
||||||
|
example.org
|
||||||
|
|
||||||
|
|
3
example_mutelist1.txt
Normal file
3
example_mutelist1.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
|
||||||
|
|
||||||
|
example.net
|
30
src/list.rs
30
src/list.rs
|
@ -1,34 +1,16 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
mod source;
|
||||||
|
mod tests;
|
||||||
|
|
||||||
|
pub use source::*;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
pub enum Action {
|
pub enum Action {
|
||||||
Block,
|
Block,
|
||||||
Silence,
|
Silence,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Source {
|
|
||||||
pub actions: HashMap<String, Action>,
|
|
||||||
pub trust: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<HashMap<String, Action>> for Source {
|
|
||||||
fn from(map: HashMap<String, Action>) -> Self {
|
|
||||||
Self {
|
|
||||||
actions: map,
|
|
||||||
trust: 100,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Source {
|
|
||||||
pub fn build(map: HashMap<String, Action>, trust: u16) -> Self {
|
|
||||||
let mut src = Self::from(map);
|
|
||||||
src.trust = trust;
|
|
||||||
src
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Eq, PartialEq)]
|
#[derive(Debug, Default, Eq, PartialEq)]
|
||||||
pub struct ActionTrust {
|
pub struct ActionTrust {
|
||||||
pub block: u16,
|
pub block: u16,
|
||||||
|
|
49
src/list/source.rs
Normal file
49
src/list/source.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
use std::{collections::HashMap, fs};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct Source {
|
||||||
|
pub actions: HashMap<String, Action>,
|
||||||
|
pub trust: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Source {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
actions: HashMap::new(),
|
||||||
|
trust: 100,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<HashMap<String, Action>> for Source {
|
||||||
|
fn from(map: HashMap<String, Action>) -> Self {
|
||||||
|
Self {
|
||||||
|
actions: map,
|
||||||
|
trust: 100,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Source {
|
||||||
|
pub fn build(map: HashMap<String, Action>, 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
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,7 +3,6 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use list::*;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add_action() {
|
fn add_action() {
|
||||||
|
@ -78,6 +77,21 @@ fn source_build_from_hashmap() {
|
||||||
assert_eq!(src2.trust, 123);
|
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]
|
#[test]
|
||||||
fn build_moderation_list() {
|
fn build_moderation_list() {
|
||||||
let mut ml = ModerationList::default();
|
let mut ml = ModerationList::default();
|
||||||
|
@ -88,44 +102,43 @@ fn build_moderation_list() {
|
||||||
(String::from("example.net"), Action::Block),
|
(String::from("example.net"), Action::Block),
|
||||||
]));
|
]));
|
||||||
|
|
||||||
let src2 = Source::from(HashMap::from([
|
let mut src2 = Source::default();
|
||||||
(String::from("example.com"), Action::Block),
|
src2.add_from_file("example_blocklist1.txt", Action::Block)
|
||||||
(String::from("example.org"), Action::Block),
|
.add_from_file("example_mutelist1.txt", Action::Silence);
|
||||||
(String::from("example.net"), Action::Silence),
|
|
||||||
]));
|
|
||||||
|
|
||||||
ml.add_source(src1).add_source(src2);
|
ml.add_source(src1).add_source(src2);
|
||||||
|
|
||||||
let test_ml = ModerationList(
|
let test_ml = ModerationList(HashMap::from([
|
||||||
HashMap::from([
|
|
||||||
(String::from("example.com"), ActionTrust::from((200, 0))),
|
(String::from("example.com"), ActionTrust::from((200, 0))),
|
||||||
(String::from("example.org"), ActionTrust::from((100, 100))),
|
(String::from("example.org"), ActionTrust::from((100, 100))),
|
||||||
(String::from("example.net"), ActionTrust::from((100, 100))),
|
(String::from("example.net"), ActionTrust::from((100, 100))),
|
||||||
])
|
]));
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(ml, test_ml);
|
assert_eq!(ml, test_ml);
|
||||||
|
|
||||||
let src3 = Source::build(HashMap::from([
|
let src3 = Source::build(
|
||||||
|
HashMap::from([
|
||||||
(String::from("example.com"), Action::Block),
|
(String::from("example.com"), Action::Block),
|
||||||
(String::from("example.org"), Action::Silence),
|
(String::from("example.org"), Action::Silence),
|
||||||
]), 200);
|
]),
|
||||||
|
200,
|
||||||
|
);
|
||||||
|
|
||||||
let src4 = Source::build(HashMap::from([
|
let src4 = Source::build(
|
||||||
|
HashMap::from([
|
||||||
(String::from("example.com"), Action::Block),
|
(String::from("example.com"), Action::Block),
|
||||||
(String::from("example.net"), Action::Silence),
|
(String::from("example.net"), Action::Silence),
|
||||||
]), 50);
|
]),
|
||||||
|
50,
|
||||||
|
);
|
||||||
|
|
||||||
ml.add_source(src3).add_source(src4);
|
ml.add_source(src3).add_source(src4);
|
||||||
|
|
||||||
let test_ml = ModerationList(
|
let test_ml = ModerationList(HashMap::from([
|
||||||
HashMap::from([
|
|
||||||
(String::from("example.com"), ActionTrust::from((450, 0))),
|
(String::from("example.com"), ActionTrust::from((450, 0))),
|
||||||
(String::from("example.org"), ActionTrust::from((100, 300))),
|
(String::from("example.org"), ActionTrust::from((100, 300))),
|
||||||
(String::from("example.net"), ActionTrust::from((100, 150))),
|
(String::from("example.net"), ActionTrust::from((100, 150))),
|
||||||
])
|
]));
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(ml, test_ml);
|
assert_eq!(ml, test_ml);
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
// src/main.rs
|
// src/main.rs
|
||||||
|
|
||||||
mod list;
|
mod list;
|
||||||
mod tests;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
// TODO argument parsing
|
||||||
|
// TODO logging
|
||||||
|
// TODO config file
|
||||||
}
|
}
|
Loading…
Reference in a new issue