diff --git a/Cargo.lock b/Cargo.lock index f20c98c..aba38b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1014,6 +1014,7 @@ name = "podal" version = "0.1.0" dependencies = [ "atom_syndication", + "bytes", "clap", "reqwest", "scraper", diff --git a/Cargo.toml b/Cargo.toml index 0c54643..a01c713 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ atom_syndication = "0.12.1" reqwest = { version = "0.11.18", features = ["json", "blocking"] } scraper = "0.17.1" clap = "4.3.19" +bytes = "1.4.0" [dev-dependencies] tempfile = "*" diff --git a/src/errors.rs b/src/errors.rs index 6769040..84c2ac6 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -4,6 +4,13 @@ use std::{fmt::Display, str::Utf8Error, string::FromUtf8Error}; pub struct Error { details: String, } +impl Error { + pub fn message(details: &str) -> Self { + Self { + details: details.to_string(), + } + } +} impl Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.details.to_string().as_str()) diff --git a/src/feed/find.rs b/src/feed/find.rs index c72e35b..9703c94 100644 --- a/src/feed/find.rs +++ b/src/feed/find.rs @@ -23,12 +23,15 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result { #[cfg(test)] mod tests { + use crate::errors::Error; + use super::*; #[test] fn finds_rss_url() -> Result<()> { //given let network_env = NetworkEnv { fetch_as_text: dummy_fetch_as_text, + fetch_as_bytes: dummy_fetch_as_bytes, }; //when let result = find("site", "@channel", &network_env)?; @@ -42,6 +45,7 @@ mod tests { //given let network_env = NetworkEnv { fetch_as_text: dummy_fetch_as_text, + fetch_as_bytes: dummy_fetch_as_bytes, }; //when let result = find("site", "invalid-channel-name", &network_env); @@ -58,4 +62,7 @@ mod tests { "# .to_string()) } + fn dummy_fetch_as_bytes(_url: &str) -> Result { + Err(Error::message("Not implemented")) + } } diff --git a/src/feed/get.rs b/src/feed/get.rs deleted file mode 100644 index 1a7ea40..0000000 --- a/src/feed/get.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::prelude::*; - -use atom_syndication::Feed; - -pub fn reqwest_blocking_get(url: &str) -> Result { - let content = reqwest::blocking::get(url)?.bytes()?; - let channel = Feed::read_from(&content[..])?; - Ok(channel) -} diff --git a/src/feed/mod.rs b/src/feed/mod.rs index 30de824..d8c68d0 100644 --- a/src/feed/mod.rs +++ b/src/feed/mod.rs @@ -1,18 +1,20 @@ use crate::prelude::*; use crate::network::NetworkEnv; +use atom_syndication::Feed; mod find; -mod get; -use atom_syndication::Feed; pub use find::find; -pub use get::reqwest_blocking_get; pub struct FeedEnv { pub find: FeedFind, - pub get: FeedGet, } pub type FeedFind = fn(&str, &str, &NetworkEnv) -> Result; -pub type FeedGet = fn(&str) -> Result; + +pub fn get(url: &str, e: &NetworkEnv) -> Result { + let content = (e.fetch_as_bytes)(url)?; + let channel = Feed::read_from(&content[..])?; + Ok(channel) +} diff --git a/src/lib.rs b/src/lib.rs index 549819e..9137f13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,7 @@ 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.network)?; - for entry in (e.feed.get)(&feed_url)?.entries() { + for entry in feed::get(&feed_url, &e.network)?.entries() { if let Some(link) = entry.links().get(0).cloned() { if !(e.history.find)(&link, history, &e.file)? { println!("Downloading {}: {}", &channel_name, entry.title().as_str()); diff --git a/src/main.rs b/src/main.rs index a13f07a..395e811 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,6 @@ fn main() -> Result<()> { podal::Env { feed: FeedEnv { find: podal::feed::find, - get: podal::feed::reqwest_blocking_get, }, history: HistoryEnv { diff --git a/src/network/env.rs b/src/network/env.rs index 55209f5..8ea937f 100644 --- a/src/network/env.rs +++ b/src/network/env.rs @@ -2,11 +2,13 @@ use crate::prelude::*; pub struct NetworkEnv { pub fetch_as_text: fn(url: &str) -> Result, + pub fetch_as_bytes: fn(url: &str) -> Result, } impl Default for NetworkEnv { fn default() -> Self { Self { fetch_as_text: |url| Ok(reqwest::blocking::get(url)?.text()?), + fetch_as_bytes: |url| Ok(reqwest::blocking::get(url)?.bytes()?), } } }