Use Env to pass in functions
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful

This commit is contained in:
Paul Campbell 2023-07-25 19:24:15 +01:00
parent 9087482672
commit 1fd5df7151
5 changed files with 45 additions and 23 deletions

View file

@ -8,5 +8,10 @@ pub use get::get;
type Feed = atom_syndication::Feed; type Feed = atom_syndication::Feed;
pub struct FeedEnv {
pub find: FeedFind,
pub get: FeedGet,
}
pub type FeedFind = fn(&str, &str) -> Result<String>; pub type FeedFind = fn(&str, &str) -> Result<String>;
pub type FeedGet = fn(&str) -> Result<Feed>; pub type FeedGet = fn(&str) -> Result<Feed>;

View file

@ -3,6 +3,10 @@ use crate::prelude::*;
use atom_syndication::Link; use atom_syndication::Link;
use std::process::Command; use std::process::Command;
pub struct FetchEnv {
pub download: FetchDownload,
}
pub type FetchDownload = fn(&Link) -> Result<()>; pub type FetchDownload = fn(&Link) -> Result<()>;
pub fn download(link: &Link) -> Result<()> { pub fn download(link: &Link) -> Result<()> {

View file

@ -8,5 +8,10 @@ pub use find::find;
type Link = atom_syndication::Link; type Link = atom_syndication::Link;
pub struct HistoryEnv {
pub find: HistoryFind,
pub add: HistoryAdd,
}
pub type HistoryFind = fn(&Link, &str) -> Result<bool>; pub type HistoryFind = fn(&Link, &str) -> Result<bool>;
pub type HistoryAdd = fn(&Link, &str) -> Result<()>; pub type HistoryAdd = fn(&Link, &str) -> Result<()>;

View file

@ -5,30 +5,27 @@ pub mod history;
pub mod prelude; pub mod prelude;
mod subscriptions; mod subscriptions;
use feed::{FeedFind, FeedGet}; use feed::FeedEnv;
use fetch::FetchDownload; use fetch::FetchEnv;
use history::{HistoryAdd, HistoryFind}; use history::HistoryEnv;
use prelude::*; use prelude::*;
pub fn run( pub struct Env {
subscriptions: &str, pub feed: FeedEnv,
history: &str, pub history: HistoryEnv,
site: &str, pub fetch: FetchEnv,
feed_find: FeedFind, }
feed_get: FeedGet,
history_find: HistoryFind, pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> {
history_add: HistoryAdd,
fetch_download: FetchDownload,
) -> Result<()> {
for channel_name in subscriptions::lines_from(subscriptions)? { for channel_name in subscriptions::lines_from(subscriptions)? {
println!("Channel: {}", channel_name); println!("Channel: {}", channel_name);
let feed_url = feed_find(site, &channel_name)?; let feed_url = (e.feed.find)(site, &channel_name)?;
for entry in feed_get(&feed_url)?.entries() { for entry in (e.feed.get)(&feed_url)?.entries() {
if let Some(link) = entry.links().get(0).cloned() { if let Some(link) = entry.links().get(0).cloned() {
if !history_find(&link, history)? { if !(e.history.find)(&link, history)? {
println!("Downloading {}: {}", &channel_name, entry.title().as_str()); println!("Downloading {}: {}", &channel_name, entry.title().as_str());
fetch_download(&link)?; (e.fetch.download)(&link)?;
history_add(&link, history)?; (e.history.add)(&link, history)?;
} }
} }
} }

View file

@ -1,5 +1,7 @@
use podal::prelude::*; use podal::prelude::*;
use podal::{feed::FeedEnv, fetch::FetchEnv, history::HistoryEnv};
fn main() -> Result<()> { fn main() -> Result<()> {
println!("Podal"); println!("Podal");
let subscriptions = "subscriptions.txt"; let subscriptions = "subscriptions.txt";
@ -10,11 +12,20 @@ fn main() -> Result<()> {
subscriptions, subscriptions,
history, history,
site, site,
podal::feed::find, podal::Env {
podal::feed::get, feed: FeedEnv {
podal::history::find, find: podal::feed::find,
podal::history::add, get: podal::feed::get,
podal::fetch::download, },
history: HistoryEnv {
find: podal::history::find,
add: podal::history::add,
},
fetch: FetchEnv {
download: podal::fetch::download,
},
},
)?; )?;
println!("Done"); println!("Done");