# 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][ci-badge]][ci-link]
[![Crates.io][crates-badge]][crates-link]
[![Docs.rs][docs-badge]][docs-link]
[![License][license-badge]][license-link]
[![Security Audit][security-badge]][security-link]
[![Code Coverage][coverage-badge]][coverage-link]
[ci-badge]: https://github.com/evoludigit/elo-rust/workflows/CI/badge.svg
[ci-link]: https://github.com/evoludigit/elo-rust/actions/workflows/ci.yml
[crates-badge]: https://img.shields.io/crates/v/elo-rust.svg
[crates-link]: https://crates.io/crates/elo-rust
[docs-badge]: https://docs.rs/elo-rust/badge.svg
[docs-link]: https://docs.rs/elo-rust
[license-badge]: https://img.shields.io/crates/l/elo-rust.svg
[license-link]: #license
[security-badge]: https://img.shields.io/badge/security-audited-green.svg
[security-link]: ./FINAL_SECURITY_REPORT.md
[coverage-badge]: https://img.shields.io/badge/coverage-65%25%2B-brightgreen.svg
[coverage-link]: #testing
## 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
```rust
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
```bash
# 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
```rust
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)
```rust
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
```elo
age >= 18
```
### Email & Username Validation
```elo
email matches "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" &&
### Temporal Validation (New in 0.4.0)
```elo
created_at >= TODAY &&
expires_at > NOW &&
updated_at < END_OF_DAY
```
### Conditional Validation with Let Bindings
```elo
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
```elo
guard age >= 18 && verified in
email matches "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$" &&
### Permission Checking
```elo
active == true
```
### Order Validation with Aggregation
```elo
total > 0
```
### Conditional Pricing with If Expression
```elo
if verified then price * 0.9 else price
```
### Complex Policy Logic
```elo
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.
```rust
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.
```rust
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.
```rust
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:
```bash
cargo test
```
Run specific test category:
```bash
cargo test string_functions
cargo test datetime_functions
cargo test array_functions
cargo test macro_usage
```
Run examples:
```bash
cargo run --example actix_validator --features serde-support
cargo run --example axum_validator --features serde-support
```
## Building
```bash
# 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