From 28e8ef84e1dd0955b15726b20e623f24b1170067 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 15 Jul 2024 03:36:01 -0700 Subject: [PATCH] AttributeSet --- src/alias.rs | 2 +- src/attribute.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- src/parser.rs | 2 +- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/alias.rs b/src/alias.rs index 286cefd..2f7072a 100644 --- a/src/alias.rs +++ b/src/alias.rs @@ -3,7 +3,7 @@ use super::*; /// An alias, e.g. `name := target` #[derive(Debug, PartialEq, Clone, Serialize)] pub(crate) struct Alias<'src, T = Rc>> { - pub(crate) attributes: BTreeSet>, + pub(crate) attributes: AttributeSet<'src>, pub(crate) name: Name<'src>, #[serde( bound(serialize = "T: Keyed<'src>"), diff --git a/src/attribute.rs b/src/attribute.rs index 5176364..4368b51 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -120,6 +120,53 @@ impl<'src> Display for Attribute<'src> { } } +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct AttributeSet<'src> { + inner: BTreeSet>, +} + +impl<'src> Serialize for AttributeSet<'src> { + fn serialize(&self, serializer: S) -> Result + 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(input: BTreeMap, 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::*; diff --git a/src/lib.rs b/src/lib.rs index 4245a15..c1dd5b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, diff --git a/src/parser.rs b/src/parser.rs index f337b90..c1fe2f8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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>, + attributes: AttributeSet<'src>, ) -> CompileResult<'src, Alias<'src, Name<'src>>> { self.presume_keyword(Keyword::Alias)?; let name = self.parse_name()?;