summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 25a008d3464942b3bee3b4f84cff0ae6f88f0f83 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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, Item, Source};

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<String>, 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<String> = 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::<Result<Vec<String>, std::io::Error>>()?
    } else {
        Vec::new()
    };

    Ok((urls, config))
}

fn generate_channel(urls: Vec<String>, 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 items_with_source(c));
            future::ready(())
        }).await;
    });
    channel
}

fn items_with_source(c: Channel) -> Vec<Item> {
    let source = Source {
        url: c.link.clone(),
        title: Some(c.title.clone()),
    };
    c.into_items().into_iter()
        .map(|mut item| { item.source = Some(source.clone()); item })
        .collect()
}