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

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(())
}