| Crates.io | rhai_trace |
| lib.rs | rhai_trace |
| version | 0.3.1 |
| created_at | 2025-09-04 11:32:15.599872+00 |
| updated_at | 2025-10-24 13:25:15.855563+00 |
| description | A small library providing better error and span support for Rhai, the embeddable programming language. |
| homepage | https://byson94.is-a.dev/rhai_trace/ |
| repository | https://github.com/byson94/rhai_trace |
| max_upload_size | |
| id | 1824070 |
| size | 62,604 |
rhai_trace is a lightweight Rust library that enhances Rhai scripts with better error reporting and span tracking. It walks the script's AST, extracts spans for statements and expressions, and provides structured error information that can be used with any diagnostic or pretty-printing crate.
With rhai_trace, you can:
ariadne or other diagnostic systems.use rhai_trace::{SpanTracer, BetterError};
use rhai::Engine;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Example Rhai code
let code = r#"
let a = 42;
let b = a + 1;
fn multiply(x, y) { x * y }
let c = multiply("a", 7); // <-- will trigger runtime error
"#;
// Initialize the span tracer
let tracer = SpanTracer::new();
// Extract spans from the code
let spans = tracer.extract_from(code)?;
println!("Extracted spans:");
for span in &spans {
println!("{}..{}: '{}'",
span.start(),
span.end(),
&code[span.start()..span.end()]
);
}
// Attempt to execute the code with Rhai engine
let engine = Engine::new();
match engine.eval::<rhai::Dynamic>(code) {
Ok(result) => println!("Execution result: {:?}", result),
Err(e) => {
// Improve the error using our library
if let Ok(better) = BetterError::improve_eval_error(&e, code, &engine) {
// ...
} else {
eprintln!("Original Error: {:?}", e);
}
}
}
Ok(())
}
For a complete working example showing integration with ariadne for pretty error reporting, see the example folder in the repository: