From c75a38f6152fffcdb7b044231f7c65da957ece25 Mon Sep 17 00:00:00 2001 From: John Turner Date: Tue, 18 Nov 2025 04:15:53 +0000 Subject: [PATCH] allow slot to be only :* := :slot/sub= or :slot --- src/atom/mod.rs | 46 ++++++++++++++++++++++++++++++--------------- src/atom/parsers.rs | 23 ++++++++++------------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/atom/mod.rs b/src/atom/mod.rs index 0378a8d..6251e35 100644 --- a/src/atom/mod.rs +++ b/src/atom/mod.rs @@ -79,11 +79,17 @@ pub enum SlotOperator { #[derive(Clone, Debug, PartialEq, Eq, Get)] pub struct SlotName(#[get(method = "name", kind = "deref")] String); -#[derive(Clone, Debug, PartialEq, Eq, Get)] -pub struct Slot { - primary: Option, - sub: Option, - operator: Option, +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Slot { + Wildcard, + Equal { + primary: Option, + sub: Option, + }, + Name { + primary: SlotName, + sub: Option, + }, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -575,19 +581,29 @@ impl fmt::Display for SlotName { impl fmt::Display for Slot { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if let Some(slot) = self.primary.as_ref() { - write!(f, "{slot}")?; - } + match self { + Self::Wildcard => write!(f, "*"), + Self::Equal { primary, sub } => { + if let Some(primary) = primary { + write!(f, "{primary}")?; + } - if let Some(sub) = self.sub.as_ref() { - write!(f, "/{sub}")?; - } + if let Some(sub) = sub { + write!(f, "/{sub}")?; + } - if let Some(operator) = self.operator.as_ref() { - write!(f, "{operator}")?; - } + write!(f, "=") + } + Self::Name { primary, sub } => { + write!(f, "{primary}")?; - Ok(()) + if let Some(sub) = sub { + write!(f, "/{sub}")?; + } + + Ok(()) + } + } } } diff --git a/src/atom/parsers.rs b/src/atom/parsers.rs index 75eca51..47c8c1e 100644 --- a/src/atom/parsers.rs +++ b/src/atom/parsers.rs @@ -186,21 +186,17 @@ impl<'a> Parseable<'a, &'a str> for Slot { type Parser = impl Parser<&'a str, Output = Self>; fn parser() -> Self::Parser { - SlotName::parser() + let wildcard = tag("*").map(|_| Slot::Wildcard); + let equals = SlotName::parser() .opt() .and(SlotName::parser().preceded_by(tag("/")).opt()) - .and(SlotOperator::parser().opt()) - .map(|((primary, sub), operator)| Slot { - primary, - sub, - operator, - }) - .verify_output(|slot| { - matches!( - (&slot.primary(), &slot.operator()), - (Some(_) | None, Some(_)) | (Some(_), None) - ) - }) + .followed_by(tag("=")) + .map(|(primary, sub)| Slot::Equal { primary, sub }); + let name = SlotName::parser() + .and(SlotName::parser().preceded_by(tag("/")).opt()) + .map(|(primary, sub)| Slot::Name { primary, sub }); + + wildcard.or(equals).or(name) } } @@ -556,6 +552,7 @@ mod test { "=dev-ml/stdio-0.17*t:=[ocamlopt?]", ">=dev-libs/libgee-0-8.5:0..8=", "=kde-frameworks/kcrash-2.16.0:6*", ]; for atom in atoms {