project_dirs/strategy/
unix.rs

1use std::path::Path;
2
3use crate::{FullProjectDirs, Project};
4
5/// Retrieve unix-style [`FullProjectDirs`]
6pub trait Unix {
7    /// Use custom prefix and parent path for the unix-style directories
8    fn unix_prefixed(&self, parent_path: &Path, prefix: &str) -> FullProjectDirs;
9
10    /// Same as unix_prefixed, but assumes prefix is "."
11    fn unix(&self, parent_path: &Path) -> FullProjectDirs {
12        self.unix_prefixed(parent_path, "")
13    }
14
15    /// Get path to the unix-style directories for the current working directory (PWD). Assumes
16    /// prefix is ".".
17    fn unix_pwd(&self) -> Result<FullProjectDirs, std::io::Error>;
18
19    /// Get path to the unix-style directories for the current user. Assumes prefix is ".".
20    fn unix_home(&self) -> Option<FullProjectDirs>;
21
22    /// Get path to the unix-style directories for the current binary. Assumes prefix is ".".
23    fn unix_binary(&self) -> Result<FullProjectDirs, std::io::Error>;
24}
25
26impl Unix for Project {
27    fn unix_prefixed(&self, parent_path: &Path, prefix: &str) -> FullProjectDirs {
28        let project_name = format!("{}{}", prefix, self.application_name);
29        let full_project_path = parent_path.join(project_name);
30
31        FullProjectDirs {
32            cache: full_project_path.join("cache"),
33            data: full_project_path.join("data"),
34            log: full_project_path.join("log"),
35            runtime: Some(full_project_path.join("tmp")),
36            state: full_project_path.join("state"),
37            bin: full_project_path.join("bin"),
38            config: full_project_path.clone(),
39            include: full_project_path.join("include"),
40            lib: full_project_path.join("lib"),
41            project_root: Some(full_project_path),
42        }
43    }
44
45    fn unix_pwd(&self) -> Result<FullProjectDirs, std::io::Error> {
46        std::env::current_dir().map(|path| self.unix_prefixed(&path, "."))
47    }
48
49    fn unix_home(&self) -> Option<FullProjectDirs> {
50        crate::dir_utils::home_dir().map(|path| self.unix_prefixed(&path, "."))
51    }
52
53    fn unix_binary(&self) -> Result<FullProjectDirs, std::io::Error> {
54        std::env::current_exe().map(|path| self.unix_prefixed(&path, "."))
55    }
56}