atomfn 0.1.0

AtomService 函数服务 Rust SDK:与 TS SDK 协议一致的常驻 HTTP 运行时
Documentation
use atomfn::{serve, Ctx, FunctionError};
use serde::{Deserialize, Serialize};

#[derive(Deserialize)]
struct AddInput {
    a: i64,
    b: i64,
}

#[derive(Serialize)]
struct AddOutput {
    sum: i64,
}

#[derive(Deserialize)]
struct TickInput {
    n: i64,
}

#[derive(Serialize)]
struct Tick {
    step: i64,
}

fn add(input: AddInput, ctx: &Ctx) -> Result<AddOutput, FunctionError> {
    ctx.info("adding");
    if input.a < 0 {
        return Err(FunctionError::business(
            "NEGATIVE",
            serde_json::json!({ "value": input.a }),
        ));
    }
    Ok(AddOutput {
        sum: input.a + input.b,
    })
}

fn tick(input: TickInput, _ctx: &Ctx) -> Result<impl Iterator<Item = Tick>, FunctionError> {
    Ok((1..=input.n).map(|step| Tick { step }))
}

serve! {
    add,
    stream tick,
}