mod channel; mod config; mod retrieve; use std::future; use anyhow::anyhow; use clap::Parser; use futures::{StreamExt, executor::block_on}; use rss::Channel; use crate::{channel::make_new_empty_channel, config::{ChannelConfig, Config}, retrieve::retrieve}; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { /// Path to the config file. If blank, searches default config locations. #[arg(short = 'c', long = "config", default_value = "")] config_file: String, } fn main() { let args = Args::parse(); let config = load_config(&args.config_file).unwrap(); let channel = generate_channel(config.urls, config.channel); let _ = channel.pretty_write_to(std::io::stdout(), b' ', 4).unwrap(); } fn load_config(config_file_arg: &str) -> anyhow::Result { match config_file_arg { "" => Config::find_config().ok_or_else(|| anyhow!("Failed to find config. Paths searched: {:#?}", config::default_config_file_locations())), path => Config::parse_config_file_at_path(path).map_err(|err| anyhow!("Failed to parse config at {}. Error: {}", path, err)), } } fn generate_channel(urls: Vec, channel_config: ChannelConfig) -> Channel { let mut channel = make_new_empty_channel(channel_config); block_on(async { retrieve(urls.iter().map(AsRef::as_ref)).await.for_each(|c| { channel.items.append(&mut c.into_items()); future::ready(()) }).await; }); channel }