amqp_api_server/api/input/
input_element.rs1use std::future::Future;
2use std::pin::Pin;
3use std::sync::Arc;
4use amqp_api_shared::request_result::RequestResult;
5
6use crate::api::input::request::Request;
7use crate::error::{Error, ErrorKind};
8use async_channel::Sender;
9use crate::config::amqp_input_api::AmqpInputApi;
10use crate::config::api::Api;
11
12pub type RequestHandler<LogicRequestType> = Arc<
13 dyn Fn(
14 Request,
15 Sender<LogicRequestType>,
16 ) -> Pin<Box<dyn Future<Output = RequestResult> + Send + Sync>>
17 + Send
18 + Sync,
19>;
20
21pub struct InputElement<LogicRequestType> {
22 name: String,
23 request_handler: RequestHandler<LogicRequestType>,
24 actions: &'static [&'static str],
25 config: AmqpInputApi,
26}
27
28impl<LogicRequestType> InputElement<LogicRequestType> {
29 fn new(
30 name: String,
31 request_handler: RequestHandler<LogicRequestType>,
32 actions: &'static [&'static str],
33 config: AmqpInputApi,
34 ) -> InputElement<LogicRequestType> {
35 InputElement {
36 name,
37 request_handler,
38 actions,
39 config,
40 }
41 }
42
43 pub fn name(&self) -> &str {
44 self.name.as_str()
45 }
46
47 pub fn request_handler(&self) -> RequestHandler<LogicRequestType> {
48 self.request_handler.clone()
49 }
50
51 pub fn actions(&self) -> &'static [&'static str] {
52 self.actions
53 }
54
55 pub fn config(&self) -> &AmqpInputApi {
56 &self.config
57 }
58}
59
60pub fn extract_input<LogicRequestType>(
61 api: &Api,
62 id: &str,
63 request_handler: RequestHandler<LogicRequestType>,
64 actions: &'static [&'static str],
65) -> Result<InputElement<LogicRequestType>, Error> {
66 let api_config = match api.input().iter().find(|api_config| api_config.id() == id) {
67 Some(api_config) => api_config,
68 None => {
69 return Err(Error::new(
70 ErrorKind::AutoConfigFailure,
71 format!("failed to find input api with id '{}'", id),
72 ))
73 }
74 };
75
76 Ok(InputElement::new(
77 id.to_string(),
78 request_handler,
79 actions,
80 api_config.clone(),
81 ))
82}