Skip to content

Commit

Permalink
AttributeSet
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak committed Sep 27, 2024
1 parent 1aa440c commit 9dc142a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>"),
Expand Down
47 changes: 47 additions & 0 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,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_set(input: BTreeSet<Attribute<'src>>) -> Self {
Self { inner: input }
}

pub(crate) fn contains(&self, attribute: &Attribute) -> bool {
self.inner.contains(attribute)
}

//pub(crate) fn ensure_validity(&self, valid_attributes:

/// 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::*;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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,
Expand Down
6 changes: 4 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ impl<'run, 'src> Parser<'run, 'src> {
} else if self.next_is(Identifier) {
match Keyword::from_lexeme(next.lexeme()) {
Some(Keyword::Alias) if self.next_are(&[Identifier, Identifier, ColonEquals]) => {
items.push(Item::Alias(self.parse_alias(take_attributes())?));
items.push(Item::Alias(
self.parse_alias(AttributeSet::from_set(take_attributes()))?,
));
}
Some(Keyword::Export) if self.next_are(&[Identifier, Identifier, ColonEquals]) => {
self.presume_keyword(Keyword::Export)?;
Expand Down Expand Up @@ -462,7 +464,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()?;
Expand Down

0 comments on commit 9dc142a

Please sign in to comment.