diff --git a/src/file/env.rs b/src/file/env.rs index 09abe8c..241de60 100644 --- a/src/file/env.rs +++ b/src/file/env.rs @@ -13,7 +13,7 @@ pub struct FileEnv { pub append_line: FileAppendLineFn, } impl FileEnv { - pub fn create(a: Args) -> Self { + pub fn create(a: &Args) -> Self { let open_dir = a.downloads.clone(); let append_dir = a.downloads.clone(); Self { diff --git a/src/file/read.rs b/src/file/read.rs index 7f01541..1bd98e8 100644 --- a/src/file/read.rs +++ b/src/file/read.rs @@ -30,7 +30,7 @@ mod tests { file_name, include_bytes!("../../test/data/subscriptions.txt"), )?; - let file_env = FileEnv::create(Args { + let file_env = FileEnv::create(&Args { downloads: dir.path().to_string_lossy().to_string(), history: "downloaded.txt".to_string(), }); @@ -52,7 +52,7 @@ mod tests { file_name, include_bytes!("../../test/data/subscriptions-blank-line.txt"), )?; - let file_env = FileEnv::create(Args { + let file_env = FileEnv::create(&Args { downloads: dir.path().to_string_lossy().to_string(), history: "downloaded.txt".to_string(), }); @@ -74,7 +74,7 @@ mod tests { file_name, include_bytes!("../../test/data/subscriptions-comment.txt"), )?; - let file_env = FileEnv::create(Args { + let file_env = FileEnv::create(&Args { downloads: dir.path().to_string_lossy().to_string(), history: "downloaded.txt".to_string(), }); diff --git a/src/history/add.rs b/src/history/add.rs index e75b519..7ea8036 100644 --- a/src/history/add.rs +++ b/src/history/add.rs @@ -39,7 +39,7 @@ mod tests { add( &link, file_name, - &FileEnv::create(Args { + &FileEnv::create(&Args { downloads: dir.path().to_string_lossy().to_string(), history: "downloaded.txt".to_string(), }), @@ -73,7 +73,7 @@ mod tests { add( &link, file_name, - &FileEnv::create(Args { + &FileEnv::create(&Args { downloads: dir.path().to_string_lossy().to_string(), history: "downloaded.txt".to_string(), }), diff --git a/src/history/find.rs b/src/history/find.rs index 3b7bb0e..148a623 100644 --- a/src/history/find.rs +++ b/src/history/find.rs @@ -38,7 +38,7 @@ mod test { let result = find( &link, file_name, - &FileEnv::create(Args { + &FileEnv::create(&Args { downloads: dir.path().to_string_lossy().to_string(), history: "downloaded.txt".to_string(), }), @@ -73,7 +73,7 @@ mod test { let result = find( &link, file_name, - &FileEnv::create(Args { + &FileEnv::create(&Args { downloads: dir.path().to_string_lossy().to_string(), history: "downloaded.txt".to_string(), }), @@ -108,7 +108,7 @@ mod test { let result = find( &link, file_name, - &FileEnv::create(Args { + &FileEnv::create(&Args { downloads: dir.path().to_string_lossy().to_string(), history: "downloaded.txt".to_string(), }), diff --git a/src/lib.rs b/src/lib.rs index 67ceced..7393d58 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +use params::Args; use prelude::*; mod errors; @@ -19,16 +20,16 @@ pub struct Env { pub file: FileEnv, } -pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> { +pub fn run(subscriptions: &str, site: &str, a: &Args, e: Env) -> Result<()> { for channel_name in file::read::lines_from(subscriptions, &e.file)? { println!("Channel: {}", channel_name); let feed_url = feed::find(site, &channel_name, &e.network)?; for entry in feed::get(&feed_url, &e.network)?.entries() { if let Some(link) = entry.links().get(0).cloned() { - if !history::find(&link, history, &e.file)? { + if !history::find(&link, &a.history, &e.file)? { println!("Downloading {}: {}", &channel_name, entry.title().as_str()); (e.network.download_as_mp3)(&link.href)?; - history::add(&link, history, &e.file)?; + history::add(&link, &a.history, &e.file)?; } } } @@ -65,6 +66,15 @@ mod tests { let history_file_name = "history"; let history_dir = create_text_file(history_file_name, "c1-f2\nc2-f3".as_bytes())?; + let history_file_name = format!( + "{}/{}", + history_dir.path().to_string_lossy(), + history_file_name.to_string() + ); + let args = Args { + downloads: subs_dir.path().to_string_lossy().to_string(), + history: history_file_name.clone(), + }; let env = Env { network: NetworkEnv { fetch_as_text: mock_fetch_as_text_with_rss_url(HashMap::from([ @@ -89,21 +99,14 @@ mod tests { subs_file_name.to_string(), format!("{}/{}", subs_dir.path().to_string_lossy(), subs_file_name), ), - ( - history_file_name.to_string(), - format!( - "{}/{}", - history_dir.path().to_string_lossy(), - history_file_name - ), - ), + (history_file_name.to_string(), history_file_name), ])), append_line: mock_file_append_line(), }, }; //when - run(subs_file_name, history_file_name, site, env)?; + run(subs_file_name, site, &args, env)?; //then drop(subs_dir); drop(history_dir); diff --git a/src/main.rs b/src/main.rs index 828c106..4fcb4d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,18 +7,17 @@ use podal::prelude::*; fn main() -> Result<()> { println!("Podal"); let subscriptions = "subscriptions.txt"; - let history = "downloaded.txt"; let site = "https://www.youtube.com/"; let args = Args::parse(); podal::run( subscriptions, - history, site, + &args, podal::Env { network: NetworkEnv::default(), - file: FileEnv::create(args), + file: FileEnv::create(&args), }, )?;