Typed-eval is a type-safe expression evaluation engine

⚓ Rust    📅 2025-08-22    👤 surdeus    👁️ 5      

surdeus

typed-eval is a type-safe expression evaluation engine for Rust. It lets you compile and execute dynamic expressions against a strongly typed context objects with full type checking at compile time.

It works by combining closures to produce the closure returning the result of the compiled expression.

Example:

use typed_eval::{Compiler, SupportedType};

#[derive(SupportedType)]
struct User {
    name: String,
    age: i64,
}

#[derive(SupportedType)]
struct Context {
    user: User,
    greet: Box<dyn Fn(String) -> String>,
}

fn main() {
    let compiler = Compiler::new();

    let greet_user = compiler.compile::<String>("greet(user.name)").unwrap();
    let double_age = compiler.compile::<i64>("user.age * 2").unwrap();

    let context = Context {
        user: User {
            name: "Bob".into(),
            age: 45,
        },
        greet: Box::new(|name| format!("Hello, {name}")),
    };

    assert_eq!(greet_user.call(&context), "Hello, Bob");
    assert_eq!(double_age.call(&context), 90);
}

It is an experimental project I wanted to implement for some time.
It is far from ready, but I believe there is already something to look at.

1 post - 1 participant

Read full topic

🏷️ Rust_feed