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::base_strategy::BaseStrategy;
use etcetera::base_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 base_strategy = Windows::new().unwrap();
let home_dir = etcetera::home_dir().unwrap();
assert_eq!(
base_strategy.home_dir(),
&home_dir
);
assert_eq!(
base_strategy.config_dir().strip_prefix(&home_dir),
Ok(Path::new("AppData/Roaming/"))
);
assert_eq!(
base_strategy.data_dir().strip_prefix(&home_dir),
Ok(Path::new("AppData/Roaming/"))
);
assert_eq!(
base_strategy.cache_dir().strip_prefix(&home_dir),
Ok(Path::new("AppData/Local/"))
);
assert_eq!(
base_strategy.state_dir(),
None
);
assert_eq!(
base_strategy.runtime_dir(),
None
);This next example gives the environment variables values:
use etcetera::base_strategy::BaseStrategy;
use etcetera::base_strategy::Windows;
use std::path::Path;
let home_path = if cfg!(windows) {
"C:\\foo\\".to_string()
} else {
etcetera::home_dir().unwrap().to_string_lossy().to_string()
};
let data_path = if cfg!(windows) {
"C:\\bar\\"
} else {
"/bar/"
};
let cache_path = if cfg!(windows) {
"C:\\baz\\"
} else {
"/baz/"
};
unsafe {
std::env::set_var("USERPROFILE", &home_path);
std::env::set_var("APPDATA", data_path);
std::env::set_var("LOCALAPPDATA", cache_path);
}
let base_strategy = Windows::new().unwrap();
assert_eq!(
base_strategy.home_dir(),
Path::new(&home_path)
);
assert_eq!(
base_strategy.config_dir(),
Path::new(data_path)
);
assert_eq!(
base_strategy.data_dir(),
Path::new(data_path)
);
assert_eq!(
base_strategy.cache_dir(),
Path::new(cache_path)
);
assert_eq!(
base_strategy.state_dir(),
None
);
assert_eq!(
base_strategy.runtime_dir(),
None
);Implementations§
Trait Implementations§
Source§impl BaseStrategy for Windows
impl BaseStrategy for Windows
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