goodbye unwraps

This commit is contained in:
gil 2024-06-02 14:38:32 -05:00
parent 58ef806e56
commit d5bb2be949
2 changed files with 20 additions and 14 deletions

View file

@ -79,13 +79,13 @@ impl LimitList {
src
}
pub fn import_file(&mut self, path: &str, limit: Limit) -> &mut Self {
let contents = fs::read_to_string(path).unwrap();
pub fn import_file(&mut self, path: &str, limit: Limit) -> std::io::Result<&mut Self> {
let contents = fs::read_to_string(path)?;
for host in contents.lines().filter(|line| !line.is_empty()) {
self.add_host(host, limit);
}
self
Ok(self)
}
}

View file

@ -78,10 +78,10 @@ fn limitlist_from_map_and_trust() {
}
#[test]
fn limitlist_from_file() {
fn limitlist_from_file() -> std::io::Result<()> {
let mut src = LimitList::default();
src.import_file("test/example_blocklist.txt", Limit::Block)
.import_file("test/example_mutelist.txt", Limit::Silence);
src.import_file("test/example_blocklist.txt", Limit::Block)?
.import_file("test/example_mutelist.txt", Limit::Silence)?;
let test_src = LimitList::from(HashMap::from([
(String::from("example.com"), Limit::Block),
@ -90,10 +90,12 @@ fn limitlist_from_file() {
]));
assert_eq!(test_src, src);
Ok(())
}
#[test]
fn mergedlist_from_limitlist() {
fn mergedlist_from_limitlist() -> std::io::Result<()> {
let mut ml = MergedLimitList::default();
let src1 = LimitList::from(HashMap::from([
@ -103,8 +105,8 @@ fn mergedlist_from_limitlist() {
]));
let mut src2 = LimitList::default();
src2.import_file("test/example_blocklist.txt", Limit::Block)
.import_file("test/example_mutelist.txt", Limit::Silence);
src2.import_file("test/example_blocklist.txt", Limit::Block)?
.import_file("test/example_mutelist.txt", Limit::Silence)?;
ml.add_limit_list(src1).add_limit_list(src2);
@ -147,10 +149,12 @@ fn mergedlist_from_limitlist() {
};
assert_eq!(ml, test_ml);
Ok(())
}
#[test]
fn mergedlist_export_txt() {
fn mergedlist_export_txt() -> std::io::Result<()> {
let mut ml = MergedLimitList::default();
let src1 = LimitList::from(HashMap::from([
@ -160,8 +164,8 @@ fn mergedlist_export_txt() {
]));
let mut src2 = LimitList::default();
src2.import_file("test/example_blocklist.txt", Limit::Block)
.import_file("test/example_mutelist.txt", Limit::Silence);
src2.import_file("test/example_blocklist.txt", Limit::Block)?
.import_file("test/example_mutelist.txt", Limit::Silence)?;
let src3 = LimitList::build(
HashMap::from([
@ -186,9 +190,11 @@ fn mergedlist_export_txt() {
let _ = ml.export_file("test/test_blocks.txt", "test/test_mutes.txt", (200, 150));
let file_blocks: String = fs::read_to_string("test/test_blocks.txt").unwrap();
let file_mutes: String = fs::read_to_string("test/test_mutes.txt").unwrap();
let file_blocks: String = fs::read_to_string("test/test_blocks.txt")?;
let file_mutes: String = fs::read_to_string("test/test_mutes.txt")?;
assert_eq!(file_blocks, "example.com\n");
assert_eq!(file_mutes, "example.net\nexample.org\n");
Ok(())
}