etcetera/app_strategy/apple.rs
1use crate::base_strategy::BaseStrategy;
2use crate::{HomeDirError, base_strategy};
3use std::path::{Path, PathBuf};
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::app_strategy::AppStrategy;
9/// use etcetera::app_strategy::AppStrategyArgs;
10/// use etcetera::app_strategy::Apple;
11/// use std::path::Path;
12///
13/// let app_strategy = Apple::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("Library/Preferences/org.acme-corp.Frobnicator-Plus/"))
28/// );
29/// assert_eq!(
30/// app_strategy.data_dir().strip_prefix(&home_dir),
31/// Ok(Path::new("Library/Application Support/org.acme-corp.Frobnicator-Plus/"))
32/// );
33/// assert_eq!(
34/// app_strategy.cache_dir().strip_prefix(&home_dir),
35/// Ok(Path::new("Library/Caches/org.acme-corp.Frobnicator-Plus/"))
36/// );
37/// assert_eq!(
38/// app_strategy.state_dir(),
39/// None
40/// );
41/// assert_eq!(
42/// app_strategy.runtime_dir(),
43/// None
44/// );
45/// ```
46#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
47pub struct Apple {
48 base_strategy: base_strategy::Apple,
49 bundle_id: String,
50}
51
52impl Apple {
53 /// Create a new Apple AppStrategy
54 pub fn new(args: super::AppStrategyArgs) -> Result<Self, HomeDirError> {
55 Ok(Self {
56 base_strategy: base_strategy::Apple::new()?,
57 bundle_id: args.bundle_id(),
58 })
59 }
60}
61
62impl super::AppStrategy for Apple {
63 fn home_dir(&self) -> &Path {
64 self.base_strategy.home_dir()
65 }
66
67 fn config_dir(&self) -> PathBuf {
68 self.base_strategy.config_dir().join(&self.bundle_id)
69 }
70
71 fn data_dir(&self) -> PathBuf {
72 self.base_strategy.data_dir().join(&self.bundle_id)
73 }
74
75 fn cache_dir(&self) -> PathBuf {
76 self.base_strategy.cache_dir().join(&self.bundle_id)
77 }
78
79 fn state_dir(&self) -> Option<PathBuf> {
80 None
81 }
82
83 fn runtime_dir(&self) -> Option<PathBuf> {
84 None
85 }
86}