ayaka_plugin_nop/
lib.rs

1//! A no-operation plugin backend.
2
3#![warn(missing_docs)]
4
5use anyhow::bail;
6use ayaka_plugin::*;
7use std::collections::HashMap;
8
9/// A no-operation WASM module.
10///
11/// Any call will return an error.
12pub struct NopModule;
13
14impl RawModule for NopModule {
15    type Linker = NopLinker;
16
17    type LinkerHandle<'a> = NopLinker;
18
19    type Func = ();
20
21    fn call<T>(&self, name: &str, _args: &[u8], _f: impl FnOnce(&[u8]) -> Result<T>) -> Result<T> {
22        bail!("Trying to call {name}.")
23    }
24}
25
26/// A no-operation store & linker.
27pub struct NopLinker;
28
29impl Linker<NopModule> for NopLinker {
30    fn new() -> Result<Self> {
31        Ok(Self)
32    }
33
34    fn create(&self, _binary: &[u8]) -> Result<NopModule> {
35        Ok(NopModule)
36    }
37
38    fn import(&mut self, _ns: impl Into<String>, _funcs: HashMap<String, ()>) -> Result<()> {
39        Ok(())
40    }
41
42    fn wrap_raw(&self, _f: impl (Fn(Self, i32, i32) -> Result<Vec<u8>>) + Send + Sync + 'static) {}
43}
44
45impl<'a> LinkerHandle<'a, NopModule> for NopLinker {
46    fn call<T>(
47        &mut self,
48        _m: &NopModule,
49        name: &str,
50        _args: &[u8],
51        _f: impl FnOnce(&[u8]) -> Result<T>,
52    ) -> Result<T> {
53        bail!("Trying to call {name}.")
54    }
55
56    fn slice<T>(&self, _start: i32, _len: i32, _f: impl FnOnce(&[u8]) -> T) -> T {
57        unimplemented!("Trying to slice.")
58    }
59
60    fn slice_mut<T>(&mut self, _start: i32, _len: i32, _f: impl FnOnce(&mut [u8]) -> T) -> T {
61        unimplemented!("Trying to slice.")
62    }
63}