lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

platform.rs (5095B)


      1 use std::fmt;
      2 use std::path::PathBuf;
      3 
      4 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
      5 pub enum RadrootsPlatform {
      6     Linux,
      7     Macos,
      8     Windows,
      9     Android,
     10     Ios,
     11 }
     12 
     13 impl RadrootsPlatform {
     14     #[must_use]
     15     #[cfg(target_os = "android")]
     16     pub fn current() -> Self {
     17         Self::Android
     18     }
     19 
     20     #[must_use]
     21     #[cfg(target_os = "ios")]
     22     pub fn current() -> Self {
     23         Self::Ios
     24     }
     25 
     26     #[must_use]
     27     #[cfg(target_os = "macos")]
     28     pub fn current() -> Self {
     29         Self::Macos
     30     }
     31 
     32     #[must_use]
     33     #[cfg(target_os = "windows")]
     34     pub fn current() -> Self {
     35         Self::Windows
     36     }
     37 
     38     #[must_use]
     39     #[cfg(all(
     40         not(target_os = "android"),
     41         not(target_os = "ios"),
     42         not(target_os = "macos"),
     43         not(target_os = "windows")
     44     ))]
     45     pub fn current() -> Self {
     46         Self::Linux
     47     }
     48 
     49     #[must_use]
     50     pub fn is_unix_like(self) -> bool {
     51         matches!(self, Self::Linux | Self::Macos)
     52     }
     53 }
     54 
     55 impl fmt::Display for RadrootsPlatform {
     56     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     57         f.write_str(match self {
     58             Self::Linux => "linux",
     59             Self::Macos => "macos",
     60             Self::Windows => "windows",
     61             Self::Android => "android",
     62             Self::Ios => "ios",
     63         })
     64     }
     65 }
     66 
     67 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
     68 pub enum RadrootsPathProfile {
     69     InteractiveUser,
     70     ServiceHost,
     71     RepoLocal,
     72     MobileNative,
     73 }
     74 
     75 impl fmt::Display for RadrootsPathProfile {
     76     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     77         f.write_str(match self {
     78             Self::InteractiveUser => "interactive_user",
     79             Self::ServiceHost => "service_host",
     80             Self::RepoLocal => "repo_local",
     81             Self::MobileNative => "mobile_native",
     82         })
     83     }
     84 }
     85 
     86 #[derive(Debug, Clone, Default, PartialEq, Eq)]
     87 pub struct RadrootsHostEnvironment {
     88     pub home_dir: Option<PathBuf>,
     89     pub appdata_dir: Option<PathBuf>,
     90     pub localappdata_dir: Option<PathBuf>,
     91     pub programdata_dir: Option<PathBuf>,
     92 }
     93 
     94 impl RadrootsHostEnvironment {
     95     #[must_use]
     96     pub fn from_current_process() -> Self {
     97         Self {
     98             home_dir: std::env::var_os("HOME").map(PathBuf::from),
     99             appdata_dir: std::env::var_os("APPDATA").map(PathBuf::from),
    100             localappdata_dir: std::env::var_os("LOCALAPPDATA").map(PathBuf::from),
    101             programdata_dir: std::env::var_os("ProgramData").map(PathBuf::from),
    102         }
    103     }
    104 }
    105 
    106 #[cfg(test)]
    107 mod tests {
    108     use std::path::PathBuf;
    109 
    110     use super::{RadrootsHostEnvironment, RadrootsPathProfile, RadrootsPlatform};
    111 
    112     #[test]
    113     fn current_matches_compiled_target_platform() {
    114         #[cfg(target_os = "android")]
    115         let expected = RadrootsPlatform::Android;
    116         #[cfg(target_os = "ios")]
    117         let expected = RadrootsPlatform::Ios;
    118         #[cfg(target_os = "macos")]
    119         let expected = RadrootsPlatform::Macos;
    120         #[cfg(target_os = "windows")]
    121         let expected = RadrootsPlatform::Windows;
    122         #[cfg(all(
    123             not(target_os = "android"),
    124             not(target_os = "ios"),
    125             not(target_os = "macos"),
    126             not(target_os = "windows")
    127         ))]
    128         let expected = RadrootsPlatform::Linux;
    129 
    130         assert_eq!(RadrootsPlatform::current(), expected);
    131     }
    132 
    133     #[test]
    134     fn unix_like_classification_is_explicit() {
    135         assert!(RadrootsPlatform::Linux.is_unix_like());
    136         assert!(RadrootsPlatform::Macos.is_unix_like());
    137         assert!(!RadrootsPlatform::Windows.is_unix_like());
    138         assert!(!RadrootsPlatform::Android.is_unix_like());
    139         assert!(!RadrootsPlatform::Ios.is_unix_like());
    140     }
    141 
    142     #[test]
    143     fn display_uses_canonical_labels() {
    144         assert_eq!(RadrootsPlatform::Linux.to_string(), "linux");
    145         assert_eq!(RadrootsPlatform::Macos.to_string(), "macos");
    146         assert_eq!(RadrootsPlatform::Windows.to_string(), "windows");
    147         assert_eq!(RadrootsPlatform::Android.to_string(), "android");
    148         assert_eq!(RadrootsPlatform::Ios.to_string(), "ios");
    149 
    150         assert_eq!(
    151             RadrootsPathProfile::InteractiveUser.to_string(),
    152             "interactive_user"
    153         );
    154         assert_eq!(RadrootsPathProfile::ServiceHost.to_string(), "service_host");
    155         assert_eq!(RadrootsPathProfile::RepoLocal.to_string(), "repo_local");
    156         assert_eq!(
    157             RadrootsPathProfile::MobileNative.to_string(),
    158             "mobile_native"
    159         );
    160     }
    161 
    162     #[test]
    163     fn host_environment_reads_current_process_variables() {
    164         let env = RadrootsHostEnvironment::from_current_process();
    165         assert_eq!(env.home_dir, std::env::var_os("HOME").map(PathBuf::from));
    166         assert_eq!(
    167             env.appdata_dir,
    168             std::env::var_os("APPDATA").map(PathBuf::from)
    169         );
    170         assert_eq!(
    171             env.localappdata_dir,
    172             std::env::var_os("LOCALAPPDATA").map(PathBuf::from)
    173         );
    174         assert_eq!(
    175             env.programdata_dir,
    176             std::env::var_os("ProgramData").map(PathBuf::from)
    177         );
    178     }
    179 }