1use crate::Error;
2use std::fmt;
3use std::fmt::Display;
4use std::ops::Deref;
5use std::str::FromStr;
6
7#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
8pub struct Os(pub(crate) target_lexicon::OperatingSystem);
9
10impl Os {
11 pub fn new(os: target_lexicon::OperatingSystem) -> Self {
12 Self(os)
13 }
14
15 pub fn from_env() -> Self {
16 Self(target_lexicon::HOST.operating_system)
17 }
18
19 pub fn is_windows(&self) -> bool {
20 matches!(self.0, target_lexicon::OperatingSystem::Windows)
21 }
22
23 pub fn is_emscripten(&self) -> bool {
24 matches!(self.0, target_lexicon::OperatingSystem::Emscripten)
25 }
26
27 pub fn is_macos(&self) -> bool {
28 matches!(self.0, target_lexicon::OperatingSystem::Darwin(_))
29 }
30
31 pub fn supports(&self, other: Self) -> bool {
33 if other.is_emscripten() {
35 return true;
36 }
37
38 *self == other
40 }
41}
42
43impl Display for Os {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 match &**self {
46 target_lexicon::OperatingSystem::Darwin(_) => write!(f, "macos"),
47 inner => write!(f, "{inner}"),
48 }
49 }
50}
51
52impl FromStr for Os {
53 type Err = Error;
54
55 fn from_str(s: &str) -> Result<Self, Self::Err> {
56 let inner = match s {
57 "macos" => target_lexicon::OperatingSystem::Darwin(None),
58 _ => target_lexicon::OperatingSystem::from_str(s)
59 .map_err(|()| Error::UnknownOs(s.to_string()))?,
60 };
61 if matches!(inner, target_lexicon::OperatingSystem::Unknown) {
62 return Err(Error::UnknownOs(s.to_string()));
63 }
64 Ok(Self(inner))
65 }
66}
67
68impl Deref for Os {
69 type Target = target_lexicon::OperatingSystem;
70
71 fn deref(&self) -> &Self::Target {
72 &self.0
73 }
74}
75
76impl From<&uv_platform_tags::Os> for Os {
77 fn from(value: &uv_platform_tags::Os) -> Self {
78 match value {
79 uv_platform_tags::Os::Dragonfly { .. } => {
80 Self::new(target_lexicon::OperatingSystem::Dragonfly)
81 }
82 uv_platform_tags::Os::FreeBsd { .. } => {
83 Self::new(target_lexicon::OperatingSystem::Freebsd)
84 }
85 uv_platform_tags::Os::Haiku { .. } => Self::new(target_lexicon::OperatingSystem::Haiku),
86 uv_platform_tags::Os::Illumos { .. } => {
87 Self::new(target_lexicon::OperatingSystem::Illumos)
88 }
89 uv_platform_tags::Os::Macos { .. } => {
90 Self::new(target_lexicon::OperatingSystem::Darwin(None))
91 }
92 uv_platform_tags::Os::Manylinux { .. }
93 | uv_platform_tags::Os::Musllinux { .. }
94 | uv_platform_tags::Os::Android { .. } => {
95 Self::new(target_lexicon::OperatingSystem::Linux)
96 }
97 uv_platform_tags::Os::NetBsd { .. } => {
98 Self::new(target_lexicon::OperatingSystem::Netbsd)
99 }
100 uv_platform_tags::Os::OpenBsd { .. } => {
101 Self::new(target_lexicon::OperatingSystem::Openbsd)
102 }
103 uv_platform_tags::Os::Windows => Self::new(target_lexicon::OperatingSystem::Windows),
104 uv_platform_tags::Os::Pyodide { .. } => {
105 Self::new(target_lexicon::OperatingSystem::Emscripten)
106 }
107 uv_platform_tags::Os::Ios { .. } => {
108 Self::new(target_lexicon::OperatingSystem::IOS(None))
109 }
110 }
111 }
112}