pub struct Windows { /* private fields */ }Expand description
This strategy follows Windows’ conventions. It seems that all Windows GUI apps, and some command-line ones follow this pattern. The specification is available here.
This initial example removes all the relevant environment variables to show the strategy’s use of the:
- (on Windows) SHGetKnownFolderPath API.
- (on non-Windows) Windows default directories.
use etcetera::app_strategy::AppStrategy;
use etcetera::app_strategy::AppStrategyArgs;
use etcetera::app_strategy::Windows;
use std::path::Path;
// Remove the environment variables that the strategy reads from.
unsafe {
std::env::remove_var("USERPROFILE");
std::env::remove_var("APPDATA");
std::env::remove_var("LOCALAPPDATA");
}
let app_strategy = Windows::new(AppStrategyArgs {
top_level_domain: "org".to_string(),
author: "Acme Corp".to_string(),
app_name: "Frobnicator Plus".to_string(),
}).unwrap();
let home_dir = etcetera::home_dir().unwrap();
assert_eq!(
app_strategy.home_dir(),
&home_dir
);
assert_eq!(
app_strategy.config_dir().strip_prefix(&home_dir),
Ok(Path::new("AppData/Roaming/Acme Corp/Frobnicator Plus/config"))
);
assert_eq!(
app_strategy.data_dir().strip_prefix(&home_dir),
Ok(Path::new("AppData/Roaming/Acme Corp/Frobnicator Plus/data"))
);
assert_eq!(
app_strategy.cache_dir().strip_prefix(&home_dir),
Ok(Path::new("AppData/Local/Acme Corp/Frobnicator Plus/cache"))
);
assert_eq!(
app_strategy.state_dir(),
None
);
assert_eq!(
app_strategy.runtime_dir(),
None
);This next example gives the environment variables values:
use etcetera::app_strategy::AppStrategy;
use etcetera::app_strategy::AppStrategyArgs;
use etcetera::app_strategy::Windows;
use std::path::Path;
let home_path = if cfg!(windows) {
"C:\\my_home_location\\".to_string()
} else {
etcetera::home_dir().unwrap().to_string_lossy().to_string()
};
let data_path = if cfg!(windows) {
"C:\\my_data_location\\"
} else {
"/my_data_location/"
};
let cache_path = if cfg!(windows) {
"C:\\my_cache_location\\"
} else {
"/my_cache_location/"
};
unsafe {
std::env::set_var("USERPROFILE", &home_path);
std::env::set_var("APPDATA", data_path);
std::env::set_var("LOCALAPPDATA", cache_path);
}
let app_strategy = Windows::new(AppStrategyArgs {
top_level_domain: "org".to_string(),
author: "Acme Corp".to_string(),
app_name: "Frobnicator Plus".to_string(),
}).unwrap();
assert_eq!(
app_strategy.home_dir(),
Path::new(&home_path)
);
assert_eq!(
app_strategy.config_dir(),
Path::new(&format!("{}/Acme Corp/Frobnicator Plus/config", data_path))
);
assert_eq!(
app_strategy.data_dir(),
Path::new(&format!("{}/Acme Corp/Frobnicator Plus/data", data_path))
);
assert_eq!(
app_strategy.cache_dir(),
Path::new(&format!("{}/Acme Corp/Frobnicator Plus/cache", cache_path))
);
assert_eq!(
app_strategy.state_dir(),
None
);
assert_eq!(
app_strategy.runtime_dir(),
None
);Implementations§
Source§impl Windows
impl Windows
Sourcepub fn new(args: AppStrategyArgs) -> Result<Self, HomeDirError>
pub fn new(args: AppStrategyArgs) -> Result<Self, HomeDirError>
Create a new Windows AppStrategy
Trait Implementations§
Source§impl AppStrategy for Windows
impl AppStrategy for Windows
Source§fn config_dir(&self) -> PathBuf
fn config_dir(&self) -> PathBuf
Gets the configuration directory for your application.
Source§fn runtime_dir(&self) -> Option<PathBuf>
fn runtime_dir(&self) -> Option<PathBuf>
Source§fn in_config_dir<P: AsRef<OsStr>>(&self, path: P) -> PathBuf
fn in_config_dir<P: AsRef<OsStr>>(&self, path: P) -> PathBuf
Constructs a path inside your application’s configuration directory to which a path of your choice has been appended.
Source§fn in_data_dir<P: AsRef<OsStr>>(&self, path: P) -> PathBuf
fn in_data_dir<P: AsRef<OsStr>>(&self, path: P) -> PathBuf
Constructs a path inside your application’s data directory to which a path of your choice has been appended.
Source§fn in_cache_dir<P: AsRef<OsStr>>(&self, path: P) -> PathBuf
fn in_cache_dir<P: AsRef<OsStr>>(&self, path: P) -> PathBuf
Constructs a path inside your application’s cache directory to which a path of your choice has been appended.
Source§impl Ord for Windows
impl Ord for Windows
Source§impl PartialOrd for Windows
impl PartialOrd for Windows
impl Eq for Windows
impl StructuralPartialEq for Windows
Auto Trait Implementations§
impl Freeze for Windows
impl RefUnwindSafe for Windows
impl Send for Windows
impl Sync for Windows
impl Unpin for Windows
impl UnwindSafe for Windows
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more