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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Utilities for querying OS information, such as version, name, and other
//! things.

use version::OsVersion;

pub mod os;

/// Metadata for a specific operating system.
///
/// Information for each OS in non-exhaustive. Fields may be added later and it
/// won't be considered breaking backwards compatibility.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum OsMeta {
    /// Apple macOS.
    #[cfg(target_os = "macos")]
    MacOs {
        /// The macOS release name, if known.
        release: Option<os::OsRelease>
    },

    /// Microsoft Windows.
    #[cfg(target_os = "windows")]
    Windows {
        /// The Windows release name, if known.
        release: Option<os::OsRelease>
    },

    /// Ubuntu Linux.
    #[cfg(target_os = "linux")]
    Ubuntu {
        /// The Ubuntu release name, if known.
        release: Option<os::ubuntu::OsRelease>
    },

    /// Some unknown Linux operating system.
    #[cfg(target_os = "linux")]
    UnknownLinux {},
}

/// Information about the host operating system.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OsInfo {
    /// Metadata for the host OS.
    pub meta: OsMeta,
    /// The operating system version.
    pub version: Option<OsVersion>,
}

impl OsInfo {
    #[cfg(target_os = "macos")]
    fn _get() -> OsInfo {
        let version = OsVersion::get();
        let release = version.and_then(os::OsRelease::new);
        OsInfo {
            meta: OsMeta::MacOs {
                release,
            },
            version,
        }
    }

    #[cfg(target_os = "windows")]
    fn _get() -> OsInfo {
        let version = OsVersion::get();
        let release = version.and_then(os::OsRelease::new);
        OsInfo {
            meta: OsMeta::Windows {
                release,
            },
            version,
        }
    }

    #[cfg(target_os = "linux")]
    fn _get() -> OsInfo {
        let version = OsVersion::get();
        OsInfo {
            meta: OsMeta::UnknownLinux {},
            version,
        }
    }

    /// Queries information about the host operating system.
    pub fn get() -> OsInfo {
        OsInfo::_get()
    }

    /// Returns the string representation of the operating system's release
    /// version name, if one is known.
    pub fn release_name_str(&self) -> Option<&'static str> {
        #[cfg(target_os = "linux")]
        match self.meta {
            OsMeta::Ubuntu { release, .. } => release.map(Into::into),
            OsMeta::UnknownLinux { .. } => None,
        }

        #[cfg(not(target_os = "linux"))]
        match self.meta {
            #[cfg(target_os = "macos")]
            OsMeta::MacOs { release, .. } => release.map(Into::into),
            #[cfg(target_os = "windows")]
            OsMeta::Windows { release, .. } => release.map(Into::into),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn get_os_info() {
        let info = OsInfo::get();

        #[cfg(target_os = "macos")]
        {
            assert!(info.version.is_some());
            match info.meta {
                OsMeta::MacOs { release, .. } => {
                    assert!(release.is_some());
                },
            }
        }
    }
}