Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 47 additions & 31 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ pub(crate) enum Attribute<'src> {
Doc(Option<StringLiteral<'src>>),
Extension(StringLiteral<'src>),
Group(StringLiteral<'src>),
Linux,
Macos,
Linux { enabled: bool },
Macos { enabled: bool },
NoCd,
NoExitMessage,
NoQuiet,
Openbsd,
Openbsd { enabled: bool },
PositionalArguments,
Private,
Script(Option<Interpreter<'src>>),
Unix,
Windows,
Unix { enabled: bool },
Windows { enabled: bool },
WorkingDirectory(StringLiteral<'src>),
}

Expand Down Expand Up @@ -51,6 +51,7 @@ impl<'src> Attribute<'src> {
pub(crate) fn new(
name: Name<'src>,
arguments: Vec<StringLiteral<'src>>,
enabled: bool,
) -> CompileResult<'src, Self> {
let discriminant = name
.lexeme()
Expand All @@ -75,29 +76,38 @@ impl<'src> Attribute<'src> {
);
}

Ok(match discriminant {
AttributeDiscriminant::Confirm => Self::Confirm(arguments.into_iter().next()),
AttributeDiscriminant::Doc => Self::Doc(arguments.into_iter().next()),
AttributeDiscriminant::Extension => Self::Extension(arguments.into_iter().next().unwrap()),
AttributeDiscriminant::Group => Self::Group(arguments.into_iter().next().unwrap()),
AttributeDiscriminant::Linux => Self::Linux,
AttributeDiscriminant::Macos => Self::Macos,
AttributeDiscriminant::NoCd => Self::NoCd,
AttributeDiscriminant::NoExitMessage => Self::NoExitMessage,
AttributeDiscriminant::NoQuiet => Self::NoQuiet,
AttributeDiscriminant::Openbsd => Self::Openbsd,
AttributeDiscriminant::PositionalArguments => Self::PositionalArguments,
AttributeDiscriminant::Private => Self::Private,
AttributeDiscriminant::Script => Self::Script({
Ok(match (enabled, discriminant) {
(enabled, AttributeDiscriminant::Linux) => Self::Linux { enabled },
(enabled, AttributeDiscriminant::Macos) => Self::Macos { enabled },
(enabled, AttributeDiscriminant::Unix) => Self::Unix { enabled },
(enabled, AttributeDiscriminant::Windows) => Self::Windows { enabled },
(enabled, AttributeDiscriminant::Openbsd) => Self::Openbsd { enabled },

(false, _attr) => {
return Err(name.error(CompileErrorKind::InvalidInvertedAttribute {
attr_name: name.lexeme(),
}))
}

(true, AttributeDiscriminant::Confirm) => Self::Confirm(arguments.into_iter().next()),
(true, AttributeDiscriminant::Doc) => Self::Doc(arguments.into_iter().next()),
(true, AttributeDiscriminant::Extension) => {
Self::Extension(arguments.into_iter().next().unwrap())
}
(true, AttributeDiscriminant::Group) => Self::Group(arguments.into_iter().next().unwrap()),
(true, AttributeDiscriminant::NoCd) => Self::NoCd,
(true, AttributeDiscriminant::NoExitMessage) => Self::NoExitMessage,
(true, AttributeDiscriminant::NoQuiet) => Self::NoQuiet,
(true, AttributeDiscriminant::PositionalArguments) => Self::PositionalArguments,
(true, AttributeDiscriminant::Private) => Self::Private,
(true, AttributeDiscriminant::Script) => Self::Script({
let mut arguments = arguments.into_iter();
arguments.next().map(|command| Interpreter {
command,
arguments: arguments.collect(),
})
}),
AttributeDiscriminant::Unix => Self::Unix,
AttributeDiscriminant::Windows => Self::Windows,
AttributeDiscriminant::WorkingDirectory => {
(true, AttributeDiscriminant::WorkingDirectory) => {
Self::WorkingDirectory(arguments.into_iter().next().unwrap())
}
})
Expand All @@ -118,28 +128,34 @@ impl<'src> Attribute<'src> {

impl Display for Attribute<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.name())?;
let name = self.name();

match self {
Self::Confirm(Some(argument))
| Self::Doc(Some(argument))
| Self::Extension(argument)
| Self::Group(argument)
| Self::WorkingDirectory(argument) => write!(f, "({argument})")?,
Self::Script(Some(shell)) => write!(f, "({shell})")?,
| Self::WorkingDirectory(argument) => write!(f, "{name}({argument})")?,
Self::Script(Some(shell)) => write!(f, "{name}({shell})")?,
Self::Linux { enabled }
| Self::Macos { enabled }
| Self::Unix { enabled }
| Self::Openbsd { enabled }
| Self::Windows { enabled } => {
if *enabled {
write!(f, "{name}")?;
} else {
write!(f, "not({name})")?;
}
}
Self::Confirm(None)
| Self::Doc(None)
| Self::Linux
| Self::Macos
| Self::NoCd
| Self::NoExitMessage
| Self::NoQuiet
| Self::Openbsd
| Self::PositionalArguments
| Self::Private
| Self::Script(None)
| Self::Unix
| Self::Windows => {}
| Self::Script(None) => write!(f, "{name}")?,
}

Ok(())
Expand Down
13 changes: 13 additions & 0 deletions src/attribute_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ impl<'src> AttributeSet<'src> {
self.0.iter().any(|attr| attr.discriminant() == target)
}

pub(crate) fn contains_invertible(&self, target: AttributeDiscriminant) -> Option<bool> {
self.get(target).and_then(|attr| {
Some(match attr {
Attribute::Linux { enabled }
| Attribute::Macos { enabled }
| Attribute::Openbsd { enabled }
| Attribute::Unix { enabled }
| Attribute::Windows { enabled } => enabled,
_ => panic!("contains_invertible called with non-invertible attribute"),
})
})
}

pub(crate) fn get(&self, discriminant: AttributeDiscriminant) -> Option<&Attribute<'src>> {
self
.0
Expand Down
3 changes: 3 additions & 0 deletions src/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ impl Display for CompileError<'_> {
_ => character.escape_default().collect(),
}
),
InvalidInvertedAttribute { attr_name } => {
write!(f, "{attr_name} cannot be inverted with `not()`")
}
MismatchedClosingDelimiter {
open,
open_line,
Expand Down
3 changes: 3 additions & 0 deletions src/compile_error_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub(crate) enum CompileErrorKind<'src> {
InvalidEscapeSequence {
character: char,
},
InvalidInvertedAttribute {
attr_name: &'src str,
},
MismatchedClosingDelimiter {
close: Delimiter,
open: Delimiter,
Expand Down
25 changes: 23 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,17 @@ impl<'run, 'src> Parser<'run, 'src> {
token.get_or_insert(bracket);

loop {
let name = self.parse_name()?;
let (name, inverted) = {
Copy link
Owner

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:

Suggested change
let (name, inverted) = {
let (name, inverted) = {
let name = self.parse_name()?;
if name.lexeme() == "not" {
self.expect(ParenL)?;
let name = self.parse_name()?;
(name, true)
} else {
(name, false)
}
}

Copy link
Owner

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 within Attribute::new it's called enabled.

let name = self.parse_name()?;
if name.lexeme() == "not" {
self.expect(ParenL)?;
let name = self.parse_name()?;
self.expect(ParenR)?;
(name, true)
} else {
(name, false)
}
};

let mut arguments = Vec::new();

Expand All @@ -1152,7 +1162,7 @@ impl<'run, 'src> Parser<'run, 'src> {
self.expect(ParenR)?;
}

let attribute = Attribute::new(name, arguments)?;
let attribute = Attribute::new(name, arguments, !inverted)?;

let first = attributes.get(&attribute).or_else(|| {
if attribute.repeatable() {
Expand Down Expand Up @@ -2668,6 +2678,17 @@ mod tests {
kind: UnknownAttribute { attribute: "unknown" },
}

error! {
name: invalid_invertable_attribute,
input: "[not(private)]\nsome_recipe:\n @exit 3",
offset: 5,
line: 0,
column: 5,
width: 7,
kind: InvalidInvertedAttribute { attr_name: "private" },

}

error! {
name: set_unknown,
input: "set shall := []",
Expand Down
122 changes: 109 additions & 13 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@ fn error_from_signal(recipe: &str, line_number: Option<usize>, exit_status: Exit
}
}

#[derive(Debug, Clone, Copy)]
enum System {
Windows,
MacOS,
Linux,
OpenBSD,
Unix,
}

impl System {
fn current() -> &'static [System] {
use System::*;
if cfg!(target_os = "linux") {
return &[Linux, Unix];
}
if cfg!(target_os = "openbsd") {
&[OpenBSD, Unix];
}
if cfg!(target_os = "macos") {
&[MacOS, Unix];
}
if cfg!(target_os = "windows") || cfg!(windows) {
return &[Windows];
}
if cfg!(unix) {
return &[Unix];
}

&[]
}
}

/// A recipe, e.g. `foo: bar baz`
#[derive(PartialEq, Debug, Clone, Serialize)]
pub(crate) struct Recipe<'src, D = Dependency<'src>> {
Expand Down Expand Up @@ -116,19 +148,83 @@ 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)
struct Systems {
linux: bool,
macos: bool,
openbsd: bool,
unix: bool,
windows: bool,
}

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 current = System::current();

/*
let systems = Systems {
linux: matches!(linux, Some(InvertedStatus::Normal)),
macos: matches!(macos, Some(InvertedStatus::Normal)),
openbsd: matches!(openbsd, Some(InvertedStatus::Normal)),
unix: matches!(unix, Some(InvertedStatus::Normal)),
windows: matches!(windows, Some(InvertedStatus::Normal)),
};

let disabled = Systems {
linux: matches!(linux, Some(InvertedStatus::Inverted)),
macos: matches!(macos, Some(InvertedStatus::Inverted)),
openbsd: matches!(openbsd, Some(InvertedStatus::Inverted)),
unix: matches!(unix, Some(InvertedStatus::Inverted)),
windows: matches!(windows, Some(InvertedStatus::Inverted)),
};

if cfg!(target_os = "linux") {
return !(disabled.linux || disabled.unix)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these unix checks redundant with the ones below? I notice that the other Unix OS's don't have the same checks.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this maybe all gets easier if we have a System enum which represents the current system:

enum System {
  Windows,
  Linux,
  Unix,
  ...
}

impl System {
  fn current() -> &'static [System] {
    if cfg(target_os = "linux") {
       &[Self::Linux, Self::Unix]
    }
  }
}

And then we can handle all these checks in the same way.

&& ((systems.linux || systems.unix)
|| (!systems.openbsd && !systems.windows && !systems.macos));
}

if cfg!(target_os = "openbsd") {
return !disabled.openbsd
&& (systems.openbsd || (!systems.windows && !systems.macos && !systems.linux));
}

if cfg!(target_os = "windows") || cfg!(windows) {
return !disabled.windows
&& (systems.windows
|| (!systems.openbsd && !systems.unix && !systems.macos && !systems.linux));
}

if cfg!(target_os = "macos") {
return !disabled.macos
&& (systems.macos || (!systems.openbsd && !systems.windows && !systems.linux));
}

if cfg!(unix) {
return !(disabled.unix) && (systems.unix || !systems.windows);
}
*/
false
}

fn print_exit_message(&self) -> bool {
Expand Down
Loading
Loading