Skip to content

Commit

Permalink
not() attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak committed Sep 28, 2024
1 parent 70476be commit ed38795
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
38 changes: 22 additions & 16 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ pub(crate) enum Attribute<'src> {
Doc(Option<StringLiteral<'src>>),
Extension(StringLiteral<'src>),
Group(StringLiteral<'src>),
Linux,
Macos,
Linux { inverted: bool },
Macos { inverted: bool },
NoCd,
NoExitMessage,
NoQuiet,
PositionalArguments,
Private,
Script(Option<Interpreter<'src>>),
Unix,
Windows,
Unix { inverted: bool },
Windows { inverted: bool },
}

impl AttributeDiscriminant {
Expand Down Expand Up @@ -77,8 +77,8 @@ impl<'src> Attribute<'src> {
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::Linux => Self::Linux { inverted: false },
AttributeDiscriminant::Macos => Self::Macos { inverted: false },
AttributeDiscriminant::NoCd => Self::NoCd,
AttributeDiscriminant::NoExitMessage => Self::NoExitMessage,
AttributeDiscriminant::NoQuiet => Self::NoQuiet,
Expand All @@ -91,8 +91,8 @@ impl<'src> Attribute<'src> {
arguments: arguments.collect(),
})
}),
AttributeDiscriminant::Unix => Self::Unix,
AttributeDiscriminant::Windows => Self::Windows,
AttributeDiscriminant::Unix => Self::Unix { inverted: false },
AttributeDiscriminant::Windows => Self::Windows { inverted: false },
})
}

Expand All @@ -103,26 +103,32 @@ impl<'src> Attribute<'src> {

impl<'src> Display for Attribute<'src> {
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) => write!(f, "({argument})")?,
Self::Script(Some(shell)) => write!(f, "({shell})")?,
| Self::Group(argument) => write!(f, "{name}({argument})")?,
Self::Script(Some(shell)) => write!(f, "{name}({shell})")?,
Self::Linux { inverted }
| Self::Macos { inverted }
| Self::Unix { inverted }
| Self::Windows { inverted } => {
if *inverted {
write!(f, "not({name})")?
} else {
write!(f, "{name}")?
}
}
Self::Confirm(None)
| Self::Doc(None)
| Self::Linux
| Self::Macos
| Self::NoCd
| Self::NoExitMessage
| Self::NoQuiet
| Self::PositionalArguments
| Self::Private
| Self::Script(None)
| Self::Unix
| Self::Windows => {}
| Self::Script(None) => write!(f, "{name}")?,
}

Ok(())
Expand Down
16 changes: 12 additions & 4 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,18 @@ impl<'src, D> Recipe<'src, D> {
}

pub(crate) fn enabled(&self) -> bool {
let windows = self.attributes.contains(&Attribute::Windows);
let linux = self.attributes.contains(&Attribute::Linux);
let macos = self.attributes.contains(&Attribute::Macos);
let unix = self.attributes.contains(&Attribute::Unix);
let windows = self
.attributes
.contains(&Attribute::Windows { inverted: false });
let linux = self
.attributes
.contains(&Attribute::Linux { inverted: false });
let macos = self
.attributes
.contains(&Attribute::Macos { inverted: false });
let unix = self
.attributes
.contains(&Attribute::Unix { inverted: false });

(!windows && !linux && !macos && !unix)
|| (cfg!(target_os = "windows") && windows)
Expand Down

0 comments on commit ed38795

Please sign in to comment.