From d109d737519af69ed1a306c4fdc49af803dbe624 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 6 Aug 2023 13:34:43 +0100 Subject: [PATCH 1/4] rename directory param as downloads --- src/main.rs | 2 +- src/params/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8a0034e..d7d450c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ fn main() -> Result<()> { site, podal::Env { network: NetworkEnv::default(), - file: FileEnv::create(args.directory), + file: FileEnv::create(args.downloads), }, )?; diff --git a/src/params/mod.rs b/src/params/mod.rs index f92355f..d5651c3 100644 --- a/src/params/mod.rs +++ b/src/params/mod.rs @@ -6,5 +6,5 @@ pub struct Args { /// This is also the directory where the subscription and history files are stored. /// Defaults to the current directory #[arg(short, long, default_value = ".")] - pub directory: String, + pub downloads: String, } -- 2.45.3 From 61ebddf83b80fa0785cf3b385784acb6f3bd104d Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 6 Aug 2023 13:54:13 +0100 Subject: [PATCH 2/4] Add history param and pass entire Args to FileEnv::create --- src/file/env.rs | 11 ++++++----- src/file/read.rs | 17 +++++++++++++---- src/history/add.rs | 11 +++++++++-- src/history/find.rs | 17 +++++++++++++---- src/main.rs | 2 +- src/params/mod.rs | 5 +++++ 6 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/file/env.rs b/src/file/env.rs index 1718b75..09abe8c 100644 --- a/src/file/env.rs +++ b/src/file/env.rs @@ -1,3 +1,4 @@ +use crate::params::Args; use crate::prelude::*; use std::fs::{File, OpenOptions}; @@ -12,13 +13,13 @@ pub struct FileEnv { pub append_line: FileAppendLineFn, } impl FileEnv { - pub fn create(directory: String) -> Self { - let open_dir = directory.clone(); - let append_dir = directory.clone(); + pub fn create(a: Args) -> Self { + let open_dir = a.downloads.clone(); + let append_dir = a.downloads.clone(); Self { open: Box::new(move |file_name| { let path = format!("{}/{}", &open_dir, file_name); - let file = File::open(&path)?; + let file = File::open(path)?; Ok(file) }), append_line: Box::new(move |file_name, line| { @@ -27,7 +28,7 @@ impl FileEnv { .write(true) .append(true) .create(true) - .open(&path) + .open(path) .unwrap(); writeln!(file, "{}", line)?; Ok(()) diff --git a/src/file/read.rs b/src/file/read.rs index a5b6926..7f01541 100644 --- a/src/file/read.rs +++ b/src/file/read.rs @@ -18,7 +18,7 @@ pub fn lines_from(file_name: &str, e: &FileEnv) -> Result> { #[cfg(test)] mod tests { - use crate::test_utils::create_text_file; + use crate::{params::Args, test_utils::create_text_file}; use super::*; @@ -30,7 +30,10 @@ mod tests { file_name, include_bytes!("../../test/data/subscriptions.txt"), )?; - let file_env = FileEnv::create(dir.path().to_string_lossy().to_string()); + let file_env = FileEnv::create(Args { + downloads: dir.path().to_string_lossy().to_string(), + history: "downloaded.txt".to_string(), + }); //when let result = lines_from(file_name, &file_env)?; @@ -49,7 +52,10 @@ mod tests { file_name, include_bytes!("../../test/data/subscriptions-blank-line.txt"), )?; - let file_env = FileEnv::create(dir.path().to_string_lossy().to_string()); + let file_env = FileEnv::create(Args { + downloads: dir.path().to_string_lossy().to_string(), + history: "downloaded.txt".to_string(), + }); //when let result = lines_from(file_name, &file_env)?; @@ -68,7 +74,10 @@ mod tests { file_name, include_bytes!("../../test/data/subscriptions-comment.txt"), )?; - let file_env = FileEnv::create(dir.path().to_string_lossy().to_string()); + let file_env = FileEnv::create(Args { + downloads: dir.path().to_string_lossy().to_string(), + history: "downloaded.txt".to_string(), + }); //when let result = lines_from(file_name, &file_env)?; diff --git a/src/history/add.rs b/src/history/add.rs index 492916b..e75b519 100644 --- a/src/history/add.rs +++ b/src/history/add.rs @@ -13,6 +13,7 @@ mod tests { use crate::{ history::Link, + params::Args, test_utils::{create_text_file, read_text_file}, }; @@ -38,7 +39,10 @@ mod tests { add( &link, file_name, - &FileEnv::create(dir.path().to_string_lossy().to_string()), + &FileEnv::create(Args { + downloads: dir.path().to_string_lossy().to_string(), + history: "downloaded.txt".to_string(), + }), )?; //then @@ -69,7 +73,10 @@ mod tests { add( &link, file_name, - &FileEnv::create(dir.path().to_string_lossy().to_string()), + &FileEnv::create(Args { + downloads: dir.path().to_string_lossy().to_string(), + history: "downloaded.txt".to_string(), + }), )?; //then diff --git a/src/history/find.rs b/src/history/find.rs index 63c236b..3b7bb0e 100644 --- a/src/history/find.rs +++ b/src/history/find.rs @@ -18,7 +18,7 @@ pub fn find(link: &Link, file_name: &str, e: &FileEnv) -> Result { #[cfg(test)] mod test { - use crate::{history::Link, test_utils::create_text_file}; + use crate::{history::Link, params::Args, test_utils::create_text_file}; use super::*; #[test] @@ -38,7 +38,10 @@ mod test { let result = find( &link, file_name, - &FileEnv::create(dir.path().to_string_lossy().to_string()), + &FileEnv::create(Args { + downloads: dir.path().to_string_lossy().to_string(), + history: "downloaded.txt".to_string(), + }), )?; //then @@ -70,7 +73,10 @@ mod test { let result = find( &link, file_name, - &FileEnv::create(dir.path().to_string_lossy().to_string()), + &FileEnv::create(Args { + downloads: dir.path().to_string_lossy().to_string(), + history: "downloaded.txt".to_string(), + }), )?; //then @@ -102,7 +108,10 @@ mod test { let result = find( &link, file_name, - &FileEnv::create(dir.path().to_string_lossy().to_string()), + &FileEnv::create(Args { + downloads: dir.path().to_string_lossy().to_string(), + history: "downloaded.txt".to_string(), + }), )?; //then diff --git a/src/main.rs b/src/main.rs index d7d450c..828c106 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ fn main() -> Result<()> { site, podal::Env { network: NetworkEnv::default(), - file: FileEnv::create(args.downloads), + file: FileEnv::create(args), }, )?; diff --git a/src/params/mod.rs b/src/params/mod.rs index d5651c3..10c3199 100644 --- a/src/params/mod.rs +++ b/src/params/mod.rs @@ -7,4 +7,9 @@ pub struct Args { /// Defaults to the current directory #[arg(short, long, default_value = ".")] pub downloads: String, + + /// The name of the history file. + /// Defaults to "downloaded.txt" located in the downloads directory. + #[arg(long, default_value = "downloaded.txt")] + pub history: String, } -- 2.45.3 From 0d8cfb84149cc73e0c347a6f8bf814d1b8647128 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 6 Aug 2023 14:15:07 +0100 Subject: [PATCH 3/4] Use the history file from the cli --- src/file/env.rs | 2 +- src/file/read.rs | 6 +++--- src/history/add.rs | 4 ++-- src/history/find.rs | 6 +++--- src/lib.rs | 27 +++++++++++++++------------ src/main.rs | 5 ++--- 6 files changed, 26 insertions(+), 24 deletions(-) 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), }, )?; -- 2.45.3 From 69bf7b12e293bebf143f89af2c9431e1bcc4216a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 6 Aug 2023 14:16:46 +0100 Subject: [PATCH 4/4] clippy fix --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 7393d58..2095452 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,7 +69,7 @@ mod tests { let history_file_name = format!( "{}/{}", history_dir.path().to_string_lossy(), - history_file_name.to_string() + history_file_name ); let args = Args { downloads: subs_dir.path().to_string_lossy().to_string(), -- 2.45.3