request.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. use std::future::Future;
  2. use crate::{
  3. errors::{DispatchError, InternalError},
  4. module::{Event, ModuleDataMap},
  5. request::payload::Payload,
  6. util::ready::{ready, Ready},
  7. };
  8. use derivative::*;
  9. use futures_core::ready;
  10. use std::{
  11. fmt::Debug,
  12. pin::Pin,
  13. sync::Arc,
  14. task::{Context, Poll},
  15. };
  16. #[derive(Clone, Debug, Derivative)]
  17. pub struct EventRequest {
  18. pub(crate) id: String,
  19. pub(crate) event: Event,
  20. #[derivative(Debug = "ignore")]
  21. pub(crate) module_data: Arc<ModuleDataMap>,
  22. }
  23. impl EventRequest {
  24. pub fn new<E>(id: String, event: E, module_data: Arc<ModuleDataMap>) -> EventRequest
  25. where
  26. E: Into<Event>,
  27. {
  28. Self {
  29. id,
  30. event: event.into(),
  31. module_data,
  32. }
  33. }
  34. pub fn module_data<T: 'static>(&self) -> Option<&T>
  35. where
  36. T: Send + Sync,
  37. {
  38. if let Some(data) = self.module_data.get::<T>() {
  39. return Some(data);
  40. }
  41. None
  42. }
  43. }
  44. pub trait FromRequest: Sized {
  45. type Error: Into<DispatchError>;
  46. type Future: Future<Output = Result<Self, Self::Error>>;
  47. fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future;
  48. }
  49. #[doc(hidden)]
  50. impl FromRequest for () {
  51. type Error = DispatchError;
  52. type Future = Ready<Result<(), DispatchError>>;
  53. fn from_request(_req: &EventRequest, _payload: &mut Payload) -> Self::Future { ready(Ok(())) }
  54. }
  55. #[doc(hidden)]
  56. impl FromRequest for String {
  57. type Error = DispatchError;
  58. type Future = Ready<Result<Self, Self::Error>>;
  59. fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
  60. match &payload {
  61. Payload::None => ready(Err(unexpected_none_payload(req))),
  62. Payload::Bytes(buf) => ready(Ok(String::from_utf8_lossy(buf).into_owned())),
  63. }
  64. }
  65. }
  66. pub fn unexpected_none_payload(request: &EventRequest) -> DispatchError {
  67. log::warn!("{:?} expected payload", &request.event);
  68. InternalError::new("Expected payload").into()
  69. }
  70. #[doc(hidden)]
  71. impl<T> FromRequest for Result<T, T::Error>
  72. where
  73. T: FromRequest,
  74. {
  75. type Error = DispatchError;
  76. type Future = FromRequestFuture<T::Future>;
  77. fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
  78. FromRequestFuture {
  79. fut: T::from_request(req, payload),
  80. }
  81. }
  82. }
  83. #[pin_project::pin_project]
  84. pub struct FromRequestFuture<Fut> {
  85. #[pin]
  86. fut: Fut,
  87. }
  88. impl<Fut, T, E> Future for FromRequestFuture<Fut>
  89. where
  90. Fut: Future<Output = Result<T, E>>,
  91. {
  92. type Output = Result<Result<T, E>, DispatchError>;
  93. fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
  94. let this = self.project();
  95. let res = ready!(this.fut.poll(cx));
  96. Poll::Ready(Ok(res))
  97. }
  98. }