ptcg-tools/src/directories.rs

48 lines
1.8 KiB
Rust
Raw Normal View History

//! User directories handling
use anyhow::{Result, anyhow};
use camino::Utf8PathBuf;
use directories::ProjectDirs;
use crate::constants::APP_NAME;
/// Returns the path to the user data directory.
///
/// Post condition: this function ensures the directory is already created when returning.
pub async fn data_directory() -> Result<Utf8PathBuf> {
let user_directory = ProjectDirs::from("cl", "bstr", APP_NAME)
.ok_or_else(|| anyhow!("failed to get ProjectDirs"))?
.data_dir()
.to_path_buf();
let user_directory = Utf8PathBuf::try_from(user_directory)?;
tokio::fs::create_dir_all(&user_directory).await?;
Ok(user_directory)
}
/// Returns the path to the user data cache directory.
///
/// Post condition: this function ensures the directory is already created when returning.
pub async fn data_cache_directory() -> Result<Utf8PathBuf> {
let user_directory = ProjectDirs::from("cl", "bstr", APP_NAME)
.ok_or_else(|| anyhow!("failed to get ProjectDirs"))?
.cache_dir()
.to_path_buf();
let user_directory = Utf8PathBuf::try_from(user_directory)?;
let user_directory = user_directory.join("data");
tokio::fs::create_dir_all(&user_directory).await?;
Ok(user_directory)
}
/// Returns the path to the user data cache directory.
///
/// Post condition: this function ensures the directory is already created when returning.
pub async fn image_cache_directory() -> Result<Utf8PathBuf> {
let user_directory = ProjectDirs::from("cl", "bstr", APP_NAME)
.ok_or_else(|| anyhow!("failed to get ProjectDirs"))?
.cache_dir()
.to_path_buf();
let user_directory = Utf8PathBuf::try_from(user_directory)?;
let user_directory = user_directory.join("data");
tokio::fs::create_dir_all(&user_directory).await?;
Ok(user_directory)
}