Enum Literal

Source
pub enum Literal {
    Empty,
    Bool(bool),
    Date(DateTime),
    Number(f64),
    Task(Option<bool>, String),
    Text(String),
}
Expand description

Represents a literal value in the AIM expression grammar.

The Literal enum encapsulates all fundamental value types that can appear directly in AIM expressions, without requiring evaluation. Literals form the basis of the type system and provide the primary mechanism for type conversion and promotion throughout the expression evaluation pipeline.

§Literal Variants

Each variant represents a specific data type with its own semantics:

  • Empty - Represents an empty or null value. Used as a placeholder when no meaningful value is present or appropriate. This variant has special handling in comparison operations and type conversion.

  • Bool(bool) - Boolean values, representing logical true/false states. Booleans have straightforward truthiness semantics and can be converted to numbers (1/0) or text (“true”/“false”) as needed.

  • Date(DateTime) - Date and time values using the jiff crate’s DateTime type. Dates support conversion from Unix timestamps (numbers) and ISO 8601-style date strings while maintaining timezone awareness.

  • Number(f64) - 64-bit floating point numbers. The primary numeric type used throughout AIMX, supporting arithmetic operations, mathematical functions, and various conversion contexts.

  • Task(Option<bool>, String) - Task primitives with optional status and description text. Tasks are a unique semantic type in AIMX that represent work items in agentic workflows. Status can be Some(true) (completed), Some(false) (failed), or None (pending).

  • Text(String) - UTF-8 string values. Text literals support the full Unicode character set and provide comprehensive string manipulation capabilities through built-in functions.

§Type System Integration

Literals implement comprehensive type checking and conversion capabilities:

  • Type Matching: The Literal::is_type method checks compatibility with Typedef
  • Type Resolution: The Literal::get_type method returns the literal’s type
  • Type Conversion: Various as_* methods handle type promotion
  • Type Casting: The Literal::as_type method converts to match another literal’s type

§Operator Implementations

The Literal type implements several important traits:

  • PartialEq and Eq - Equality comparison for all literal types
  • PartialOrd and Ord - Consistent ordering across different literal types
  • ExpressionLike - Integration with the expression evaluation system
  • Display - Human-readable string representation

§Examples

§Creating Literals

use aimx::literal::Literal;

// Using constructor functions
let bool_lit = Literal::from_bool(true);
let number_lit = Literal::from_number(42.5);
let text_lit = Literal::from_text("hello".to_string());

// Direct enum construction
let task_lit = Literal::Task(Some(true), "Completed task".to_string());
let empty_lit = Literal::Empty;

§Type Conversion

use aimx::literal::Literal;

let number = Literal::from_number(42.0);
 
// Convert number to text
let as_text = number.clone().as_text().unwrap();
assert_eq!(as_text.to_string(), "42");
 
// Convert number to boolean (non-zero is true)
let as_bool = number.as_bool();
assert!(as_bool.to_bool());

§Type Checking

use aimx::literal::Literal;
use aimx::typedef::Typedef;

let date_lit = Literal::Date(jiff::civil::DateTime::new(2023, 1, 1, 0, 0, 0, 0).unwrap());
 
assert!(date_lit.is_date());
assert!(date_lit.is_type(&Typedef::Date));
assert!(date_lit.is_type(&Typedef::Any));

let date_type = date_lit.get_type().unwrap();
assert_eq!(date_type, Typedef::Date);

§See Also

  • Typedef - The type definition system used for type checking
  • Value - The runtime value representation that wraps literals
  • parse_literal - Function for parsing literal values from strings

Variants§

§

Empty

Represents an empty or null value.

Empty literals are used when no meaningful value is available or appropriate. They have special behavior in comparisons and type conversions:

  • Empty compared to empty text is considered equal
  • Cannot be converted to most specific types except boolean (false)
  • Used as default values in various contexts
§

Bool(bool)

A boolean value representing logical true or false.

Boolean literals participate in logical operations and can be converted to numbers (1 for true, 0 for false) or text (“true”/“false”).

§

Date(DateTime)

A date and time value using the jiff crate’s DateTime type.

Date literals support comprehensive date/time operations including date arithmetic, formatting, and timezone-aware operations.

§

Number(f64)

A 64-bit floating point number.

Number literals are the primary numeric type in AIMX, supporting mathematical operations, functions, and various numeric conversions.

§

Task(Option<bool>, String)

A task primitive with optional status and description text.

Tasks are a semantic type unique to AIMX, representing work items in agentic workflows. The status can be:

  • Some(true) - Completed task
  • Some(false) - Failed task
  • None - Pending task

Tasks have special numeric semantics: completed=1, failed=-1, pending=0.

§

Text(String)

A UTF-8 string value.

Text literals support the full Unicode character set and provide comprehensive string manipulation through built-in functions.

Implementations§

Source§

impl Literal

Source

