Skip to content

Commit 1bacd8c

Browse files
committed
Use System type
1 parent f124d73 commit 1bacd8c

File tree

2 files changed

+92
-72
lines changed

2 files changed

+92
-72
lines changed

src/attribute_set.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
use {super::*, std::collections};
22

3-
#[derive(Debug, Copy, Clone)]
4-
pub(crate) enum InvertedStatus {
5-
Normal,
6-
Inverted,
7-
}
8-
93
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
104
pub(crate) struct AttributeSet<'src>(BTreeSet<Attribute<'src>>);
115

@@ -18,25 +12,14 @@ impl<'src> AttributeSet<'src> {
1812
self.0.iter().any(|attr| attr.discriminant() == target)
1913
}
2014

21-
pub(crate) fn contains_invertible(
22-
&self,
23-
target: AttributeDiscriminant,
24-
) -> Option<InvertedStatus> {
25-
self.get(target).and_then(|attr| {
26-
Some(match attr {
27-
Attribute::Linux { enabled }
28-
| Attribute::Macos { enabled }
29-
| Attribute::Openbsd { enabled }
30-
| Attribute::Unix { enabled }
31-
| Attribute::Windows { enabled } => {
32-
if *enabled {
33-
InvertedStatus::Normal
34-
} else {
35-
InvertedStatus::Inverted
36-
}
37-
}
38-
_ => panic!("contains_invertible called with non-invertible attribute"),
39-
})
15+
pub(crate) fn contains_invertible(&self, target: AttributeDiscriminant) -> Option<bool> {
16+
self.get(target).map(|attr| match attr {
17+
Attribute::Linux { enabled }
18+
| Attribute::Macos { enabled }
19+
| Attribute::Openbsd { enabled }
20+
| Attribute::Unix { enabled }
21+
| Attribute::Windows { enabled } => *enabled,
22+
_ => panic!("contains_invertible called with non-invertible attribute"),
4023
})
4124
}
4225

src/recipe.rs

Lines changed: 84 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,76 @@ fn error_from_signal(recipe: &str, line_number: Option<usize>, exit_status: Exit
1616
}
1717
}
1818

19+
#[derive(Debug)]
20+
struct SystemMap {
21+
windows: bool,
22+
macos: bool,
23+
linux: bool,
24+
openbsd: bool,
25+
unix: bool,
26+
}
27+
28+
#[derive(Debug, Clone, Copy)]
29+
enum System {
30+
Windows,
31+
MacOS,
32+
Linux,
33+
OpenBSD,
34+
Unix,
35+
}
36+
37+
impl System {
38+
fn current() -> System {
39+
use System::*;
40+
if cfg!(target_os = "linux") {
41+
return Linux;
42+
}
43+
if cfg!(target_os = "openbsd") {
44+
return OpenBSD;
45+
}
46+
if cfg!(target_os = "macos") {
47+
return MacOS;
48+
}
49+
if cfg!(target_os = "windows") || cfg!(windows) {
50+
return Windows;
51+
}
52+
if cfg!(unix) {
53+
return Unix;
54+
}
55+
panic!("No recognized system");
56+
}
57+
58+
fn enabled(self, enabled: SystemMap, disabled: SystemMap) -> bool {
59+
match self {
60+
System::Windows => {
61+
!disabled.windows
62+
&& (enabled.windows
63+
|| !(enabled.macos || enabled.linux || enabled.openbsd || enabled.unix))
64+
}
65+
System::MacOS => {
66+
!disabled.macos
67+
&& ((enabled.macos || enabled.unix)
68+
|| !(enabled.windows || enabled.linux || enabled.openbsd))
69+
}
70+
System::Linux => {
71+
!disabled.linux
72+
&& ((enabled.linux || enabled.unix)
73+
|| !(enabled.windows || enabled.macos || enabled.openbsd))
74+
}
75+
System::OpenBSD => {
76+
!disabled.openbsd
77+
&& ((enabled.openbsd || enabled.unix)
78+
|| !(enabled.windows || enabled.macos || enabled.linux))
79+
}
80+
System::Unix => {
81+
!disabled.unix
82+
&& (enabled.unix
83+
|| !(enabled.windows || enabled.macos || enabled.linux || enabled.openbsd))
84+
}
85+
}
86+
}
87+
}
88+
1989
/// A recipe, e.g. `foo: bar baz`
2090
#[derive(PartialEq, Debug, Clone, Serialize)]
2191
pub(crate) struct Recipe<'src, D = Dependency<'src>> {
@@ -116,15 +186,7 @@ impl<'src, D> Recipe<'src, D> {
116186
}
117187

118188
pub(crate) fn enabled(&self) -> bool {
119-
use attribute_set::InvertedStatus;
120-
121-
struct Systems {
122-
linux: bool,
123-
macos: bool,
124-
openbsd: bool,
125-
unix: bool,
126-
windows: bool,
127-
}
189+
use std::ops::Not;
128190

129191
let linux = self
130192
.attributes
@@ -149,48 +211,23 @@ impl<'src, D> Recipe<'src, D> {
149211
return true;
150212
}
151213

152-
let systems = Systems {
153-
linux: matches!(linux, Some(InvertedStatus::Normal)),
154-
macos: matches!(macos, Some(InvertedStatus::Normal)),
155-
openbsd: matches!(openbsd, Some(InvertedStatus::Normal)),
156-
unix: matches!(unix, Some(InvertedStatus::Normal)),
157-
windows: matches!(windows, Some(InvertedStatus::Normal)),
214+
let enabled = SystemMap {
215+
windows: windows.unwrap_or(false),
216+
macos: macos.unwrap_or(false),
217+
linux: linux.unwrap_or(false),
218+
openbsd: openbsd.unwrap_or(false),
219+
unix: unix.unwrap_or(false),
158220
};
159221

160-
let disabled = Systems {
161-
linux: matches!(linux, Some(InvertedStatus::Inverted)),
162-
macos: matches!(macos, Some(InvertedStatus::Inverted)),
163-
openbsd: matches!(openbsd, Some(InvertedStatus::Inverted)),
164-
unix: matches!(unix, Some(InvertedStatus::Inverted)),
165-
windows: matches!(windows, Some(InvertedStatus::Inverted)),
222+
let disabled = SystemMap {
223+
linux: linux.is_some_and(bool::not),
224+
macos: macos.is_some_and(bool::not),
225+
openbsd: openbsd.is_some_and(bool::not),
226+
unix: unix.is_some_and(bool::not),
227+
windows: windows.is_some_and(bool::not),
166228
};
167229

168-
if cfg!(target_os = "linux") {
169-
return !(disabled.linux || disabled.unix)
170-
&& ((systems.linux || systems.unix)
171-
|| (!systems.openbsd && !systems.windows && !systems.macos));
172-
}
173-
174-
if cfg!(target_os = "openbsd") {
175-
return !disabled.openbsd
176-
&& (systems.openbsd || (!systems.windows && !systems.macos && !systems.linux));
177-
}
178-
179-
if cfg!(target_os = "windows") || cfg!(windows) {
180-
return !disabled.windows
181-
&& (systems.windows
182-
|| (!systems.openbsd && !systems.unix && !systems.macos && !systems.linux));
183-
}
184-
185-
if cfg!(target_os = "macos") {
186-
return !disabled.macos
187-
&& (systems.macos || (!systems.openbsd && !systems.windows && !systems.linux));
188-
}
189-
190-
if cfg!(unix) {
191-
return !(disabled.unix) && (systems.unix || !systems.windows);
192-
}
193-
false
230+
System::current().enabled(enabled, disabled)
194231
}
195232

196233
fn print_exit_message(&self) -> bool {

0 commit comments

Comments
 (0)