AttributeSet

This commit is contained in:
Greg Shuflin 2024-07-15 03:36:01 -07:00
parent d5ebc9515e
commit 28e8ef84e1
4 changed files with 50 additions and 3 deletions

View File

@ -3,7 +3,7 @@ use super::*;
/// An alias, e.g. `name := target`
#[derive(Debug, PartialEq, Clone, Serialize)]
pub(crate) struct Alias<'src, T = Rc<Recipe<'src>>> {
pub(crate) attributes: BTreeSet<Attribute<'src>>,
pub(crate) attributes: AttributeSet<'src>,
pub(crate) name: Name<'src>,
#[serde(
bound(serialize = "T: Keyed<'src>"),

View File

@ -120,6 +120,53 @@ impl<'src> Display for Attribute<'src> {
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct AttributeSet<'src> {
inner: BTreeSet<Attribute<'src>>,
}
impl<'src> Serialize for AttributeSet<'src> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.inner.serialize(serializer)
}
}
impl<'src> AttributeSet<'src> {
pub(crate) fn empty() -> Self {
Self {
inner: BTreeSet::new(),
}
}
pub(crate) fn from_map<T>(input: BTreeMap<Attribute<'src>, T>) -> Self {
Self {
inner: input.into_keys().collect(),
}
}
pub(crate) fn contains(&self, attribute: &Attribute) -> bool {
self.inner.contains(attribute)
}
/// Get the names of all Group attributes defined in this attribute set
pub(crate) fn groups(&self) -> Vec<&StringLiteral<'src>> {
self
.inner
.iter()
.filter_map(|attr| {
if let Attribute::Group(name) = attr {
Some(name)
} else {
None
}
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -22,7 +22,7 @@
pub(crate) use {
crate::{
alias::Alias, analyzer::Analyzer, argument_parser::ArgumentParser, assignment::Assignment,
assignment_resolver::AssignmentResolver, ast::Ast, attribute::Attribute, binding::Binding,
assignment_resolver::AssignmentResolver, ast::Ast, attribute::{Attribute, AttributeSet}, binding::Binding,
color::Color, color_display::ColorDisplay, command_color::CommandColor,
command_ext::CommandExt, compilation::Compilation, compile_error::CompileError,
compile_error_kind::CompileErrorKind, compiler::Compiler, condition::Condition,

View File

@ -460,7 +460,7 @@ impl<'run, 'src> Parser<'run, 'src> {
/// Parse an alias, e.g `alias name := target`
fn parse_alias(
&mut self,
attributes: BTreeSet<Attribute<'src>>,
attributes: AttributeSet<'src>,
) -> CompileResult<'src, Alias<'src, Name<'src>>> {
self.presume_keyword(Keyword::Alias)?;
let name = self.parse_name()?;