etcetera/base_strategy/
apple.rs

1use std::path::{Path, PathBuf};
2
3use crate::HomeDirError;
4
5/// This is the strategy created by Apple for use on macOS and iOS devices. It is always used by GUI apps on macOS, and is sometimes used by command-line applications there too. iOS only has GUIs, so all iOS applications follow this strategy. The specification is available [here](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW1).
6///
7/// ```
8/// use etcetera::base_strategy::Apple;
9/// use etcetera::base_strategy::BaseStrategy;
10/// use std::path::Path;
11///
12/// let base_strategy = Apple::new().unwrap();
13///
14/// let home_dir = etcetera::home_dir().unwrap();
15///
16/// assert_eq!(
17///     base_strategy.home_dir(),
18///     &home_dir
19/// );
20/// assert_eq!(
21///     base_strategy.config_dir().strip_prefix(&home_dir),
22///     Ok(Path::new("Library/Preferences/"))
23/// );
24/// assert_eq!(
25///     base_strategy.data_dir().strip_prefix(&home_dir),
26///     Ok(Path::new("Library/Application Support/"))
27/// );
28/// assert_eq!(
29///     base_strategy.cache_dir().strip_prefix(&home_dir),
30///     Ok(Path::new("Library/Caches/"))
31/// );
32/// assert_eq!(
33///     base_strategy.state_dir(),
34///     None
35/// );
36/// assert_eq!(
37///     base_strategy.runtime_dir(),
38///     None
39/// );
40/// ```
41#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
42pub struct Apple {
43    home_dir: PathBuf,
44}
45impl Apple {
46    /// Create a new Apple BaseStrategy
47    pub fn new() -> Result<Self, HomeDirError> {
48        Ok(Self {
49            home_dir: crate::home_dir()?,
50        })
51    }
52}
53
54impl super::BaseStrategy for Apple {
55    fn home_dir(&self) -> &Path {
56        &self.home_dir
57    }
58
59    fn config_dir(&self) -> PathBuf {
60        self.home_dir.join("Library/Preferences/")
61    }
62
63    fn data_dir(&self) -> PathBuf {
64        self.home_dir.join("Library/Application Support/")
65    }
66
67    fn cache_dir(&self) -> PathBuf {
68        self.home_dir.join("Library/Caches/")
69    }
70
71    fn state_dir(&self) -> Option<PathBuf> {
72        None
73    }
74
75    fn runtime_dir(&self) -> Option<PathBuf> {
76        None
77    }
78}