use std::path::{Path, PathBuf}; use directories::ProjectDirs; use crate::errors::{Error, ErrorStatus, DIRECTORIES_ERROR_MESSAGE}; pub fn get_project_dir() -> Result { match ProjectDirs::from("moe", "solarpunk", "meowy-webring") { Some(project) => Ok(project), None => Err(Error { status: ErrorStatus::DirectoriesError, data: DIRECTORIES_ERROR_MESSAGE.into(), }), } } pub fn get_file_from_directory(path: &Path, filename: &str) -> Result { if !path.exists() { create_directory(path)?; } Ok(path.join(filename)) } pub fn get_names_path() -> Result { let directory = get_project_dir()?; return get_file_from_directory(directory.data_dir(), "names.json"); } pub fn get_names_project_path() -> Result { let directory = get_project_dir()?; return Ok(directory.data_dir().to_path_buf()); } fn create_directory(path: &Path) -> Result<(), Error> { match std::fs::create_dir_all(path) { Ok(_) => { log::debug!("created the directory {}", path.display()); Ok(()) } Err(err) => Err(Error { status: ErrorStatus::IOError, data: err.to_string(), }), } }