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

use serde::Deserialize;

#[derive(Deserialize)]
pub struct Config {
    #[serde(default)]
    pub urls: Vec<String>
}

pub fn default_config_file_locations() -> Vec<String> {
    let mut default_config_file_locations = vec![
        String::from("/etc/rss-aggregator.toml"),
    ];
    match dirs::config_dir().and_then(|config_dir| config_dir.into_string().ok()) {
        Some(config_dir) => {
            default_config_file_locations.push(format!("{config_dir}/rss-aggregator.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> {
        for path in default_config_file_locations() {
            if let Ok(config) = Self::parse_config_file_at_path(&path) {
                return Some(config);
            };
        };
        None
    }
}