use asl::asl::execution::{ Execution, ExecutionStatus, StateExecutionHandler, StateExecutionOutput, }; use asl::asl::state_machine::StateMachine; use asl::asl::types::execution::EmptyContext; use rstest::rstest; use serde_json::{json, Value}; use similar_asserts::assert_eq; struct TaskHandler {} impl StateExecutionHandler for TaskHandler { type TaskExecutionError = String; fn execute_task( &self, resource: &str, input: &Value, credentials: Option<&Value>, ) -> Result, Self::TaskExecutionError> { let result = match resource { "SayHello" => "Hello", "SayWorld" => "World", _ => Err("Unknown resource!")?, }; println!("{}", result); Ok(None) // We opt into returning nothing } } #[rstest] fn execute_hello_world() { let definition = r#"{ "Comment": "A simple minimal example of the States language", "StartAt": "State: Hello", "States": { "State: Hello": { "Type": "Task", "Resource": "SayHello", "Next": "State: World" }, "State: World": { "Type": "Task", "Resource": "SayWorld", "End": true } } }"#; let state_machine = StateMachine::parse(definition).unwrap(); let input = Value::Null; let execution: Execution = state_machine.start(&input, TaskHandler {}, EmptyContext {}); // the Execution type implements Iterator, so we can iterate until there are no more states to execute let steps: Vec = execution.collect(); assert_eq!( steps[0], StateExecutionOutput { status: ExecutionStatus::Executing, state_name: Some("State: Hello".to_string()), result: Some(json!({})), // returning nothing is the same as returning an empty JSON object } ); // assert_eq!( steps[1], StateExecutionOutput { status: ExecutionStatus::FinishedWithSuccess(Some(json!({}))), state_name: Some("State: World".to_string()), result: Some(json!({})), } ); }