tek/.old/src/app_paths.rs

52 lines
1.5 KiB
Rust

//! Global settings.
use crate::*;
use std::path::{Path, PathBuf};
use std::fs::{File, create_dir_all};
const CONFIG_FILE_NAME: &str = "tek.toml";
const PROJECT_FILE_NAME: &str = "project.toml";
/// Filesystem locations of things.
pub struct AppPaths {
config_dir: PathBuf,
config_file: PathBuf,
data_dir: PathBuf,
project_file: PathBuf,
}
impl AppPaths {
pub fn new (xdg: &XdgApp) -> Usually<Self> {
let config_dir = PathBuf::from(xdg.app_config()?);
let config_file = PathBuf::from(config_dir.join(CONFIG_FILE_NAME));
let data_dir = PathBuf::from(xdg.app_data()?);
let project_file = PathBuf::from(data_dir.join(PROJECT_FILE_NAME));
Ok(Self { config_dir, config_file, data_dir, project_file })
}
pub fn should_create (&self) -> bool {
for path in [
&self.config_dir,
&self.config_file,
&self.data_dir,
&self.project_file,
].iter() {
if !Path::new(path).exists() {
return true
}
}
false
}
pub fn create (&self) -> Usually<()> {
for dir in [&self.config_dir, &self.data_dir].iter() {
if !Path::new(dir).exists() {
create_dir_all(&dir)?;
}
}
for file in [&self.config_file, &self.project_file].iter() {
if !Path::new(file).exists() {
File::create_new(&file)?;
}
}
Ok(())
}
}