1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use anyhow::Result;

mod linux;
pub use crate::linux::Linux;
mod macos;
pub use crate::macos::MacOS;
mod windows;
pub use crate::windows::Windows;
mod android;
pub use crate::android::Android;
mod openbsd;
pub use crate::openbsd::OpenBSD;

#[cfg(target_os = "windows")]
mod winapi;

pub fn detect() -> Result<OsVersion> {
    if cfg!(target_os = "linux") {
        Ok(OsVersion::Linux(Linux::detect()?))
    } else if cfg!(target_os = "macos") {
        Ok(OsVersion::MacOS(MacOS::detect()?))
    } else if cfg!(target_os = "windows") {
        Ok(OsVersion::Windows(Windows::detect()?))
    } else if cfg!(target_os = "android") {
        Ok(OsVersion::Android(Android::detect()?))
    } else if cfg!(target_os = "openbsd") {
        Ok(OsVersion::OpenBSD(OpenBSD::detect()?))
    } else {
        Ok(OsVersion::Unknown)
    }
}

#[derive(Debug)]
#[non_exhaustive]
pub enum OsVersion {
    Linux(Linux),
    MacOS(MacOS),
    Windows(Windows),
    Android(Android),
    OpenBSD(OpenBSD),
    Unknown,
}

impl ToString for OsVersion {
    fn to_string(&self) -> String {
        match self {
            OsVersion::Linux(v) => v.to_string(),
            OsVersion::MacOS(v) => v.to_string(),
            OsVersion::Windows(v) => v.to_string(),
            OsVersion::Android(v) => v.to_string(),
            OsVersion::OpenBSD(v) => v.to_string(),
            OsVersion::Unknown => String::from("unknown"),
        }
    }
}