komichi 2.2.0

Application tools for working with file-system paths
Documentation
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 {
                                                // UNIX         MAC
    fn get_cache_home(&self) -> Utf8PathBuf;    // /var/cache   /usr/local/cache
    fn get_config_home(&self) -> Utf8PathBuf;   // /etc/opt     /usr/local/etc
    fn get_data_home(&self) -> Utf8PathBuf;     // /var/opt     /usr/local/var
    fn get_log_home(&self) -> Utf8PathBuf;      // /var/log     /usr/local/var/log
    fn get_state_home(&self) -> Utf8PathBuf;    // /var/lib     /usr/local/var/lib
    fn get_opt_home(&self) -> Utf8PathBuf;      // /opt         /opt
}

/// Provide path locations for a given application-name as if the given
/// application-name was installed on the system (not installed in the user's
/// directory).
///
/// The return directory-paths, provided by this struct, are all made with the
/// assumption that the application will be installed in `/opt`
///
/// The convenience function
/// ['get_system_application_paths'](fn@crate::get_system_application_paths)
/// can also be used to instantiate this struct.
///
/// # Notes
/// * The term `system` is used to mean an application that is being run
///   as a 'server' or is a 'system wide' tool/application.
/// * The [`SystemDirectories`](struct@crate::system::SystemDirectories) struct
///   only provides the directory locations; it does NOT create or validate the
///   their existence.
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 }
    }
    /// Return the system-directory-path to the location of the application
    /// cache files:
    /// * Linux/Unix: `/var/cache/<app_name>`
    /// * Mac: `/usr/local/cache/<app_name>`
    ///
    /// This method does NOT create or validate the existence of the returned
    /// path.
    pub fn get_cache_home(&self) -> Utf8PathBuf {
        self.dirs.get_cache_home()
    }
    /// Return the system-directory-path to the location of the application
    /// config files.
    /// * Linux/Unix: `/etc/opt/<app_name>`
    /// * Mac: `/usr/local/etc/<app_name>`
    ///
    /// This method does NOT create or validate the existence of the returned
    /// path.
    pub fn get_config_home(&self) -> Utf8PathBuf {
        self.dirs.get_config_home()
    }
    /// Return the system-directory-path to the location of the application
    /// data files.
    /// * Linux/Unix: `/var/opt/<app_name>`
    /// * Mac: `/usr/local/var/<app_name>`
    ///
    /// This method does NOT create or validate the existence of the returned
    /// path.
    pub fn get_data_home(&self) -> Utf8PathBuf {
        self.dirs.get_data_home()
    }
    /// Return the system-directory-path to the location of the application
    /// log files.
    /// * Linux/Unix: `/var/log/<app_name>`
    /// * Mac: `/usr/local/var/log/<app_name>`
    ///
    /// This method does NOT create or validate the existence of the returned
    /// path.
    pub fn get_log_home(&self) -> Utf8PathBuf {
        self.dirs.get_log_home()
    }
    /// Return the system-directory-path to the location of the application
    /// state files.
    /// * Linux/Unix: `/var/lib/<app_name>`
    /// * Mac: `/usr/local/var/lib/<app_name>`
    ///
    /// This method does NOT create or validate the existence of the returned
    /// path.
    pub fn get_state_home(&self) -> Utf8PathBuf {
        self.dirs.get_state_home()
    }
    /// Return the system-directory-path to the location of the application
    /// installed files.
    /// * Linux/Unix: `/opt/<app_name>`
    /// * Mac: `/opt/<app_name>`
    ///
    /// This method does NOT create or validate the existence of the returned
    /// path.
    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);
    }
}