This commit is contained in:
2025-04-03 13:12:08 +02:00
parent 4b1f305b07
commit a361f6d4ba
2 changed files with 99 additions and 91 deletions

View File

@@ -42,32 +42,26 @@ pub fn main() -> Result<(), String> {
// Test some functions from each script
println!("\nDynamic function testing:");
// Test string_utils functions
test_function(&script_manager, "string_utils:capitalize", vec![Dynamic::from("hello world")])?;
// Test math_utils functions
test_function(&script_manager, "math_utils:format_number", vec![Dynamic::from(1234567)])?;
// Test test_utils functions
test_function(&script_manager, "test_utils:reverse_string", vec![Dynamic::from("hello world")])?;
// Using direct script execution
println!("\nExecuting scripts directly:");
// 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
//TODO: should not start with string_utils we want capitalize as function usable as is
script_manager.run( "string_utils:capitalize(hello world);")?;
script_manager.run( "math_utils:format_number(1234567);")?;
// Test evaluating a complex script
println!("\nEvaluating a 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
let sentence = "Count the words in this sentence";
let count = test_utils:count_words(sentence);
print("Word count: " + truncate("count",1));
count * 2 // Return double the word count
"#;
execute_script_with_result(&script_manager, complex_script)?;
// Evaluate the script
println!("```\n{}\n```", complex_script);
match script_manager.eval::<Dynamic>(complex_script) {
Ok(result) => println!(" Result: {}", result),
Err(e) => println!(" Error: {}", e)
}
println!("\n=== DYNAMIC FUNCTION LOADING TEST COMPLETE ===\n");
@@ -102,44 +96,3 @@ fn display_functions_by_script(scripts: &HashMap<String, Vec<String>>) {
}
}
}
// 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(())
}