lib

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

platform.rs (4569B)


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