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

use serde::Deserialize;

#[derive(Deserialize)]
pub struct Config {
}

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() {
            match Self::parse_config_file_at_path(&path) {
                Ok(config) => { return Some(config); },
                Err(_) => {},
            };
        };
        None
    }
}