elo-rust 0.4.1

Rust code generation target for ELO validation language
Documentation

ELO Rust Code Generation Target

A production-grade Rust code generation target for the ELO validation language. Converts ELO validation expressions into zero-cost Rust validators with <1ยตs execution time.

CI Crates.io Docs.rs License Security Audit Code Coverage

Features

โœจ High Performance

  • Generated validators execute in <1ยตs
  • Zero-copy design with minimal allocations
  • Compile-time optimization via Rust compiler

๐ŸŽฏ Comprehensive Validation

  • String operations: regex matching, contains, length, case conversion, trim, starts_with, ends_with
  • Date/time functions: today(), now(), age(), days_since(), date parsing
  • Array operations: contains, any, all, length, is_empty
  • Type checking: is_null, is_some for Option types

๐Ÿ› ๏ธ Developer Friendly

  • Simple validator macro: #[elo_validator(elo = "expression")]
  • CLI tool for code generation: elo compile --expression "age >= 18"
  • Framework integration examples (Actix-web, Axum)
  • Comprehensive error reporting

Quick Start

Using the Validator Macro

use elo_rust::elo_validator;

#[elo_validator(elo = r#"
  email matches "^[a-z]+@example\.com$" &&
  age >= 18 &&
  verified == true
"#)]
pub struct UserValidator;

let user = User { age: 25, email: "john@example.com".to_string(), verified: true };
match UserValidator::validate(&user) {
    Ok(()) => println!("โœ… Valid user"),
    Err(e) => println!("โŒ Error: {}", e),
}

Using the CLI

# Parse and validate ELO expression
elo compile --expression "age >= 18 && verified == true"

# Parse from file
elo validate --input rules.elo

# Compile with output
elo compile --input rules.elo --output validator.rs

As a Library

use elo_rust::parser::Parser;
use elo_rust::codegen::RustCodeGenerator;

// Parse ELO expression
let parser = Parser::new("age >= 18");
let ast = parser.parse()?;

// Generate Rust code
let gen = RustCodeGenerator::new();
let code = gen.generate_validator("validate_age", &ast, "User")?;

In Actix-web (With ELO Validator)

use actix_web::{post, web, App, HttpServer};
use elo_rust::elo_validator;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Clone)]
struct CreateUserRequest {
    username: String,
    email: String,
    age: i32,
}

#[derive(Serialize)]
struct ValidationError {
    field: String,
    message: String,
}

#[elo_validator(elo = r#"
  email matches "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$" &&
  username.length() >= 3 && username.length() <= 20 &&
  age >= 18 && age <= 120
"#)]
struct UserValidator;

#[post("/users")]
async fn create_user(req: web::Json<CreateUserRequest>) -> HttpResponse {
    match UserValidator::validate(&req) {
        Ok(()) => HttpResponse::Created().json(serde_json::json!({
            "message": "User created successfully"
        })),
        Err(errors) => HttpResponse::BadRequest().json(serde_json::json!({
            "errors": errors
        })),
    }
}

Supported Functions & Operators

String Functions (8)

  • matches(pattern) - Regex pattern matching
  • contains(substring) - Substring search
  • length() - String length
  • uppercase() - Convert to uppercase
  • lowercase() - Convert to lowercase
  • trim() - Remove whitespace
  • starts_with(prefix) - Prefix check
  • ends_with(suffix) - Suffix check

DateTime Functions (5)

  • today() - Current date
  • now() - Current UTC timestamp
  • age(birthdate) - Age calculation from birthdate
  • days_since(date) - Days elapsed
  • date("YYYY-MM-DD") - Parse ISO 8601 date

Temporal Keywords (16)

NOW, TODAY, TOMORROW, YESTERDAY, EPOCH, UTC, START_OF_DAY, END_OF_DAY, START_OF_WEEK, END_OF_WEEK, START_OF_MONTH, END_OF_MONTH, START_OF_YEAR, END_OF_YEAR, MIDNIGHT, NOON

Array Functions (5)

  • contains(value) - Element search
  • any(predicate) - Existence check with closure
  • all(predicate) - Universal check with closure
  • length() - Array size
  • is_empty() - Empty check

Type Functions (2)

  • is_null() - Option null check
  • is_some() - Option some check

Operators

Arithmetic: +, -, *, /, % Comparison: ==, !=, <, <=, >, >= Logical: &&, ||, !

Expression Examples

Simple Validation

age >= 18

Email & Username Validation

email matches "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" &&
username |> length() >= 3 && username |> length() <= 20

Temporal Validation (New in 0.4.0)

created_at >= TODAY &&
expires_at > NOW &&
updated_at < END_OF_DAY

Conditional Validation with Let Bindings

let username_len = username |> length() in
let email_valid = email matches "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$" in
username_len >= 3 && username_len <= 20 && email_valid

User Account Validation with Guard

guard age >= 18 && verified in
email matches "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$" &&
username |> length() >= 3

Permission Checking

(roles |> contains("admin") || roles |> contains("moderator")) &&
verified == true &&
active == true

Order Validation with Aggregation

items |> length() > 0 &&
items |> all(quantity > 0 && price > 0) &&
total > 0

Conditional Pricing with If Expression

if verified then price * 0.9 else price

Complex Policy Logic

let is_admin = roles |> contains("admin") in
let account_age = days_since(created_at) in
if is_admin then
  account_age > 0
else
  account_age > 30 && verified && payment_verified

API Documentation

RustCodeGenerator

Main code generator for transforming ELO expressions to Rust code.

pub struct RustCodeGenerator {
    // Type context for resolving custom types
}

impl RustCodeGenerator {
    pub fn new() -> Self
    pub fn with_context(type_context: TypeContext) -> Self
    pub fn generate_function_signature(
        &self,
        name: &str,
        input_type: &str,
    ) -> Result<TokenStream, String>

