Skip to content

Commit 88335fe

Browse files
committed
refactor
1 parent 010e3cc commit 88335fe

File tree

1 file changed

+46
-52
lines changed

1 file changed

+46
-52
lines changed

src/recipe.rs

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

19-
#[derive(Debug)]
20-
struct SystemMap {
21-
linux: bool,
22-
macos: bool,
23-
openbsd: bool,
24-
unix: bool,
25-
windows: bool,
26-
}
27-
28-
#[derive(Debug, Clone, Copy)]
19+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::EnumIter)]
2920
enum System {
3021
Linux,
3122
MacOS,
@@ -56,36 +47,35 @@ impl System {
5647
Unrecognized
5748
}
5849

59-
fn enabled(self, enabled: SystemMap, disabled: SystemMap) -> bool {
50+
fn is_specific_unix(self) -> bool {
6051
match self {
61-
System::Windows => {
62-
!disabled.windows
63-
&& (enabled.windows
64-
|| !(enabled.macos || enabled.linux || enabled.openbsd || enabled.unix))
65-
}
66-
System::MacOS => {
67-
!disabled.macos
68-
&& ((enabled.macos || enabled.unix)
69-
|| !(enabled.windows || enabled.linux || enabled.openbsd))
70-
}
71-
System::Linux => {
72-
!disabled.linux
73-
&& ((enabled.linux || enabled.unix)
74-
|| !(enabled.windows || enabled.macos || enabled.openbsd))
75-
}
76-
System::OpenBSD => {
77-
!disabled.openbsd
78-
&& ((enabled.openbsd || enabled.unix)
79-
|| !(enabled.windows || enabled.macos || enabled.linux))
80-
}
81-
System::Unix => {
82-
!disabled.unix
83-
&& (enabled.unix
84-
|| !(enabled.windows || enabled.macos || enabled.linux || enabled.openbsd))
85-
}
86-
System::Unrecognized => true,
52+
System::Linux => true,
53+
System::MacOS => true,
54+
System::OpenBSD => true,
55+
_ => false,
8756
}
8857
}
58+
59+
fn others(self) -> Vec<System> {
60+
use strum::IntoEnumIterator;
61+
System::iter()
62+
.filter(|system| {
63+
*system != self
64+
&& if system.is_specific_unix() {
65+
*system != System::Unix
66+
} else {
67+
true
68+
}
69+
})
70+
.collect()
71+
}
72+
73+
fn enabled(self, enabled: HashMap<System, bool>, disabled: HashMap<System, bool>) -> bool {
74+
let not_disabled = !disabled[&self];
75+
let explicitly_enabled = enabled[&self];
76+
let no_others_enabled = !self.others().iter().any(|system| enabled[system]);
77+
not_disabled && (explicitly_enabled || no_others_enabled)
78+
}
8979
}
9080

9181
/// A recipe, e.g. `foo: bar baz`
@@ -211,21 +201,25 @@ impl<'src, D> Recipe<'src, D> {
211201
return true;
212202
}
213203

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),
220-
};
221-
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),
228-
};
204+
let enabled: HashMap<System, bool> = [
205+
(System::Windows, windows.unwrap_or(false)),
206+
(System::MacOS, macos.unwrap_or(false)),
207+
(System::Linux, linux.unwrap_or(false)),
208+
(System::OpenBSD, openbsd.unwrap_or(false)),
209+
(System::Unix, unix.unwrap_or(false)),
210+
]
211+
.into_iter()
212+
.collect();
213+
214+
let disabled: HashMap<System, bool> = [
215+
(System::Windows, windows.is_some_and(bool::not)),
216+
(System::MacOS, macos.is_some_and(bool::not)),
217+
(System::Linux, linux.is_some_and(bool::not)),
218+
(System::OpenBSD, openbsd.is_some_and(bool::not)),
219+
(System::Unix, unix.is_some_and(bool::not)),
220+
]
221+
.into_iter()
222+
.collect();
229223

230224
System::current().enabled(enabled, disabled)
231225
}

0 commit comments

Comments
 (0)