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;
pub struct FeedEnv {
pub find: FeedFind,
pub get: FeedGet,
}
pub type FeedFind = fn(&str, &str) -> Result<String>;
pub type FeedGet = fn(&str) -> Result<Feed>;

View file

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

View file

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

View file

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

View file

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