-
Notifications
You must be signed in to change notification settings - Fork 503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invertible attributes #2393
base: master
Are you sure you want to change the base?
Invertible attributes #2393
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,76 @@ fn error_from_signal(recipe: &str, line_number: Option<usize>, exit_status: Exit | |
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct SystemMap { | ||
windows: bool, | ||
macos: bool, | ||
linux: bool, | ||
openbsd: bool, | ||
unix: bool, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
enum System { | ||
Windows, | ||
MacOS, | ||
Linux, | ||
OpenBSD, | ||
Unix, | ||
} | ||
|
||
impl System { | ||
fn current() -> System { | ||
use System::*; | ||
if cfg!(target_os = "linux") { | ||
return Linux; | ||
} | ||
if cfg!(target_os = "openbsd") { | ||
return OpenBSD; | ||
} | ||
if cfg!(target_os = "macos") { | ||
return MacOS; | ||
} | ||
if cfg!(target_os = "windows") || cfg!(windows) { | ||
return Windows; | ||
} | ||
if cfg!(unix) { | ||
return Unix; | ||
} | ||
panic!("No recognized system"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this can panic. We should have an |
||
} | ||
|
||
fn enabled(self, enabled: SystemMap, disabled: SystemMap) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need some kind of more extensive testing for this. It looks right to me, but it strikes me as very fragile, and easy to get wrong when extending.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, makes senses - I'm not not entirely happy with the fragility of this code either, but I don't have any ideas off the top of my head for how to make it more robust. Do you have any ideas for how the tests should be architected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One idea:
One idea for improving the structure would be to try to factor out this pattern:
Maybe with maps:
|
||
match self { | ||
System::Windows => { | ||
!disabled.windows | ||
&& (enabled.windows | ||
|| !(enabled.macos || enabled.linux || enabled.openbsd || enabled.unix)) | ||
} | ||
System::MacOS => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On MacOS, and other unix systems, a recipe with |
||
!disabled.macos | ||
&& ((enabled.macos || enabled.unix) | ||
|| !(enabled.windows || enabled.linux || enabled.openbsd)) | ||
} | ||
System::Linux => { | ||
!disabled.linux | ||
&& ((enabled.linux || enabled.unix) | ||
|| !(enabled.windows || enabled.macos || enabled.openbsd)) | ||
} | ||
System::OpenBSD => { | ||
!disabled.openbsd | ||
&& ((enabled.openbsd || enabled.unix) | ||
|| !(enabled.windows || enabled.macos || enabled.linux)) | ||
} | ||
System::Unix => { | ||
!disabled.unix | ||
&& (enabled.unix | ||
|| !(enabled.windows || enabled.macos || enabled.linux || enabled.openbsd)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// A recipe, e.g. `foo: bar baz` | ||
#[derive(PartialEq, Debug, Clone, Serialize)] | ||
pub(crate) struct Recipe<'src, D = Dependency<'src>> { | ||
|
@@ -116,19 +186,48 @@ impl<'src, D> Recipe<'src, D> { | |
} | ||
|
||
pub(crate) fn enabled(&self) -> bool { | ||
let linux = self.attributes.contains(AttributeDiscriminant::Linux); | ||
let macos = self.attributes.contains(AttributeDiscriminant::Macos); | ||
let openbsd = self.attributes.contains(AttributeDiscriminant::Openbsd); | ||
let unix = self.attributes.contains(AttributeDiscriminant::Unix); | ||
let windows = self.attributes.contains(AttributeDiscriminant::Windows); | ||
|
||
(!windows && !linux && !macos && !openbsd && !unix) | ||
|| (cfg!(target_os = "linux") && (linux || unix)) | ||
|| (cfg!(target_os = "macos") && (macos || unix)) | ||
|| (cfg!(target_os = "openbsd") && (openbsd || unix)) | ||
|| (cfg!(target_os = "windows") && windows) | ||
|| (cfg!(unix) && unix) | ||
|| (cfg!(windows) && windows) | ||
use std::ops::Not; | ||
|
||
let linux = self | ||
.attributes | ||
.contains_invertible(AttributeDiscriminant::Linux); | ||
let macos = self | ||
.attributes | ||
.contains_invertible(AttributeDiscriminant::Macos); | ||
let openbsd = self | ||
.attributes | ||
.contains_invertible(AttributeDiscriminant::Openbsd); | ||
let unix = self | ||
.attributes | ||
.contains_invertible(AttributeDiscriminant::Unix); | ||
let windows = self | ||
.attributes | ||
.contains_invertible(AttributeDiscriminant::Windows); | ||
|
||
if [linux, macos, openbsd, unix, windows] | ||
.into_iter() | ||
.all(|x| x.is_none()) | ||
{ | ||
return true; | ||
} | ||
|
||
let enabled = SystemMap { | ||
windows: windows.unwrap_or(false), | ||
macos: macos.unwrap_or(false), | ||
linux: linux.unwrap_or(false), | ||
openbsd: openbsd.unwrap_or(false), | ||
unix: unix.unwrap_or(false), | ||
}; | ||
|
||
let disabled = SystemMap { | ||
linux: linux.is_some_and(bool::not), | ||
macos: macos.is_some_and(bool::not), | ||
openbsd: openbsd.is_some_and(bool::not), | ||
unix: unix.is_some_and(bool::not), | ||
windows: windows.is_some_and(bool::not), | ||
}; | ||
|
||
System::current().enabled(enabled, disabled) | ||
} | ||
|
||
fn print_exit_message(&self) -> bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is probably cleaner than using mutable variables:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I think we should actually, since we don't have any invertable variables which aren't system variables, change the argument to
Attribute::new
to be !inverted, and withinAttribute::new
it's calledenabled
.