use serde_json::json;
use taskflow_rs::executor::handlers::{FileTaskHandler, PythonTaskHandler};
use taskflow_rs::task::{Task, TaskDefinition, TaskHandler};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Testing handler execution directly...\n");
println!("=== Testing Python Handler ===");
let python_handler = PythonTaskHandler::new();
let python_task = Task::new(
TaskDefinition::new("test-python", "python_script")
.with_payload(
"script",
json!("print('Hello from direct execution!'); x = 5 + 3; print(f'5 + 3 = {x}')"),
)
.with_payload("args", json!([])),
);
match python_handler.execute(&python_task).await {
Ok(result) => {
println!("Python execution successful: {}", result.success);
println!("Output: {:?}", result.output);
}
Err(e) => println!("Python execution failed: {}", e),
}
println!("\n=== Testing File Handler ===");
let file_handler = FileTaskHandler::new();
let create_task = Task::new(
TaskDefinition::new("test-create", "file_operation")
.with_payload("operation", json!("write"))
.with_payload("path", json!("/tmp/test_direct.txt"))
.with_payload(
"content",
json!("This was created by direct handler execution\nTest successful!"),
),
);
match file_handler.execute(&create_task).await {
Ok(result) => {
println!("File creation successful: {}", result.success);
println!("Output: {:?}", result.output);
}
Err(e) => println!("File creation failed: {}", e),
}
println!("\n=== Testing File Read ===");
let read_task = Task::new(
TaskDefinition::new("test-read", "file_operation")
.with_payload("operation", json!("read"))
.with_payload("path", json!("/tmp/test_direct.txt")),
);
match file_handler.execute(&read_task).await {
Ok(result) => {
println!("File read successful: {}", result.success);
if let Some(content) = result.output {
println!("File content: {}", content);
}
}
Err(e) => println!("File read failed: {}", e),
}
println!("\n=== Cleaning Up ===");
let delete_task = Task::new(
TaskDefinition::new("test-delete", "file_operation")
.with_payload("operation", json!("delete"))
.with_payload("path", json!("/tmp/test_direct.txt")),
);
match file_handler.execute(&delete_task).await {
Ok(result) => println!("File cleanup successful: {}", result.success),
Err(e) => println!("File cleanup failed: {}", e),
}
println!("\nDirect handler execution test completed!");
Ok(())
}