From 73b9ad960bb9fde93b52cdf47a14b3e6a70434f0 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 29 Jul 2023 19:38:59 +0100 Subject: [PATCH] add NetworkEnv --- src/feed/find.rs | 22 ++++++++++++---------- src/feed/mod.rs | 4 ++-- src/fetch.rs | 9 +-------- src/lib.rs | 5 ++++- src/main.rs | 3 ++- src/network/env.rs | 12 ++++++++++++ src/network/mod.rs | 3 +++ 7 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 src/network/env.rs create mode 100644 src/network/mod.rs diff --git a/src/feed/find.rs b/src/feed/find.rs index f37ce84..c72e35b 100644 --- a/src/feed/find.rs +++ b/src/feed/find.rs @@ -1,15 +1,14 @@ +use crate::network::NetworkEnv; use crate::prelude::*; -use crate::fetch::FetchGet; - -pub fn find(site: &str, channel_name: &str, e: &FetchGet) -> Result { +pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result { if let Some(channel_prefix) = channel_name.chars().next() { if channel_prefix != '@' { return Err(format!("Channel Name must begin with an '@': {}", channel_name).into()); } } let channel_url = format!("{}{}", site, channel_name); - let response = (e)(&channel_url)?; + let response = (e.fetch_as_text)(&channel_url)?; let rss_url = scraper::Html::parse_document(&response) .select(&scraper::Selector::parse("link[title='RSS']").unwrap()) .next() @@ -23,15 +22,16 @@ pub fn find(site: &str, channel_name: &str, e: &FetchGet) -> Result { #[cfg(test)] mod tests { - use crate::fetch::Response; use super::*; #[test] fn finds_rss_url() -> Result<()> { //given - let fetch_get = &(get as FetchGet); + let network_env = NetworkEnv { + fetch_as_text: dummy_fetch_as_text, + }; //when - let result = find("site", "@channel", fetch_get)?; + let result = find("site", "@channel", &network_env)?; //then assert_eq!(result, "the-rss-url"); Ok(()) @@ -40,15 +40,17 @@ mod tests { #[test] fn error_if_channel_name_is_invalid() -> Result<()> { //given - let fetch_get = &(get as FetchGet); + let network_env = NetworkEnv { + fetch_as_text: dummy_fetch_as_text, + }; //when - let result = find("site", "invalid-channel-name", fetch_get); + let result = find("site", "invalid-channel-name", &network_env); //then assert!(result.is_err()); Ok(()) } - fn get(_url: &str) -> Result { + fn dummy_fetch_as_text(_url: &str) -> Result { Ok(r#" diff --git a/src/feed/mod.rs b/src/feed/mod.rs index 253bf26..30de824 100644 --- a/src/feed/mod.rs +++ b/src/feed/mod.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use crate::fetch::FetchGet; +use crate::network::NetworkEnv; mod find; mod get; @@ -14,5 +14,5 @@ pub struct FeedEnv { pub get: FeedGet, } -pub type FeedFind = fn(&str, &str, &FetchGet) -> Result; +pub type FeedFind = fn(&str, &str, &NetworkEnv) -> Result; pub type FeedGet = fn(&str) -> Result; diff --git a/src/fetch.rs b/src/fetch.rs index b0d47fd..cad48b0 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -5,11 +5,10 @@ use std::process::Command; pub struct FetchEnv { pub download: FetchDownload, - pub get: FetchGet, } pub type FetchDownload = fn(&Link) -> Result<()>; -pub type FetchGet = fn(&str) -> Result; +pub type FetchGet = fn(&str) -> Result; pub fn download(link: &Link) -> Result<()> { let cmd = "yt-dlp"; @@ -26,9 +25,3 @@ pub fn download(link: &Link) -> Result<()> { } Ok(()) } - -pub type Response = String; - -pub fn get(url: &str) -> Result { - Ok(reqwest::blocking::get(url)?.text()?) -} diff --git a/src/lib.rs b/src/lib.rs index 74787a2..549819e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod feed; pub mod fetch; pub mod file; pub mod history; +pub mod network; pub mod prelude; #[cfg(test)] @@ -12,10 +13,12 @@ use feed::FeedEnv; use fetch::FetchEnv; use file::FileEnv; use history::HistoryEnv; +use network::NetworkEnv; use prelude::*; pub struct Env { pub feed: FeedEnv, + pub network: NetworkEnv, pub history: HistoryEnv, pub fetch: FetchEnv, pub file: FileEnv, @@ -24,7 +27,7 @@ pub struct Env { pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> { for channel_name in file::read::lines_from(subscriptions, &e.file)? { println!("Channel: {}", channel_name); - let feed_url = (e.feed.find)(site, &channel_name, &e.fetch.get)?; + let feed_url = (e.feed.find)(site, &channel_name, &e.network)?; for entry in (e.feed.get)(&feed_url)?.entries() { if let Some(link) = entry.links().get(0).cloned() { if !(e.history.find)(&link, history, &e.file)? { diff --git a/src/main.rs b/src/main.rs index 5f11f1c..a13f07a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use podal::file::FileEnv; +use podal::network::NetworkEnv; use podal::prelude::*; use podal::{feed::FeedEnv, fetch::FetchEnv, history::HistoryEnv}; @@ -25,8 +26,8 @@ fn main() -> Result<()> { }, fetch: FetchEnv { download: podal::fetch::download, - get: podal::fetch::get, }, + network: NetworkEnv::default(), file: FileEnv::default(), }, )?; diff --git a/src/network/env.rs b/src/network/env.rs new file mode 100644 index 0000000..55209f5 --- /dev/null +++ b/src/network/env.rs @@ -0,0 +1,12 @@ +use crate::prelude::*; + +pub struct NetworkEnv { + pub fetch_as_text: fn(url: &str) -> Result, +} +impl Default for NetworkEnv { + fn default() -> Self { + Self { + fetch_as_text: |url| Ok(reqwest::blocking::get(url)?.text()?), + } + } +} diff --git a/src/network/mod.rs b/src/network/mod.rs new file mode 100644 index 0000000..3af1de6 --- /dev/null +++ b/src/network/mod.rs @@ -0,0 +1,3 @@ +mod env; + +pub use env::NetworkEnv;