This commit is contained in:
kristof 2025-04-03 12:49:08 +02:00
parent 452150a2fc
commit 4b1f305b07
3 changed files with 78 additions and 6 deletions

View File

@ -1,3 +1,6 @@
# rhaj
DSL's for our ecosystem
```bash
#tests, following one shows how we can dynamically load the functions from a set of rhai scripts
cargo run --bin test_dynamic_loading
```

View File

@ -51,11 +51,23 @@ pub fn main() -> Result<(), String> {
// Test test_utils functions
test_function(&script_manager, "test_utils:reverse_string", vec![Dynamic::from("hello world")])?;
test_function(&script_manager, "test_utils:count_words",
vec![Dynamic::from("this is a test sentence")])?;
// Using direct script execution
println!("\nExecuting scripts directly:");
test_function(&script_manager, "test_utils:factorial",
vec![Dynamic::from(5)])?;
// Direct function call with arguments as a script
execute_script(&script_manager, "test_utils:count_words(\"this is a test sentence\")")?;
// Direct script with expression
execute_script(&script_manager, "test_utils:factorial(5)")?;
// Run a more complex script
let complex_script = r#"
let result = test_utils:count_words("Count the words in this sentence");
print("Word count: " + result);
result * 2 // Return double the word count
"#;
execute_script_with_result(&script_manager, complex_script)?;
println!("\n=== DYNAMIC FUNCTION LOADING TEST COMPLETE ===\n");
@ -91,9 +103,43 @@ fn display_functions_by_script(scripts: &HashMap<String, Vec<String>>) {
}
}
// Helper function to test a function and display its result
// Helper function to test a function call and display its result
fn test_function(script_manager: &ScriptManager, name: &str, args: Vec<Dynamic>) -> Result<(), String> {
// Call the function with the provided arguments
let result = script_manager.call_function(name, args)?;
// Print the result
println!(" {}() => {}", name, result);
Ok(())
}
// Execute a script directly and display the result
fn execute_script(script_manager: &ScriptManager, script: &str) -> Result<(), String> {
println!(" Executing: {}", script);
// Use the run method to execute the script
script_manager.run(script)?;
// For functions that return a value, we can use eval to get the result
if let Ok(result) = script_manager.eval::<Dynamic>(script) {
println!(" Result: {}", result);
}
Ok(())
}
// Execute a script and return its result
fn execute_script_with_result(script_manager: &ScriptManager, script: &str) -> Result<(), String> {
println!(" Executing script with result:");
println!("```");
println!("{}", script);
println!("```");
// Use the eval method to execute the script and get the result
let result = script_manager.eval::<Dynamic>(script)?;
println!(" Result: {}", result);
Ok(())
}

View File

@ -290,4 +290,27 @@ impl ScriptManager {
pub fn get_function_names(&self) -> Vec<String> {
self.functions.keys().cloned().collect()
}
/// Execute a script snippet directly
pub fn eval<T: Clone>(&self, script: &str) -> Result<T, String>
where T: 'static + Clone {
// Create a configured engine for execution
let engine = create_standard_engine();
let mut scope = Scope::new();
// Execute the script and return the result
engine.eval_with_scope::<T>(&mut scope, script)
.map_err(|e| format!("Error evaluating script: {}", e))
}
/// Run a script without returning a value
pub fn run(&self, script: &str) -> Result<(), String> {
// Create a configured engine for execution
let engine = create_standard_engine();
let mut scope = Scope::new();
// Run the script
engine.run_with_scope(&mut scope, script)
.map_err(|e| format!("Error running script: {}", e))
}
}