46 lines
1.1 KiB
Rust
46 lines
1.1 KiB
Rust
use std::path::{Path, PathBuf};
|
|
|
|
use directories::ProjectDirs;
|
|
|
|
use crate::errors::{Error, ErrorStatus, DIRECTORIES_ERROR_MESSAGE};
|
|
|
|
pub fn get_project_dir() -> Result<ProjectDirs, Error> {
|
|
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<PathBuf, Error> {
|
|
if !path.exists() {
|
|
create_directory(path)?;
|
|
}
|
|
Ok(path.join(filename))
|
|
}
|
|
|
|
pub fn get_names_path() -> Result<PathBuf, Error> {
|
|
let directory = get_project_dir()?;
|
|
return get_file_from_directory(directory.data_dir(), "names.json");
|
|
}
|
|
|
|
pub fn get_names_project_path() -> Result<PathBuf, Error> {
|
|
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(),
|
|
}),
|
|
}
|
|
}
|