fluvio_smartengine/engine/wasmtime/
look_back.rs1use std::convert::TryFrom;
2use std::fmt::Debug;
3
4use anyhow::{Result, Ok};
5use fluvio_smartmodule::dataplane::smartmodule::{
6 SmartModuleLookbackOutput, SmartModuleLookbackErrorStatus, SmartModuleInput,
7};
8use wasmtime::{AsContextMut, TypedFunc};
9
10use super::instance::SmartModuleInstanceContext;
11
12const LOOKBACK_FN_NAME: &str = "look_back";
13type LookBackFn = TypedFunc<(i32, i32, u32), i32>;
14
15pub(crate) struct SmartModuleLookBack(LookBackFn);
16
17impl Debug for SmartModuleLookBack {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 write!(f, "LookBackFn")
20 }
21}
22
23impl SmartModuleLookBack {
24 pub fn try_instantiate(
25 ctx: &SmartModuleInstanceContext,
26 store: &mut impl AsContextMut,
27 ) -> Result<Option<Self>> {
28 match ctx.get_wasm_func(store, LOOKBACK_FN_NAME) {
29 Some(func) => func
31 .typed(&mut *store)
32 .or_else(|_| func.typed(store))
33 .map(Self)
34 .map(Some),
35 None => Ok(None),
36 }
37 }
38
39 pub(crate) fn call(
40 &mut self,
41 input: SmartModuleInput,
42 ctx: &mut SmartModuleInstanceContext,
43 store: &mut impl AsContextMut,
44 ) -> Result<()> {
45 let slice = ctx.write_input(&input, &mut *store)?;
46 let output = self.0.call(&mut *store, slice)?;
47
48 if output < 0 {
49 let internal_error = SmartModuleLookbackErrorStatus::try_from(output)
50 .unwrap_or(SmartModuleLookbackErrorStatus::UnknownError);
51
52 match internal_error {
53 SmartModuleLookbackErrorStatus::PropagatedError => {
54 let output: SmartModuleLookbackOutput = ctx.read_output(store)?;
55 Err(output.error.into())
56 }
57 _ => Err(internal_error.into()),
58 }
59 } else {
60 Ok(())
61 }
62 }
63}