use camino::Utf8PathBuf;
#[cfg(target_os = "macos")]
mod mac;
#[cfg(target_os = "macos")]
use mac::SystemDirs;
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
mod unix;
#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "ios"))
))]
use unix::SystemDirs;
#[rustfmt::skip]
trait SystemHomeInfo {
fn get_cache_home(&self) -> Utf8PathBuf; fn get_config_home(&self) -> Utf8PathBuf; fn get_data_home(&self) -> Utf8PathBuf; fn get_log_home(&self) -> Utf8PathBuf; fn get_state_home(&self) -> Utf8PathBuf; fn get_opt_home(&self) -> Utf8PathBuf; }
pub struct SystemDirectories {
dirs: Box<dyn SystemHomeInfo>,
}
impl SystemDirectories {
pub fn new(app_name: &str) -> Self {
let dirs = Box::new(SystemDirs::new(app_name));
Self { dirs }
}
pub fn get_cache_home(&self) -> Utf8PathBuf {
self.dirs.get_cache_home()
}
pub fn get_config_home(&self) -> Utf8PathBuf {
self.dirs.get_config_home()
}
pub fn get_data_home(&self) -> Utf8PathBuf {
self.dirs.get_data_home()
}
pub fn get_log_home(&self) -> Utf8PathBuf {
self.dirs.get_log_home()
}
pub fn get_state_home(&self) -> Utf8PathBuf {
self.dirs.get_state_home()
}
pub fn get_opt_home(&self) -> Utf8PathBuf {
self.dirs.get_opt_home()
}
}
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_cache_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_cache_home().to_string();
let exp = "/var/cache/test_app".to_string();
assert_eq!(res, exp);
}
#[test]
fn test_get_config_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_config_home().to_string();
let exp = "/etc/opt/test_app".to_string();
assert_eq!(res, exp);
}
#[test]
fn test_get_data_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_data_home().to_string();
let exp = "/var/opt/test_app".to_string();
assert_eq!(res, exp);
}
#[test]
fn test_get_log_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_log_home().to_string();
let exp = "/var/log/test_app".to_string();
assert_eq!(res, exp);
}
#[test]
fn test_get_state_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_state_home().to_string();
let exp = "/var/lib/test_app".to_string();
assert_eq!(res, exp);
}
#[test]
fn test_get_opt_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_opt_home().to_string();
let exp = "/opt/test_app".to_string();
assert_eq!(res, exp);
}
}
#[cfg(target_os = "macos")]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_cache_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_cache_home().to_string();
let exp = "/usr/local/cache/test_app".to_string();
assert_eq!(res, exp);
}
#[test]
fn test_get_config_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_config_home().to_string();
let exp = "/usr/local/etc/test_app".to_string();
assert_eq!(res, exp);
}
#[test]
fn test_get_data_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_data_home().to_string();
let exp = "/usr/local/var/test_app".to_string();
assert_eq!(res, exp);
}
#[test]
fn test_get_log_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_log_home().to_string();
let exp = "/usr/local/var/log/test_app".to_string();
assert_eq!(res, exp);
}
#[test]
fn test_get_state_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_state_home().to_string();
let exp = "/usr/local/var/lib/test_app".to_string();
assert_eq!(res, exp);
}
#[test]
fn test_get_opt_home() {
let app_name = "test_app";
let sys = SystemDirectories::new(app_name);
let res = sys.get_opt_home().to_string();
let exp = "/opt/test_app".to_string();
assert_eq!(res, exp);
}
}