pub enum Equality {
Equal(Relational, Relational),
NotEqual(Relational, Relational),
Primary(Box<Primary>),
}Expand description
An equality expression node in the abstract syntax tree.
Represents an equality comparison (=, !=) or a lower-precedence
expression that has been flattened in the AST. This enum is part of
the AIMX expression hierarchy and implements the ExpressionLike trait.
§AST Flattening
The Primary variant represents an optimization where expressions that
don’t contain equality operators are flattened to reduce AST depth and
improve evaluation performance. This flattening occurs during parsing
when an expression doesn’t contain any equality operators.
§Variants
§Equal(Relational, Relational)
Represents an equality comparison using the = or == operator.
The operands are Relational expressions, which have higher precedence.
§NotEqual(Relational, Relational)
Represents an inequality comparison using the != operator.
The operands are Relational expressions, which have higher precedence.
§Primary(Primary)
A flattened primary expression for optimization. This variant is used when the expression doesn’t contain any equality operators.
§Examples
use aimx::expressions::{
equality::Equality,
relational::Relational,
primary::Primary,
};
use aimx::{Literal, ExpressionLike, Context};
// This demonstrates how equality expressions can be constructed
// In practice, these would be parsed from text expressions
// Create an equality expression conceptually like: 5 = 5
// Create an inequality expression conceptually like: 5 != 10§See Also
parse_equality- Function to parse equality expressionsExpressionLike- Trait for expression evaluationRelational- Higher precedence expression typePrimary- Flattened primary expression type
Variants§
Equal(Relational, Relational)
NotEqual(Relational, Relational)
Primary(Box<Primary>)
Primary flattened AST optimization
Trait Implementations§
Source§impl Display for Equality
impl Display for Equality
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Format this equality expression as a string.
This implementation delegates to the write method
using a string-based writer with default formatting. The output is suitable
for display purposes and debugging.
§Format
The formatting follows these conventions:
- Operators are surrounded by spaces:
5 = 5,5 != 10 - Complex expressions are properly parenthesized
- Literals are formatted according to their type
- Variables and references are displayed as-is
§Examples
use aimx::expressions::equality::{Equality, parse_equality};
let expr = parse_equality("5 = 5").unwrap().1;
assert_eq!(expr.to_string(), "5 = 5");
let expr = parse_equality("x > 0 = y < 10").unwrap().1;
assert_eq!(expr.to_string(), "x > 0 = y < 10");
let expr = parse_equality("5").unwrap().1;
assert_eq!(expr.to_string(), "5");§See Also
ExpressionLike::write- The underlying serialization methodExpressionLike::to_formula- For round-trippable formula representationExpressionLike::to_sanitized- For sanitized output
Source§impl ExpressionLike for Equality
impl ExpressionLike for Equality
Source§fn evaluate(&self, context: &mut dyn ContextLike) -> Result<Value>
fn evaluate(&self, context: &mut dyn ContextLike) -> Result<Value>
Evaluate this equality expression within the given context.
This method implements the core evaluation logic for equality expressions.
It handles both equality (=) and inequality (!=) comparisons, using
type promotion to ensure both operands have compatible types.
§Evaluation Strategy
-
Equality Comparison (
Equality::Equal):- Evaluates both operands using
evaluate_and_promote - Promotes the right operand to match the left operand’s type
- Compares values of the same type using
==operator - Returns
trueif equal,falseotherwise
- Evaluates both operands using
-
Inequality Comparison (
Equality::NotEqual):- Evaluates both operands using
evaluate_and_promote - Promotes the right operand to match the left operand’s type
- Compares values of the same type using
!=operator - Returns
trueif not equal,falseotherwise
- Evaluates both operands using
-
Flattened Expression (
Equality::Primary):- Delegates evaluation to the underlying primary expression
§Type Support
Equality operations support these value types:
Bool- Boolean equality comparisonDate- DateTime equality comparisonNumber- Numeric equality comparisonText- String equality comparisonTask- Task status equality comparison
§Error Handling
Returns an error if:
- Type promotion fails (incompatible types)
- Either operand evaluation fails
- Comparison of unsupported types is attempted
Error messages include type information and the original formula:
"Expected Bool, Date, Number, Task or Text, found Type1 = Type2 ~formula"§Arguments
context- The evaluation context providing variable values and function implementations
§Returns
Returns a Result<Value> containing:
Value::Literal(Literal::Bool(true))if comparison evaluates to trueValue::Literal(Literal::Bool(false))if comparison evaluates to false- An error if evaluation fails
§Examples
use aimx::expressions::equality::{Equality, parse_equality};
use aimx::{ExpressionLike, Context, Literal, Value};
let mut context = Context::new();
// Evaluate equality: 5 = 5
let expr = parse_equality("5 = 5").unwrap().1;
let result = expr.evaluate(&mut context).unwrap();
assert_eq!(result, Value::Literal(Literal::Bool(true)));
// Evaluate inequality: 5 != 10
let expr = parse_equality("5 != 10").unwrap().1;
let result = expr.evaluate(&mut context).unwrap();
assert_eq!(result, Value::Literal(Literal::Bool(true)));
// Evaluate with type promotion: 5 = "5"
let expr = parse_equality("5 = \"5\"").unwrap().1;
let result = expr.evaluate(&mut context).unwrap();
assert_eq!(result, Value::Literal(Literal::Bool(true)));Source§fn write(&self, writer: &mut Writer)
fn write(&self, writer: &mut Writer)
Write this equality expression to the provided writer.
This method serializes the expression to a text representation using the writer’s current formatting mode. The writer handles indentation, operator spacing, and other formatting concerns.
§Serialization Strategy
-
Equality Comparison (
Equality::Equal):- Writes left operand
- Writes
" = "operator with spaces - Writes right operand
-
Inequality Comparison (
Equality::NotEqual):- Writes left operand
- Writes
" != "operator with spaces - Writes right operand
-
Flattened Expression (
Equality::Primary):- Delegates writing to the underlying primary expression
§Arguments
writer- The writer to serialize the expression to
§Examples
use aimx::expressions::equality::{Equality, parse_equality};
use aimx::{Writer, PrintMode, Prefix};
// This demonstrates the general pattern for writing equality expressions
// In practice, these would be used internally by the expression evaluator
// Writing an equality expression like "5 = 5"
// Writing an inequality expression like "5 != 10"Source§fn to_sanitized(&self) -> String
fn to_sanitized(&self) -> String
Convert this equality expression to a sanitized string representation.
This method produces a string with special characters escaped to make it safe for various contexts like JSON output, HTML embedding, or other contexts where escaping is required.
§Returns
A sanitized string representation of this expression.
Source§fn to_formula(&self) -> String
fn to_formula(&self) -> String
Convert this equality expression to a formula string representation.
This method produces a string with proper quoting and escaping for use in formulas. The output should be round-trippable, meaning it can be parsed back into an equivalent expression.
§Returns
A formula string representation of this expression.
impl StructuralPartialEq for Equality
Auto Trait Implementations§
impl Freeze for Equality
impl RefUnwindSafe for Equality
impl Send for Equality
impl Sync for Equality
impl Unpin for Equality
impl UnwindSafe for Equality
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.