Enum Unary

Source
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: !operand
  • Positive - Unary plus operation: +operand
  • Negative - Unary minus operation: -operand

§Type Casting Operations

These variants provide explicit type conversion using the (Type) syntax:

  • CastBool - Cast to boolean type: (Bool)operand
  • CastDate - Cast to date type: (Date)operand
  • CastNumber - Cast to number type: (Number)operand
  • CastTask - Cast to task type: (Task)operand
  • CastText - 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 NOT
  • Positive/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 Clone for Unary

Source§

fn clone(&self) -> Unary

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Unary

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Unary

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Format the unary expression as a string.

This implementation uses the Writer::stringizer() to produce a human-readable string representation of the expression.

§Arguments
  • f - The formatter to write to
§Returns
  • fmt::Result - The result of the formatting operation
Source§

impl ExpressionLike for Unary

Source§

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 using as_bool(), then applies logical NOT
  • Positive: Evaluates operand to number using as_number(), preserves sign
  • Negative: Evaluates operand to number using as_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

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

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
Source§

fn write(&self, writer: &mut Writer)

Write this expression to the provided writer. Read more
Source§

impl PartialEq for Unary

Source§

fn eq(&self, other: &Unary) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T> ToStringFallible for T
where T: Display,

§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,