data.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. use crate::{module::Event, request::Payload, response::EventResponse};
  2. use derivative::*;
  3. use std::{
  4. fmt::{Debug, Display},
  5. hash::Hash,
  6. };
  7. // #[derive(Debug)]
  8. // pub struct SenderPayload {
  9. // pub(crate) payload: Payload,
  10. // pub(crate) event: Event,
  11. // }
  12. //
  13. // impl SenderPayload {
  14. // pub fn new<E>(event: E) -> SenderPayload
  15. // where
  16. // E: Eq + Hash + Debug + Clone + Display,
  17. // {
  18. // Self {
  19. // event: event.into(),
  20. // payload: Payload::None,
  21. // }
  22. // }
  23. //
  24. // pub fn payload(mut self, payload: Payload) -> Self {
  25. // self.payload = payload;
  26. // self
  27. // }
  28. //
  29. // pub fn from_bytes(bytes: Vec<u8>) -> Self { unimplemented!() }
  30. // }
  31. //
  32. // impl std::convert::Into<ModuleRequest> for SenderPayload {
  33. // fn into(self) -> ModuleRequest {
  34. // ModuleRequest::new(self.event).payload(self.payload) } }
  35. //
  36. // impl std::default::Default for SenderPayload {
  37. // fn default() -> Self { SenderPayload::new("").payload(Payload::None) }
  38. // }
  39. //
  40. // impl std::convert::Into<EventRequest> for SenderPayload {
  41. // fn into(self) -> EventRequest { unimplemented!() }
  42. // }
  43. pub type BoxStreamCallback<T> = Box<dyn FnOnce(T, EventResponse) + 'static + Send + Sync>;
  44. // #[derive(Debug)]
  45. // pub struct SenderRequest2<T, C>
  46. // where
  47. // T: 'static + Debug,
  48. // C: FnOnce(T, EventResponse) + 'static,
  49. // {
  50. // pub config: T,
  51. // pub event: Event,
  52. // pub payload: Option<Payload>,
  53. // pub callback: Box<dyn C>,
  54. // }
  55. #[derive(Derivative)]
  56. #[derivative(Debug)]
  57. pub struct SenderRequest<T>
  58. where
  59. T: 'static + Debug,
  60. {
  61. pub config: T,
  62. pub event: Event,
  63. pub payload: Option<Payload>,
  64. #[derivative(Debug = "ignore")]
  65. pub callback: Option<BoxStreamCallback<T>>,
  66. }
  67. impl<T> SenderRequest<T>
  68. where
  69. T: 'static + Debug,
  70. {
  71. pub fn new<E>(config: T, event: E) -> Self
  72. where
  73. E: Eq + Hash + Debug + Clone + Display,
  74. {
  75. Self {
  76. config,
  77. payload: None,
  78. event: event.into(),
  79. callback: None,
  80. }
  81. }
  82. pub fn payload(mut self, payload: Payload) -> Self {
  83. self.payload = Some(payload);
  84. self
  85. }
  86. pub fn callback<F>(mut self, callback: F) -> Self
  87. where
  88. F: FnOnce(T, EventResponse) + 'static + Send + Sync,
  89. {
  90. self.callback = Some(Box::new(callback));
  91. self
  92. }
  93. }