Skip to main content

eval_async

Function eval_async 

Source
pub async fn eval_async<D>(
    node: &Node,
    ctx: Value,
    dispatcher: &D,
) -> Result<Value, EvalError>
where D: AsyncDispatcher + ?Sized,
Expand description

Evaluate a Node against a context value asynchronously, using the given async dispatcher for Step resolution.

Legacy Value-passing async evaluator — backward compat wrapper around eval_async_with_storage + MemoryCtx. 既存 caller (= dynamic injection を要求しない用途) は引き続きこの API で OK。

eval (sync) と同型 logic、 dispatch を .await に置き換え。 Seq / Branch は recursive async fn (= async_recursion macro で Pin<Box> wrap)。

§Quick start

use async_trait::async_trait;
use mlua_flow_ir::{eval_async, AsyncDispatcher, EvalError, Expr, Node};
use serde_json::{json, Value};

struct Fixture;

#[async_trait]
impl AsyncDispatcher for Fixture {
    async fn dispatch(&self, _r: &str, input: Value) -> Result<Value, EvalError> {
        if let Value::String(s) = input {
            Ok(Value::String(s.to_uppercase()))
        } else {
            Ok(input)
        }
    }
}

let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
    let node = Node::Step {
        ref_: "up".into(),
        in_: Expr::Path { at: "$.input".parse().unwrap() },
        out: Expr::Path { at: "$.output".parse().unwrap() },
    };
    let out = eval_async(&node, json!({ "input": "hello" }), &Fixture).await.unwrap();
    assert_eq!(out, json!({ "input": "hello", "output": "HELLO" }));
});