pub fn is_type(&self, typedef: &Typedef) -> bool

Check if this literal matches the specified type.

§Arguments
  • typedef - The type definition to check against
§Returns

Returns true if this literal matches the specified type, false otherwise.

Source

pub fn get_type(&self) -> Result<Typedef>

Get the type of this literal.

§Returns

Returns a Result<Typedef> containing the type of this literal, or an error if this is an Empty literal.

Source

pub fn as_type(self, literal: &Literal) -> Result<Literal>

Convert this literal to match the type of another literal.

This method performs type conversion according to the grammar’s type promotion rules, converting this literal to match the type of the provided reference literal.

§Arguments
  • literal - The reference literal whose type determines the conversion
§Returns

Returns a Result<Literal> containing the converted literal or an error if conversion is not possible.

Source

pub fn to_type(self, typedef: &Typedef) -> Result<Literal>

Source

pub fn is_empty(&self) -> bool

Check if this literal is empty.

Source

pub fn is_bool(&self) -> bool

Check if this literal represents a boolean value.

Source

pub fn from_bool(b: bool) -> Self

Create a boolean literal from a boolean value.

Source

pub fn as_bool(self) -> Literal

Convert this literal to a boolean representation.

Performs type conversion to boolean according to the grammar’s truthiness rules:

  • Numbers: 0 is false, non-zero is true
  • Text: Empty string is false, non-empty is true
  • Dates: Always true (they exist)
  • Tasks: Status determines value, no status is false

This function provides an implicit error free conversion from any literal to bool specifically for the conditional ternary operator making a type safe error free check possible.

e.g. user.birthday ? user.birthday : _

Source

pub fn to_bool(&self) -> bool

Extract a boolean value from this literal.

This is a convenience method for when you specifically need a bool value.

Source

pub fn is_date(&self) -> bool

Check if this literal represents a date value.

Source

pub fn from_date(d: DateTime) -> Self

Create a date literal from a DateTime value.

Source

pub fn as_date(self) -> Result<Literal>

Convert this literal to a date representation.

Attempts to convert the literal to a date according to the grammar’s conversion rules:

  • Boolean: true becomes Unix epoch + 1 second, false becomes Unix epoch
  • Number: Interpreted as Unix timestamp
  • Text: Parsed as date if possible
  • Task: Text component parsed as date if possible
Source

pub fn is_number(&self) -> bool

Check if this literal represents a number value.

Source

pub fn from_number(n: f64) -> Self

Create a number literal from an f64 value.

Source

pub fn as_number(self) -> Result<Literal>

Convert this literal to a number representation.

Attempts to convert the literal to a number according to the grammar’s conversion rules:

  • Boolean: false becomes 0, true becomes 1
  • Date: Converted to Unix timestamp
  • Text: Parsed as number if it represents a valid numeric literal
  • Task: Status determines value (true=1, false=-1, none=0)
Source

pub fn to_number(&self) -> Result<f64>

Extract a numeric value from this literal.

This is a convenience method for when you specifically need a f64 number.

Source

pub fn is_task(&self) -> bool

Check if this literal represents a task value.

Source

pub fn from_task(status: Option<bool>, task: String) -> Self

Create a task literal from status and text.

Source

pub fn as_task(self) -> Result<Literal>

Convert this literal to a task representation.

Converts the literal to a task according to the grammar’s conversion rules:

  • Boolean: Status becomes the boolean value, text becomes “true”/“false”
  • Date: No status, text becomes date string
  • Number: Status based on sign (positive=true, negative=false, zero=none), text becomes number string
  • Text: No status, text remains the same
Source

pub fn is_text(&self) -> bool

Check if this literal represents a text value.

Source

pub fn from_text(t: String) -> Self

Create a text literal from a String.

Source

pub fn as_text(self) -> Result<Literal>

Convert this literal to a text representation.

Converts the literal to text according to the grammar’s conversion rules:

  • Boolean: “true” or “false”
  • Date: Formatted as date string
  • Number: Formatted as string
  • Task: Text component of the task
Source

pub fn type_as_string(&self) -> &'static str

Get a string representation of this literal’s type.

Trait Implementations§

Source§

impl Clone for Literal

Source§

fn clone(&self) -> Literal

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 Literal

Source§

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

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

impl Display for Literal

Source§

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

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

impl ExpressionLike for Literal

Source§

fn evaluate(&self, _context: &mut dyn ContextLike) -> Result<Value>

Evaluate the expression within the given context. Read more
Source§

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

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

fn to_sanitized(&self) -> String

Convert this expression to a sanitized string representation. Read more
Source§

fn to_formula(&self) -> String

Convert this expression to a formula string representation. Read more
Source§

impl Ord for Literal

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Literal

Source§

fn eq(&self, other: &Literal) -> 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 PartialOrd for Literal

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for Literal

Source§

impl StructuralPartialEq for Literal

Auto Trait Implementations§

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
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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,