etcetera/app_strategy/unix.rs
1use std::path::{Path, PathBuf};
2
3use crate::HomeDirError;
4
5/// This strategy has no standard or official specification. It has arisen over time through hundreds of Unixy tools. Vim and Cargo are notable examples whose configuration/data/cache directory layouts are similar to those created by this strategy.
6///
7/// ```
8/// use etcetera::app_strategy::AppStrategy;
9/// use etcetera::app_strategy::AppStrategyArgs;
10/// use etcetera::app_strategy::Unix;
11/// use std::path::Path;
12///
13/// let app_strategy = Unix::new(AppStrategyArgs {
14/// top_level_domain: "org".to_string(),
15/// author: "Acme Corp".to_string(),
16/// app_name: "Frobnicator Plus".to_string(),
17/// }).unwrap();
18///
19/// let home_dir = etcetera::home_dir().unwrap();
20///
21/// assert_eq!(
22/// app_strategy.home_dir(),
23/// &home_dir
24/// );
25/// assert_eq!(
26/// app_strategy.config_dir().strip_prefix(&home_dir),
27/// Ok(Path::new(".frobnicator-plus/"))
28/// );
29/// assert_eq!(
30/// app_strategy.data_dir().strip_prefix(&home_dir),
31/// Ok(Path::new(".frobnicator-plus/data/"))
32/// );
33/// assert_eq!(
34/// app_strategy.cache_dir().strip_prefix(&home_dir),
35/// Ok(Path::new(".frobnicator-plus/cache/"))
36/// );
37/// assert_eq!(
38/// app_strategy.state_dir().unwrap().strip_prefix(&home_dir),
39/// Ok(Path::new(".frobnicator-plus/state/"))
40/// );
41/// assert_eq!(
42/// app_strategy.runtime_dir().unwrap().strip_prefix(&home_dir),
43/// Ok(Path::new(".frobnicator-plus/runtime/"))
44/// );
45/// ```
46#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
47pub struct Unix {
48 // This is `.frobnicator-plus` in the above example.
49 home_dir: PathBuf,
50 unixy_name: String,
51}
52
53impl Unix {
54 /// Create a new Unix AppStrategy
55 pub fn new(args: super::AppStrategyArgs) -> Result<Self, HomeDirError> {
56 Ok(Self {
57 home_dir: crate::home_dir()?,
58 unixy_name: format!(".{}", args.unixy_name()),
59 })
60 }
61}
62
63impl super::AppStrategy for Unix {
64 fn home_dir(&self) -> &Path {
65 &self.home_dir
66 }
67
68 fn config_dir(&self) -> PathBuf {
69 self.home_dir.join(&self.unixy_name)
70 }
71
72 fn data_dir(&self) -> PathBuf {
73 self.home_dir.join(&self.unixy_name).join("data/")
74 }
75
76 fn cache_dir(&self) -> PathBuf {
77 self.home_dir.join(&self.unixy_name).join("cache/")
78 }
79
80 fn state_dir(&self) -> Option<PathBuf> {
81 Some(self.home_dir.join(&self.unixy_name).join("state/"))
82 }
83
84 fn runtime_dir(&self) -> Option<PathBuf> {
85 Some(self.home_dir.join(&self.unixy_name).join("runtime/"))
86 }
87}