service.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. use std::future::Future;
  2. use crate::{
  3. request::{payload::Payload, AFPluginEventRequest},
  4. response::AFPluginEventResponse,
  5. };
  6. pub trait Service<Request> {
  7. type Response;
  8. type Error;
  9. type Future: Future<Output = Result<Self::Response, Self::Error>>;
  10. fn call(&self, req: Request) -> Self::Future;
  11. }
  12. /// Returns a future that can handle the request. For the moment, the request will be the
  13. /// `AFPluginRequest`
  14. pub trait AFPluginServiceFactory<Request> {
  15. type Response;
  16. type Error;
  17. type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
  18. type Context;
  19. type Future: Future<Output = Result<Self::Service, Self::Error>>;
  20. fn new_service(&self, cfg: Self::Context) -> Self::Future;
  21. }
  22. pub(crate) struct ServiceRequest {
  23. event_state: AFPluginEventRequest,
  24. payload: Payload,
  25. }
  26. impl ServiceRequest {
  27. pub(crate) fn new(event_state: AFPluginEventRequest, payload: Payload) -> Self {
  28. Self {
  29. event_state,
  30. payload,
  31. }
  32. }
  33. #[inline]
  34. pub(crate) fn into_parts(self) -> (AFPluginEventRequest, Payload) {
  35. (self.event_state, self.payload)
  36. }
  37. }
  38. pub struct ServiceResponse {
  39. request: AFPluginEventRequest,
  40. response: AFPluginEventResponse,
  41. }
  42. impl ServiceResponse {
  43. pub fn new(request: AFPluginEventRequest, response: AFPluginEventResponse) -> Self {
  44. ServiceResponse { request, response }
  45. }
  46. pub fn into_parts(self) -> (AFPluginEventRequest, AFPluginEventResponse) {
  47. (self.request, self.response)
  48. }
  49. }