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
use std::{
borrow::Cow,
env::{self, VarError},
fmt,
};
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Os<'a> {
Android,
Bitrig,
CloudABI,
Dragonfly,
Emscripten,
FreeBSD,
Fuchsia,
Haiku,
#[allow(non_camel_case_types)]
iOs,
Linux,
MacOs,
NetBSD,
OpenBSD,
Redox,
Solaris,
Windows,
Other(Cow<'a, str>),
}
impl<'a> Os<'a> {
pub fn as_str(&self) -> &str {
match self {
Os::Android => "android",
Os::Bitrig => "bitrig",
Os::CloudABI => "cloudabi",
Os::Dragonfly => "dragonfly",
Os::Emscripten => "emscripten",
Os::FreeBSD => "freebsd",
Os::Fuchsia => "fuchsia",
Os::Haiku => "haiku",
Os::iOs => "ios",
Os::Linux => "linux",
Os::MacOs => "macos",
Os::NetBSD => "netbsd",
Os::OpenBSD => "openbsd",
Os::Redox => "redox",
Os::Solaris => "solaris",
Os::Windows => "windows",
Os::Other(s) => s,
}
}
fn from_str(os_name: impl Into<Cow<'a, str>>) -> Self {
let os_name = os_name.into();
match os_name.as_ref() {
"android" => Os::Android,
"bitrig" => Os::Bitrig,
"cloudabi" => Os::CloudABI,
"dragonfly" => Os::Dragonfly,
"emscripten" => Os::Emscripten,
"freebsd" => Os::FreeBSD,
"fuchsia" => Os::Fuchsia,
"haiku" => Os::Haiku,
"ios" => Os::iOs,
"linux" => Os::Linux,
"macos" => Os::MacOs,
"netbsd" => Os::NetBSD,
"openbsd" => Os::OpenBSD,
"redox" => Os::Redox,
"solaris" => Os::Solaris,
"windows" => Os::Windows,
_ => Os::Other(os_name),
}
}
pub fn target() -> Result<Self, VarError> {
env::var("CARGO_CFG_TARGET_OS").map(Self::from_str)
}
}
impl<'a> fmt::Display for Os<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}