/ crates / distrox-cli / src / xdg.rs
xdg.rs
 1  pub async fn ensure_xdg_dirs_exists() -> Result<(), XdgError> {
 2      let dir = xdg::BaseDirectories::with_prefix("distrox");
 3  
 4      if let Some(cache_home) = dir.get_cache_home() {
 5          ensure_dir(cache_home).await?;
 6      }
 7  
 8      if let Some(config_home) = dir.get_config_home() {
 9          ensure_dir(config_home).await?;
10      }
11  
12      if let Some(state_home) = dir.get_state_home() {
13          ensure_dir(state_home).await?;
14      }
15  
16      Ok(())
17  }
18  
19  async fn ensure_dir(path: std::path::PathBuf) -> Result<(), XdgError> {
20      if path.exists() {
21          return Ok(());
22      }
23  
24      tracing::info!("Trying to create {}", path.display());
25      tokio::fs::create_dir_all(path)
26          .await
27          .map_err(XdgError::FailedToCreate)
28  }
29  
30  #[derive(Debug, thiserror::Error)]
31  pub enum XdgError {
32      #[error("Failed to create directory")]
33      FailedToCreate(#[source] std::io::Error),
34  }