From e409a5a6c2d6d95320e228b4d5f2eeca7caf79dd Mon Sep 17 00:00:00 2001 From: elvis Date: Sun, 17 Aug 2025 02:53:56 +0200 Subject: [PATCH] Harder, better, faster, stronger refactoring. --- src/rsprocess/assert/dsl.rs | 56 ++++++++++++++++++++++------------- src/rsprocess/assert/fmt.rs | 26 ++++++++++++---- src/rsprocess/assert/tests.rs | 12 +++++--- 3 files changed, 65 insertions(+), 29 deletions(-) diff --git a/src/rsprocess/assert/dsl.rs b/src/rsprocess/assert/dsl.rs index bfd3f4a..f28078d 100644 --- a/src/rsprocess/assert/dsl.rs +++ b/src/rsprocess/assert/dsl.rs @@ -32,10 +32,17 @@ pub enum Variable { trait SpecialVariables: std::fmt::Display + std::fmt::Debug + Sized + Eq + Copy + std::hash::Hash { + /// Returns the type of the specific special variable. fn type_of(&self) -> AssertionTypes; + + /// Returns the type of the qualified special variable. fn type_qualified(&self, q: &Qualifier) -> Result; + + /// Creates a new context. fn new_context(input: HashMap) -> HashMap; + + /// Returns true if fn correct_type(&self, other: &AssertReturnValue) -> bool; } @@ -68,12 +75,8 @@ pub enum Unary { Empty, Length, ToStr, - Qualifier(Qualifier), ToEl, - Source, - Target, - Neighbours, - System, + Qualifier(Qualifier), } #[derive(Debug, Clone, Copy)] @@ -100,11 +103,25 @@ pub enum QualifierSystem { Context, } +#[derive(Debug, Clone, Copy)] +pub enum QualifierEdge { + Source, + Target, +} + +#[derive(Debug, Clone, Copy)] +pub enum QualifierNode { + Neighbours, + System, +} + #[derive(Debug, Clone, Copy)] pub enum Qualifier { System(QualifierSystem), Label(QualifierLabel), Restricted(QualifierRestricted), + Edge(QualifierEdge), + Node(QualifierNode), } #[derive(Debug, Clone, Copy)] @@ -254,11 +271,7 @@ impl Unary { Self::Length | Self::ToStr | Self::Qualifier(_) | - Self::ToEl | - Self::Source | - Self::Target | - Self::Neighbours | - Self::System => false, + Self::ToEl => false, } } @@ -299,13 +312,11 @@ impl Unary { (Self::ToEl, AssertionTypes::String) => { Ok(AssertionTypes::Element) }, - (Self::Source, AssertionTypes::Edge) => { + (Self::Qualifier(Qualifier::Edge(_)), AssertionTypes::Edge) => { Ok(AssertionTypes::Node) }, - (Self::Target, AssertionTypes::Edge) => { - Ok(AssertionTypes::Node) - }, - (Self::Neighbours, AssertionTypes::Node) => { + (Self::Qualifier(Qualifier::Node(QualifierNode::Neighbours)), + AssertionTypes::Node) => { Ok(AssertionTypes::RangeNeighbours) }, (Self::Qualifier( @@ -318,7 +329,8 @@ impl Unary { AssertionTypes::System) => { Ok(AssertionTypes::Context) }, - (Self::System, AssertionTypes::Node) => { + (Self::Qualifier(Qualifier::Node(QualifierNode::System)), + AssertionTypes::Node) => { Ok(AssertionTypes::System) }, (op, type_exp) => { @@ -774,18 +786,22 @@ impl AssertReturnValue { (AssertReturnValue::String(s), Unary::ToEl) => { Ok(AssertReturnValue::Element(translator.encode(s))) }, - (AssertReturnValue::Edge(edge), Unary::Source) => { + (AssertReturnValue::Edge(edge), + Unary::Qualifier(Qualifier::Edge(QualifierEdge::Source))) => { Ok(AssertReturnValue::Node( graph.edge_endpoints(edge).unwrap().0)) }, - (AssertReturnValue::Edge(edge), Unary::Target) => { + (AssertReturnValue::Edge(edge), + Unary::Qualifier(Qualifier::Edge(QualifierEdge::Target))) => { Ok(AssertReturnValue::Node( graph.edge_endpoints(edge).unwrap().1)) }, - (AssertReturnValue::Node(node), Unary::Neighbours) => { + (AssertReturnValue::Node(node), + Unary::Qualifier(Qualifier::Node(QualifierNode::Neighbours))) => { Ok(AssertReturnValue::Neighbours(node)) }, - (AssertReturnValue::Node(node), Unary::System) => { + (AssertReturnValue::Node(node), + Unary::Qualifier(Qualifier::Node(QualifierNode::System))) => { Ok(AssertReturnValue::System( graph.node_weight(node).unwrap().clone() )) diff --git a/src/rsprocess/assert/fmt.rs b/src/rsprocess/assert/fmt.rs index 42cbfe7..8437702 100644 --- a/src/rsprocess/assert/fmt.rs +++ b/src/rsprocess/assert/fmt.rs @@ -92,12 +92,8 @@ impl fmt::Display for Unary { Self::Empty => write!(f, ".empty"), Self::Length => write!(f, ".length"), Self::ToStr => write!(f, ".tostr"), - Self::Qualifier(q) => write!(f, ".{q}"), Self::ToEl => write!(f, ".toel"), - Self::Source => write!(f, ".source"), - Self::Target => write!(f, ".target"), - Self::Neighbours => write!(f, ".neighbours"), - Self::System => write!(f, ".system"), + Self::Qualifier(q) => write!(f, ".{q}"), } } } @@ -135,12 +131,32 @@ impl fmt::Display for QualifierSystem { } } +impl fmt::Display for QualifierEdge { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Source => write!(f, "source"), + Self::Target => write!(f, "target"), + } + } +} + +impl fmt::Display for QualifierNode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Neighbours => write!(f, "neighbours"), + Self::System => write!(f, "system"), + } + } +} + impl fmt::Display for Qualifier { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Label(q) => write!(f, "{q}"), Self::Restricted(q) => write!(f, "{q}"), Self::System(q) => write!(f, "{q}"), + Self::Edge(q) => write!(f, "{q}"), + Self::Node(q) => write!(f, "{q}"), } } } diff --git a/src/rsprocess/assert/tests.rs b/src/rsprocess/assert/tests.rs index 1a38d27..09bcf69 100644 --- a/src/rsprocess/assert/tests.rs +++ b/src/rsprocess/assert/tests.rs @@ -1322,9 +1322,11 @@ fn assert_tycheck_for_8() { Variable::Id("a".into()), None, Box::new(Expression::Unary( - Unary::Neighbours, + Unary::Qualifier(Qualifier::Node( + QualifierNode::Neighbours)), Box::new(Expression::Unary( - Unary::Source, + Unary::Qualifier(Qualifier::Edge( + QualifierEdge::Source)), Box::new(Expression::Var( Variable::Special(EdgeRelablerInput::Edge) )) @@ -1404,9 +1406,11 @@ fn assert_tycheck_system() { Qualifier::System(QualifierSystem::Entities) ), Box::new(Expression::Unary( - Unary::System, + Unary::Qualifier(Qualifier::Node( + QualifierNode::System)), Box::new(Expression::Unary( - Unary::Target, + Unary::Qualifier(Qualifier::Edge( + QualifierEdge::Target)), Box::new(Expression::Var( Variable::Special(EdgeRelablerInput::Edge) ))