camel-language-rhai
Rhai scripting language for rust-camel
Overview
camel-language-rhai provides a Rhai scripting language integration for rust-camel, similar to Apache Camel's Groovy or JavaScript language support. Rhai is an embedded scripting language for Rust with no external dependencies.
Available Variables
Scripts have access to these variables injected from the Exchange:
| Variable | Type | Description |
|---|---|---|
body |
String | Message body as text |
headers |
Map | Message headers as a key-value map |
properties |
Map | Exchange properties as a key-value map |
Read-only API (create_expression / create_predicate)
| Function | Description |
|---|---|
header("name") |
Look up a header value by name |
set_header("key", value) |
Set a header visible within the same script evaluation (not propagated back) |
property("name") |
Look up an exchange property by name |
set_property("key", value) |
Set a property visible within the same script evaluation (not propagated back) |
Note:
set_headerandset_propertyin read-only mode are local to the script evaluation. Changes are not propagated back to the Exchange.
Mutating API (create_mutating_expression)
Use create_mutating_expression when you need script changes to persist back to the Exchange. The mutating engine uses direct map assignment instead of helper functions:
headers["tenant"] = "acme"; // set header — propagated back
properties["trace"] = "enabled"; // set property — propagated back
body = "new content"; // set body — propagated back
let v = headers["existing"]; // read header
Changes are applied atomically: if the script throws an error, all modifications are rolled back and the Exchange is restored to its pre-execution state.
The .script() builder method uses this API automatically:
from
.script
.to
.route_id
.build?;
Usage
use RhaiLanguage;
use Language;
let lang = new;
// Predicate using header lookup
let pred = lang.create_predicate.unwrap;
// Expression with string concatenation
let expr = lang.create_expression.unwrap;
// Full Rhai scripting
let expr = lang.create_expression.unwrap;
Registration
Register manually in CamelContext (not included by default to avoid the Rhai dependency):
use RhaiLanguage;
let mut ctx = new;
ctx.register_language.unwrap;
License
Apache-2.0