Skip to main content

cap_directories/
project_dirs.rs

1use crate::not_found;
2use cap_std::fs::Dir;
3use cap_std::{ambient_authority, AmbientAuthority};
4use std::{fs, io};
5
6/// `ProjectDirs` computes the cache, config or data directories for a specific
7/// application, which are derived from the standard directories and the name
8/// of the project/organization.
9///
10/// This corresponds to [`directories::ProjectDirs`], except that the
11/// functions create the directories if they don't exist, open them, and return
12/// `Dir`s instead of returning `Path`s.
13///
14/// Unlike `directories::ProjectDirs`, this API has no
15/// `ProjectDirs::from_path`, `ProjectDirs::path` or
16/// `ProjectDirs::project_path`, and the `*_dir` functions return `Dir`s rather
17/// than `Path`s, because absolute paths don't interoperate well with the
18/// capability model.
19#[derive(Clone)]
20pub struct ProjectDirs {
21    inner: directories::ProjectDirs,
22}
23
24impl ProjectDirs {
25    /// Creates a `ProjectDirs` struct from values describing the project.
26    ///
27    /// This corresponds to [`directories::ProjectDirs::from`].
28    ///
29    /// # Ambient Authority
30    ///
31    /// This function makes use of ambient authority to access the project
32    /// directories.
33    pub fn from(
34        qualifier: &str,
35        organization: &str,
36        application: &str,
37        ambient_authority: AmbientAuthority,
38    ) -> Option<Self> {
39        let _ = ambient_authority;
40        let inner = directories::ProjectDirs::from(qualifier, organization, application)?;
41        Some(Self { inner })
42    }
43
44    /// Returns the project's cache directory.
45    ///
46    /// This corresponds to [`directories::ProjectDirs::cache_dir`].
47    pub fn cache_dir(&self) -> io::Result<Dir> {
48        let path = self.inner.cache_dir();
49        fs::create_dir_all(path)?;
50        Dir::open_ambient_dir(path, ambient_authority())
51    }
52
53    /// Returns the project's config directory.
54    ///
55    /// This corresponds to [`directories::ProjectDirs::config_dir`].
56    pub fn config_dir(&self) -> io::Result<Dir> {
57        let path = self.inner.config_dir();
58        fs::create_dir_all(path)?;
59        Dir::open_ambient_dir(path, ambient_authority())
60    }
61
62    /// Returns the project's data directory.
63    ///
64    /// This corresponds to [`directories::ProjectDirs::data_dir`].
65    pub fn data_dir(&self) -> io::Result<Dir> {
66        let path = self.inner.data_dir();
67        fs::create_dir_all(path)?;
68        Dir::open_ambient_dir(path, ambient_authority())
69    }
70
71    /// Returns the project's local data directory.
72    ///
73    /// This corresponds to [`directories::ProjectDirs::data_local_dir`].
74    pub fn data_local_dir(&self) -> io::Result<Dir> {
75        let path = self.inner.data_local_dir();
76        fs::create_dir_all(path)?;
77        Dir::open_ambient_dir(path, ambient_authority())
78    }
79
80    /// Returns the project's runtime directory.
81    ///
82    /// This corresponds to [`directories::ProjectDirs::runtime_dir`].
83    pub fn runtime_dir(&self) -> io::Result<Dir> {
84        let path = self.inner.runtime_dir().ok_or_else(not_found)?;
85        fs::create_dir_all(path)?;
86        Dir::open_ambient_dir(path, ambient_authority())
87    }
88
89    /// Returns the project's state directory.
90    ///
91    /// This corresponds to [`directories::ProjectDirs::state_dir`].
92    pub fn state_dir(&self) -> io::Result<Option<Dir>> {
93        let Some(path) = self.inner.state_dir() else {
94            return Ok(None);
95        };
96        fs::create_dir_all(path)?;
97        Dir::open_ambient_dir(path, ambient_authority()).map(Some)
98    }
99}