use app_dirs2::{app_root, get_app_root, AppDataType, AppInfo};
use std::{error::Error, path::PathBuf};
const APP_INFO: AppInfo = AppInfo {
name: "usenet_reborn",
author: "ReK2",
};
pub struct Paths {
pub config: PathBuf,
pub subscriptions: PathBuf,
pub cache_db: PathBuf,
}
pub fn locate_paths() -> Result<Paths, Box<dyn Error>> {
let config_root = get_app_root(AppDataType::UserConfig, &APP_INFO)?;
let mut config = config_root.join("config.toml");
if !config.exists() {
let alt = std::env::current_dir()?.join("config.toml");
if alt.exists() {
config = alt;
} else {
return Err(format!(
"Configuration file not found.\n\
Please place ‘config.toml’ in {} or in the current directory.",
config_root.display()
)
.into());
}
}
let mut subscriptions = config_root.join("subscriptions");
if !subscriptions.exists() {
let alt = std::env::current_dir()?.join("subscriptions");
if alt.exists() {
subscriptions = alt;
} else {
return Err(format!(
"Subscriptions file not found.\n\
Please place ‘subscriptions’ in {} or in the current directory.",
config_root.display()
)
.into());
}
}
let cache_root = app_root(AppDataType::UserCache, &APP_INFO)?;
std::fs::create_dir_all(&cache_root)?;
let cache_db = cache_root.join("cache.sqlite3");
Ok(Paths {
config,
subscriptions,
cache_db,
})
}