adapter/lib.rs
1/// The Adapter trait is used to define the interface for an adapter.
2/// An adapter is a component that is used to convert the input from a client
3/// into a format that is understood by the service.
4///
5/// By making the Input, Output and Identifier generic, the end user must restrict
6/// the types that can be used with the adapter.
7///
8/// The implementation's of an adapter can further restrict generic types.
9/// For example, we can restrict a lua adapter to only work with types that can be
10/// converted to and from lua, and have a valid identifier. See [mlua adapter example](../examples/mlua/main.rs).
11///
12///
13/// ```rust
14/// use adapter::Adapter;
15///
16/// use mlua::prelude::*;
17///
18/// pub struct MLuaAdapter(pub Lua);
19///
20/// impl MLuaAdapter {
21/// pub fn new() -> MLuaAdapter {
22/// MLuaAdapter(Lua::new())
23/// }
24///
25/// pub fn from_lua(lua: Lua) -> MLuaAdapter {
26/// MLuaAdapter(lua)
27/// }
28/// }
29///
30/// impl<'lua, Input, Output, Identifier> Adapter<'lua, Input, Output, Identifier> for MLuaAdapter
31/// where
32/// Input: IntoLuaMulti<'lua>,
33/// Output: FromLuaMulti<'lua>,
34/// Identifier: IntoLua<'lua>,
35/// {
36/// type Error = mlua::Error;
37///
38/// fn call(&'lua mut self, identifier: Identifier, input: Input) -> Result<Output, Self::Error> {
39/// let lua = &self.0;
40/// let globals = lua.globals();
41/// let func: mlua::Function = globals.get(identifier)?;
42/// func.call::<Input, Output>(input)
43/// }
44/// }
45/// ```
46pub trait Adapter<'a, Input, Output, Identifier> {
47 /// Errors produced by the plugin provider.
48 type Error;
49
50 /// Process a call and return the response synchronously.
51 fn call(&'a mut self, identifier: Identifier, input: Input) -> Result<Output, Self::Error>;
52}