use std::{fs::File, io::{read_to_string}, path::Path}; use serde::Deserialize; #[derive(Deserialize)] pub struct Config { #[serde(default)] pub urls: Vec } pub fn default_config_file_locations() -> Vec { 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 { 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 { for path in default_config_file_locations() { if let Ok(config) = Self::parse_config_file_at_path(&path) { return Some(config); }; }; None } }