pub enum Unary {
Not(Box<Unary>),
Positive(Box<Unary>),
Negative(Box<Unary>),
CastBool(Box<Unary>),
CastDate(Box<Unary>),
CastNumber(Box<Unary>),
CastTask(Box<Unary>),
CastText(Box<Unary>),
Primary(Box<Primary>),
}Expand description
Represents a unary expression in the AIM grammar.
Unary expressions perform operations on a single operand, including logical NOT, unary plus/minus, and type casting. These operations have right-to-left associativity, meaning they group from right to left.
§AST Flattening Optimization
The Unary enum uses AST flattening where it includes variants for all
lower-precedence expression types. This allows for efficient evaluation
without deep recursion and simplifies the implementation of the
ExpressionLike trait.
§Variants
§Basic Unary Operations
These variants represent single-operand operations:
Not- Logical NOT operation:!operandPositive- Unary plus operation:+operandNegative- Unary minus operation:-operand
§Type Casting Operations
These variants provide explicit type conversion using the (Type) syntax:
CastBool- Cast to boolean type:(Bool)operandCastDate- Cast to date type:(Date)operandCastNumber- Cast to number type:(Number)operandCastTask- Cast to task type:(Task)operandCastText- Cast to text type:(Text)operand
§Flattened Expressions (AST Optimization)
These variants flatten the AST structure for efficient evaluation:
Primary- Flattened primary expression (literals, references, parentheses)
§Examples
use aimx::expressions::unary::{Unary, parse_unary};
// Parse and match different Unary variants
let (_, not_expr) = parse_unary("!true").unwrap();
assert!(matches!(not_expr, Unary::Not(_)));
let (_, positive_expr) = parse_unary("+5").unwrap();
assert!(matches!(positive_expr, Unary::Positive(_)));
let (_, cast_expr) = parse_unary("(Number)\"123\"").unwrap();
assert!(matches!(cast_expr, Unary::CastNumber(_)));
// Chained operations demonstrate right-to-left associativity
let (_, chained) = parse_unary("!(Bool)0").unwrap();
// This parses as !((Bool)0) - NOT applied to the result of the cast§Evaluation Behavior
Each variant has specific evaluation behavior:
Not: Evaluates operand to boolean, then applies logical NOTPositive/Negative: Evaluates operand to number, then applies sign- Cast variants: Evaluates operand, then converts to target type
Primary: Delegates evaluation to the contained primary expression
Variants§
Not(Box<Unary>)
Logical NOT operation: !operand
Positive(Box<Unary>)
Unary plus operation: +operand
Negative(Box<Unary>)
Unary minus operation: -operand
CastBool(Box<Unary>)
Cast to boolean type: (Bool)operand
CastDate(Box<Unary>)
Cast to date type: (Date)operand
CastNumber(Box<Unary>)
Cast to number type: (Number)operand
CastTask(Box<Unary>)
Cast to task type: (Task)operand
CastText(Box<Unary>)
Cast to text type: (Text)operand
Primary(Box<Primary>)
Primary flattened AST optimization
Trait Implementations§
Source§impl ExpressionLike for Unary
impl ExpressionLike for Unary
Source§fn evaluate(&self, context: &mut dyn ContextLike) -> Result<Value>
fn evaluate(&self, context: &mut dyn ContextLike) -> Result<Value>
Evaluate the unary expression within the given context.
This method evaluates the expression recursively, handling each variant according to its specific semantics. Evaluation follows the operator precedence rules and handles type conversions as needed.
§Arguments
context- The evaluation context providing variable values and function implementations
§Returns
Result<Value>- The evaluated result or an error if evaluation fails
§Evaluation Behavior by Variant
Not: Evaluates operand to boolean usingas_bool(), then applies logical NOTPositive: Evaluates operand to number usingas_number(), preserves signNegative: Evaluates operand to number usingas_number(), negates value- Cast variants: Evaluates operand, then converts to target type using corresponding
as_*()method Primary: Delegates evaluation to the contained primary expression
§Error Handling
Returns Err if:
- Operand evaluation fails
- Type conversion fails (e.g., non-numeric operand for
+/-) - Invalid type for cast operation
§Examples
use aimx::{expressions::unary::Unary, ExpressionLike, Context, Literal};
let mut context = Context::new();
// Create a simple unary expression for testing
let expr = Unary::CastText(Box::new(Unary::Primary(Box::new(aimx::Primary::Literal(Literal::from_number(42.0))))));
let result = expr.evaluate(&mut context).unwrap();
assert_eq!(result.to_string(), "42");Source§fn to_sanitized(&self) -> String
fn to_sanitized(&self) -> String
Convert the expression to a sanitized string representation.
Sanitized strings are suitable for display to users and may omit sensitive information or simplify complex expressions.
§Returns
String- A sanitized string representation of the expression
Source§fn to_formula(&self) -> String
fn to_formula(&self) -> String
Convert the expression to a formula string representation.
Formula strings preserve the exact syntax of the original expression and are suitable for serialization or debugging.
§Returns
String- A formula string representation of the expression
impl StructuralPartialEq for Unary
Auto Trait Implementations§
impl Freeze for Unary
impl RefUnwindSafe for Unary
impl Send for Unary
impl Sync for Unary
impl Unpin for Unary
impl UnwindSafe for Unary
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.