Skip to main content

plugmem_host/
paths.rs

1//! Platform-aware per-user paths shared by the host and its wrappers.
2
3use directories::ProjectDirs;
4use std::path::PathBuf;
5
6const CONFIG_FILE: &str = "config.toml";
7const DATABASE_FILE: &str = "memory.plugmem";
8
9fn project_dirs() -> Option<ProjectDirs> {
10    // Empty qualifier and organization keep the user-facing project name
11    // simply `plugmem` on every supported platform.
12    ProjectDirs::from("", "", "plugmem")
13}
14
15/// The platform's conventional per-user config directory.
16pub fn default_config_dir() -> Option<PathBuf> {
17    project_dirs().map(|dirs| dirs.config_dir().to_path_buf())
18}
19
20/// The platform's conventional per-user data directory.
21pub fn default_data_dir() -> Option<PathBuf> {
22    // The database is local state, so Windows uses LocalAppData rather than
23    // the roaming data directory. On Linux and macOS this is the conventional
24    // project data directory.
25    project_dirs().map(|dirs| dirs.data_local_dir().to_path_buf())
26}
27
28/// The default config file path, when a platform user directory is available.
29pub fn default_config_path() -> Option<PathBuf> {
30    default_config_dir().map(|dir| dir.join(CONFIG_FILE))
31}
32
33/// The default persistent per-user database path.
34pub fn default_database_path() -> Option<PathBuf> {
35    default_data_dir().map(|dir| dir.join(DATABASE_FILE))
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn resolved_paths_have_stable_app_names() {
44        if let Some(path) = default_config_path() {
45            assert_eq!(
46                path.file_name().and_then(|name| name.to_str()),
47                Some(CONFIG_FILE)
48            );
49            assert!(path.to_string_lossy().contains("plugmem"));
50        }
51        if let Some(path) = default_database_path() {
52            assert_eq!(
53                path.file_name().and_then(|name| name.to_str()),
54                Some(DATABASE_FILE)
55            );
56            assert!(path.to_string_lossy().contains("plugmem"));
57        }
58    }
59}