plux_rs/context/
load_plugin_context.rs1use crate::{function::Function, utils::RegisterRequestError, Info, Plugin, Requests};
2
3pub struct LoadPluginContext<'a, 'b, O: Send + Sync, I: Info> {
4 plugin: &'b mut Plugin<'a, O, I>,
5 requests: &'b Requests,
6}
7
8impl<'a, 'b, O: Send + Sync, I: Info> LoadPluginContext<'a, 'b, O, I> {
9 pub(crate) fn new(plugin: &'b mut Plugin<'a, O, I>, requests: &'b Requests) -> Self {
10 Self { plugin, requests }
11 }
12
13 pub const fn plugin(&'b self) -> &'b Plugin<'a, O, I> {
14 self.plugin
15 }
16
17 pub const fn requests(&self) -> &'b Requests {
18 self.requests
19 }
20
21 pub fn register_request<F>(&mut self, request: F) -> Result<(), RegisterRequestError>
23 where
24 F: Function<Output = O> + 'static,
25 {
26 if let Some(req) = self.requests.iter().find(|req| *req.name == request.name()) {
27 for input in req.inputs.iter() {
28 request
29 .inputs()
30 .iter()
31 .find(|arg| *input == arg.ty)
32 .ok_or(RegisterRequestError::ArgumentsIncorrectly)?;
33 }
34
35 if req.output != request.output().map(|arg| arg.ty) {
36 return Err(RegisterRequestError::ArgumentsIncorrectly);
37 }
38 } else {
39 return Err(RegisterRequestError::NotFound);
40 }
41
42 self.plugin.requests.push(Box::new(request));
43
44 Ok(())
45 }
46}