    pub fn generate_literal_integer(&self, value: i64) -> Result<TokenStream, String>
    pub fn generate_literal_string(&self, value: &str) -> Result<TokenStream, String>
    pub fn generate_literal_bool(&self, value: bool) -> Result<TokenStream, String>

    pub fn generate_field_access(
        &self,
        receiver: &str,
        field: &str,
    ) -> Result<TokenStream, String>

    pub fn generate_validator(
        &self,
        name: &str,
        elo_expr: &str,
        input_type: &str,
    ) -> Result<TokenStream, String>
}

OperatorGenerator

Generates code for binary and unary operations.

pub struct OperatorGenerator;

impl OperatorGenerator {
    pub fn new() -> Self
    pub fn binary(
        &self,
        op: BinaryOp,
        left: TokenStream,
        right: TokenStream,
    ) -> TokenStream
    pub fn unary(
        &self,
        op: UnaryOp,
        operand: TokenStream,
    ) -> TokenStream
}

FunctionGenerator

Generates code for standard library functions.

pub struct FunctionGenerator;

impl FunctionGenerator {
    pub fn new() -> Self
    pub fn string_function(
        &self,
        name: &str,
        args: Vec<TokenStream>,
    ) -> TokenStream
    pub fn datetime_function(
        &self,
        name: &str,
        args: Vec<TokenStream>,
    ) -> TokenStream
    pub fn array_function(
        &self,
        name: &str,
        args: Vec<TokenStream>,
    ) -> TokenStream
}

Project Statistics

  • Total Tests: 786 (100% passing)
  • Code Coverage: 70%+ of codebase
  • Production Code: 4,600+ lines
  • Standard Library Functions: 20+ implemented
  • Temporal Functions: 16 keywords supported
  • Code Generation: Full AST visitor pattern with optimization
  • Performance: <5ยตs full compilation, <1ยตs execution
  • Security: Enterprise-grade hardening with comprehensive validation

Testing

Run the full test suite:

cargo test

Run specific test category:

cargo test string_functions
cargo test datetime_functions
cargo test array_functions
cargo test macro_usage

Run examples:

cargo run --example actix_validator --features serde-support
cargo run --example axum_validator --features serde-support

Building

# Debug build
cargo build

# Release build with optimizations
cargo build --release

# CLI tool
cargo build --bin elo

# Documentation
cargo doc --no-deps --open

Architecture

src/
โ”œโ”€โ”€ lib.rs                    # Public API
โ”œโ”€โ”€ parser/
โ”‚   โ”œโ”€โ”€ mod.rs              # Recursive descent parser
โ”‚   โ”œโ”€โ”€ lexer.rs            # Tokenization
โ”‚   โ””โ”€โ”€ error.rs            # Parse errors with source context
โ”œโ”€โ”€ ast/
โ”‚   โ”œโ”€โ”€ mod.rs              # AST definitions
โ”‚   โ””โ”€โ”€ visitor.rs          # Visitor pattern for traversal
โ”œโ”€โ”€ codegen/
โ”‚   โ”œโ”€โ”€ mod.rs              # Main RustCodeGenerator
โ”‚   โ”œโ”€โ”€ ast_to_code.rs      # AST visitor to TokenStream
โ”‚   โ”œโ”€โ”€ operators.rs        # Binary/unary operators
โ”‚   โ”œโ”€โ”€ functions.rs        # String/date/array functions
โ”‚   โ”œโ”€โ”€ temporal.rs         # Temporal type operations
โ”‚   โ”œโ”€โ”€ type_inference.rs   # Type inference engine
โ”‚   โ”œโ”€โ”€ optimization.rs     # Constant folding optimizer
โ”‚   โ”œโ”€โ”€ types.rs            # Type system & context
โ”‚   โ””โ”€โ”€ errors.rs           # Code generation errors
โ”œโ”€โ”€ runtime/
โ”‚   โ”œโ”€โ”€ mod.rs              # ValidationError types
โ”‚   โ”œโ”€โ”€ value.rs            # EloValue enum for runtime types
โ”‚   โ””โ”€โ”€ temporal.rs         # Temporal value operations
โ”œโ”€โ”€ security.rs             # Input validation & security
โ””โ”€โ”€ bin/
    โ””โ”€โ”€ elo.rs              # CLI tool

tests/
โ”œโ”€โ”€ error_handling.rs       # 26 error tests
โ”œโ”€โ”€ temporal_integration.rs # 14 temporal tests
โ”œโ”€โ”€ parsing.rs              # 9 benchmark tests
โ””โ”€โ”€ ... (19 other test modules, 700+ tests total)

benches/
โ””โ”€โ”€ parsing.rs              # Performance benchmarks

examples/
โ”œโ”€โ”€ simple_validator.rs     # Basic example
โ”œโ”€โ”€ actix_validator.rs      # Actix integration
โ””โ”€โ”€ axum_validator.rs       # Axum integration

Performance

Generated validators are designed for minimal overhead:

  • Code Generation: <100ms per expression
  • Validator Execution: <1ยตs per check
  • Memory Overhead: Minimal allocations
  • Binary Size: ~50 lines typical validator code

License

MIT

Contributing

Contributions are welcome! Please ensure:

  • All tests pass: cargo test
  • Code passes clippy: cargo clippy --all-targets -- -D warnings
  • Code is formatted: cargo fmt

Support

For issues, questions, or contributions, please visit: https://github.com/enspirit/elo


Version: 0.4.1 Status: โœ… Production Ready Last Updated: February 8, 2026 Architecture: โœ… Complete (8 phases) Tests: โœ… 786 passing (100%) Coverage: โœ… 70%+ Benchmarks: โœ… <5ยตs compilation, <1ยตs execution