request.rs 2.8 KB

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