2021-06-24 16:32:36 +08:00
|
|
|
use std::future::Future;
|
|
|
|
|
2021-06-25 23:53:13 +08:00
|
|
|
use crate::{
|
2023-02-13 09:29:49 +08:00
|
|
|
request::{payload::Payload, AFPluginEventRequest},
|
|
|
|
response::AFPluginEventResponse,
|
2021-06-25 23:53:13 +08:00
|
|
|
};
|
|
|
|
|
2021-06-24 16:32:36 +08:00
|
|
|
pub trait Service<Request> {
|
2023-02-13 09:29:49 +08:00
|
|
|
type Response;
|
|
|
|
type Error;
|
|
|
|
type Future: Future<Output = Result<Self::Response, Self::Error>>;
|
2021-06-24 16:32:36 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
fn call(&self, req: Request) -> Self::Future;
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:59:22 +08:00
|
|
|
/// Returns a future that can handle the request. For the moment, the request will be the
|
|
|
|
/// `AFPluginRequest`
|
2022-12-01 08:35:50 +08:00
|
|
|
pub trait AFPluginServiceFactory<Request> {
|
2023-02-13 09:29:49 +08:00
|
|
|
type Response;
|
|
|
|
type Error;
|
|
|
|
type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
|
|
|
|
type Context;
|
|
|
|
type Future: Future<Output = Result<Self::Service, Self::Error>>;
|
2021-06-24 16:32:36 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
fn new_service(&self, cfg: Self::Context) -> Self::Future;
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 08:35:50 +08:00
|
|
|
pub(crate) struct ServiceRequest {
|
2023-02-13 09:29:49 +08:00
|
|
|
event_state: AFPluginEventRequest,
|
|
|
|
payload: Payload,
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ServiceRequest {
|
2023-02-13 09:29:49 +08:00
|
|
|
pub(crate) fn new(event_state: AFPluginEventRequest, payload: Payload) -> Self {
|
|
|
|
Self {
|
|
|
|
event_state,
|
|
|
|
payload,
|
2022-01-23 12:14:00 +08:00
|
|
|
}
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2021-06-24 23:37:45 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[inline]
|
|
|
|
pub(crate) fn into_parts(self) -> (AFPluginEventRequest, Payload) {
|
|
|
|
(self.event_state, self.payload)
|
|
|
|
}
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-27 15:11:41 +08:00
|
|
|
pub struct ServiceResponse {
|
2023-02-13 09:29:49 +08:00
|
|
|
request: AFPluginEventRequest,
|
|
|
|
response: AFPluginEventResponse,
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-27 15:11:41 +08:00
|
|
|
impl ServiceResponse {
|
2023-02-13 09:29:49 +08:00
|
|
|
pub fn new(request: AFPluginEventRequest, response: AFPluginEventResponse) -> Self {
|
|
|
|
ServiceResponse { request, response }
|
|
|
|
}
|
2021-06-26 23:52:03 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
pub fn into_parts(self) -> (AFPluginEventRequest, AFPluginEventResponse) {
|
|
|
|
(self.request, self.response)
|
|
|
|
}
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|