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,
}