diff --git a/src/file.rs b/src/file.rs new file mode 100644 index 0000000..83c6006 --- /dev/null +++ b/src/file.rs @@ -0,0 +1,13 @@ +use std::fs::File; + +pub struct FileEnv { + pub open: FileOpen, +} +impl Default for FileEnv { + fn default() -> Self { + Self { + open: |path| File::open(path), + } + } +} +pub type FileOpen = fn(path: &str) -> std::io::Result; diff --git a/src/history/find.rs b/src/history/find.rs index 4171eb2..70162d2 100644 --- a/src/history/find.rs +++ b/src/history/find.rs @@ -1,11 +1,10 @@ -use crate::prelude::*; +use crate::{file::FileEnv, prelude::*}; use atom_syndication::Link; -use std::fs::File; use std::io::{BufRead, BufReader}; -pub fn find(link: &Link, file_name: &str) -> Result { - if let Ok(file) = File::open(file_name) { +pub fn find(link: &Link, file_name: &str, e: &FileEnv) -> Result { + if let Ok(file) = (e.open)(file_name) { let reader = BufReader::new(file); for line in reader.lines() { if line? == link.href { @@ -34,9 +33,8 @@ mod test { title: None, length: None, }; - //when - let result = find(&link, &file_name)?; + let result = find(&link, &file_name, &FileEnv::default())?; //then drop(dir); @@ -61,7 +59,7 @@ mod test { }; //when - let result = find(&link, &file_name)?; + let result = find(&link, &file_name, &FileEnv::default())?; //then drop(dir); @@ -88,7 +86,7 @@ mod test { }; //when - let result = find(&link, &file_name)?; + let result = find(&link, &file_name, &FileEnv::default())?; //then drop(dir); diff --git a/src/history/mod.rs b/src/history/mod.rs index d6f7255..47f7e36 100644 --- a/src/history/mod.rs +++ b/src/history/mod.rs @@ -1,4 +1,4 @@ -use crate::prelude::*; +use crate::{file::FileEnv, prelude::*}; mod add; mod find; @@ -13,5 +13,5 @@ pub struct HistoryEnv { pub add: HistoryAdd, } -pub type HistoryFind = fn(&Link, &str) -> Result; +pub type HistoryFind = fn(&Link, &str, &FileEnv) -> Result; pub type HistoryAdd = fn(&Link, &str) -> Result<()>; diff --git a/src/lib.rs b/src/lib.rs index ce77f4d..e51e116 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ mod errors; pub mod feed; pub mod fetch; +pub mod file; pub mod history; pub mod prelude; mod subscriptions; @@ -10,6 +11,7 @@ mod test_utils; use feed::FeedEnv; use fetch::FetchEnv; +use file::FileEnv; use history::HistoryEnv; use prelude::*; @@ -17,15 +19,16 @@ pub struct Env { pub feed: FeedEnv, pub history: HistoryEnv, pub fetch: FetchEnv, + pub file: FileEnv, } pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> { - for channel_name in subscriptions::lines_from(subscriptions)? { + for channel_name in subscriptions::lines_from(subscriptions, &e.file)? { println!("Channel: {}", channel_name); let feed_url = (e.feed.find)(site, &channel_name, &e.fetch.get)?; for entry in (e.feed.get)(&feed_url)?.entries() { if let Some(link) = entry.links().get(0).cloned() { - if !(e.history.find)(&link, history)? { + if !(e.history.find)(&link, history, &e.file)? { println!("Downloading {}: {}", &channel_name, entry.title().as_str()); (e.fetch.download)(&link)?; (e.history.add)(&link, history)?; diff --git a/src/main.rs b/src/main.rs index 8e7f6c5..5f11f1c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use podal::file::FileEnv; use podal::prelude::*; use podal::{feed::FeedEnv, fetch::FetchEnv, history::HistoryEnv}; @@ -26,6 +27,7 @@ fn main() -> Result<()> { download: podal::fetch::download, get: podal::fetch::get, }, + file: FileEnv::default(), }, )?; diff --git a/src/subscriptions.rs b/src/subscriptions.rs index 5ecc379..edaf777 100644 --- a/src/subscriptions.rs +++ b/src/subscriptions.rs @@ -1,10 +1,10 @@ use crate::prelude::*; -use std::fs::File; +use crate::file::FileEnv; use std::io::{BufRead, BufReader}; -pub fn lines_from(file_name: &str) -> Result> { - let file = File::open(file_name)?; +pub fn lines_from(file_name: &str, e: &FileEnv) -> Result> { + let file = (e.open)(file_name)?; let reader = BufReader::new(file); let mut lines = vec![]; for line in reader.lines().flatten() { @@ -29,9 +29,10 @@ mod tests { "subscriptions.txt", include_bytes!("../test/data/subscriptions.txt"), )?; + let file_env = FileEnv::default(); //when - let result = lines_from(&file_name)?; + let result = lines_from(&file_name, &file_env)?; //then drop(dir); @@ -46,9 +47,10 @@ mod tests { "subscriptions.txt", include_bytes!("../test/data/subscriptions-blank-line.txt"), )?; + let file_env = FileEnv::default(); //when - let result = lines_from(&file_name)?; + let result = lines_from(&file_name, &file_env)?; //then drop(dir); @@ -63,9 +65,10 @@ mod tests { "subscriptions.txt", include_bytes!("../test/data/subscriptions-comment.txt"), )?; + let file_env = FileEnv::default(); //when - let result = lines_from(&file_name)?; + let result = lines_from(&file_name, &file_env)?; //then drop(dir);