mod channel; mod config; mod retrieve; use std::{fs::File, future, io::{BufRead, BufReader}, path::{Path, PathBuf}}; use anyhow::anyhow; use clap::Parser; use futures::{StreamExt, executor::block_on}; use rss::Channel; use crate::{channel::make_new_empty_channel, config::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 (urls, config) = load_config(&args.config_file).unwrap(); let channel = generate_channel(urls, config); let _ = channel.pretty_write_to(std::io::stdout(), b' ', 4).unwrap(); } fn load_config(config_file_arg: &str) -> anyhow::Result<(Vec, Config)> { let (config, config_path) = 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(|config| (config, String::from(path))).map_err(|err| anyhow!("Failed to parse config at {}. Error: {}", path, err)), }?; let subscriptions_path: PathBuf = config.subscriptions_path.as_ref() .map(|p| Path::new(&p).to_path_buf()) .unwrap_or_else( || Path::new(&config_path) .parent() .unwrap_or_else(|| Path::new("/")) .join("subscriptions.txt")); let urls: Vec = if subscriptions_path.is_file() { let subscriptions_file = File::open(subscriptions_path)?; BufReader::new(subscriptions_file).lines() .filter(|line| line.is_err() || line.as_ref().is_ok_and(|line| line.trim().len() > 0 && !line.trim().starts_with("#"))) .collect::, std::io::Error>>()? } else { Vec::new() }; Ok((urls, config)) } fn generate_channel(urls: Vec, config: Config) -> Channel { let mut channel = make_new_empty_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 }