1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::path::PathBuf;

/// 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.
///
/// ```
/// use etcetera::app_strategy::AppStrategy;
/// use etcetera::app_strategy::AppStrategyArgs;
/// use etcetera::app_strategy::Unix;
/// use std::path::Path;
///
/// let app_strategy = Unix::new(AppStrategyArgs {
///     top_level_domain: "org".to_string(),
///     author: "Bram Moolenar".to_string(),
///     app_name: "Vim".to_string(),
/// }).unwrap();
///
/// let home_dir = etcetera::home_dir().unwrap();
///
/// assert_eq!(
///     app_strategy.config_dir().strip_prefix(&home_dir),
///     Ok(Path::new(".vim/")
/// ));
/// assert_eq!(
///     app_strategy.data_dir().strip_prefix(&home_dir),
///     Ok(Path::new(".vim/data/")
/// ));
/// assert_eq!(
///     app_strategy.cache_dir().strip_prefix(&home_dir),
///     Ok(Path::new(".vim/cache/")
/// ));
/// ```
#[derive(Debug)]
pub struct Unix {
    // This is `.vim` in the above example.
    root_dir: PathBuf,
}

impl super::AppStrategy for Unix {
    type CreationError = crate::HomeDirError;

    fn new(args: super::AppStrategyArgs) -> Result<Self, Self::CreationError> {
        let mut root_dir = crate::home_dir()?;
        root_dir.push(format!(".{}", args.unixy_name()));

        Ok(Self { root_dir })
    }

    fn config_dir(&self) -> PathBuf {
        self.root_dir.clone()
    }

    fn data_dir(&self) -> PathBuf {
        self.root_dir.join("data/")
    }

    fn cache_dir(&self) -> PathBuf {
        self.root_dir.join("cache/")
    }
}