summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 61701ab0ebfd05d942b0257cfcbddf4ebd11f924 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
    write(&channel);
}

fn load_config(config_file_arg: &str) -> anyhow::Result<Config> {
    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<String>, 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
}

fn write(channel: &Channel) {
    let items_summary: Vec<(String, String)> = channel
            .items().iter()
            .map(|item| (
                item.title().map(String::from).unwrap_or_else(String::new),
                item.pub_date().map(String::from).unwrap_or_else(String::new)))
            .collect();
    println!("{:#?}", items_summary);
}