summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: fa3dfba9b8d334959187b9f1a51de87eca9eee35 (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
use std::{collections::BTreeMap, fs::File, io::read_to_string, path::Path};

use rss::{Category, Cloud, Image, TextInput, extension::{Extension, dublincore::DublinCoreExtension, itunes::ITunesChannelExtension, syndication::SyndicationExtension}};
use serde::Deserialize;

#[derive(Deserialize)]
pub struct Config {
    pub subscriptions_path: Option<String>,
    #[serde(default)]
    pub title: String,
    pub link: String,
    pub description: String,
    pub language: Option<String>,
    pub copyright: Option<String>,
    pub managing_editor: Option<String>,
    pub webmaster: Option<String>,
    #[serde(default)]
    pub categories: Vec<Category>,
    pub cloud: Option<Cloud>,
    pub ttl: Option<String>,
    pub image: Option<Image>,
    pub text_input: Option<TextInput>,
    #[serde(default)]
    pub skip_hours: Vec<String>,
    #[serde(default)]
    pub skip_days: Vec<String>,
    #[serde(default)]
    pub extensions: BTreeMap<String, BTreeMap<String, Vec<Extension>>>,
    pub itunes_ext: Option<ITunesChannelExtension>,
    pub dublin_core_ext: Option<DublinCoreExtension>,
    pub syndication_ext: Option<SyndicationExtension>,
    #[serde(default)]
    pub namespaces: BTreeMap<String, String>,
}

pub fn default_config_file_locations() -> Vec<String> {
    let mut default_config_file_locations = vec![
        String::from("/etc/rss-aggregator/config.toml"),
    ];
    let config_dir = dirs::config_dir()
        .and_then(|config_dir| config_dir.into_string().ok());
    if let Some(config_dir) = config_dir {
        default_config_file_locations.push(format!("{config_dir}/rss-aggregator/config.toml"));
    };
    default_config_file_locations
}

impl Config {
    pub fn parse_config_file_at_path(path: &str) -> anyhow::Result<Config> {
        let path = Path::new(path);
        let file = File::open(path)?;
        let contents = read_to_string(file)?;
        Ok(toml::from_str(&contents)?)
    }

    pub fn find_config() -> Option<(Config, String)> {
        for path in default_config_file_locations() {
            if let Ok(config) = Self::parse_config_file_at_path(&path) {
                return Some((config, path));
            };
        };
        None
    }
}