blob: 089d3e2a85533dac67afe3ee959a31ca0f544adf (
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 {
#[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"),
];
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.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
}
}
|