data.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. use std::fmt::{Debug, Formatter};
  2. use std::ops;
  3. use bytes::Bytes;
  4. use crate::{
  5. byte_trait::*,
  6. errors::{DispatchError, InternalError},
  7. request::{unexpected_none_payload, AFPluginEventRequest, FromAFPluginRequest, Payload},
  8. response::{AFPluginEventResponse, AFPluginResponder, ResponseBuilder},
  9. util::ready::{ready, Ready},
  10. };
  11. pub struct AFPluginData<T>(pub T);
  12. impl<T> AFPluginData<T> {
  13. pub fn into_inner(self) -> T {
  14. self.0
  15. }
  16. }
  17. impl<T> ops::Deref for AFPluginData<T> {
  18. type Target = T;
  19. fn deref(&self) -> &T {
  20. &self.0
  21. }
  22. }
  23. impl<T> ops::DerefMut for AFPluginData<T> {
  24. fn deref_mut(&mut self) -> &mut T {
  25. &mut self.0
  26. }
  27. }
  28. impl<T> FromAFPluginRequest for AFPluginData<T>
  29. where
  30. T: AFPluginFromBytes + 'static,
  31. {
  32. type Error = DispatchError;
  33. type Future = Ready<Result<Self, DispatchError>>;
  34. #[inline]
  35. fn from_request(req: &AFPluginEventRequest, payload: &mut Payload) -> Self::Future {
  36. match payload {
  37. Payload::None => ready(Err(unexpected_none_payload(req))),
  38. Payload::Bytes(bytes) => match T::parse_from_bytes(bytes.clone()) {
  39. Ok(data) => ready(Ok(AFPluginData(data))),
  40. Err(e) => ready(Err(
  41. InternalError::DeserializeFromBytes(format!("{}", e)).into(),
  42. )),
  43. },
  44. }
  45. }
  46. }
  47. impl<T> AFPluginResponder for AFPluginData<T>
  48. where
  49. T: ToBytes,
  50. {
  51. fn respond_to(self, _request: &AFPluginEventRequest) -> AFPluginEventResponse {
  52. match self.into_inner().into_bytes() {
  53. Ok(bytes) => {
  54. log::trace!(
  55. "Serialize Data: {:?} to event response",
  56. std::any::type_name::<T>()
  57. );
  58. ResponseBuilder::Ok().data(bytes).build()
  59. },
  60. Err(e) => e.into(),
  61. }
  62. }
  63. }
  64. impl<T> std::convert::TryFrom<&Payload> for AFPluginData<T>
  65. where
  66. T: AFPluginFromBytes,
  67. {
  68. type Error = DispatchError;
  69. fn try_from(payload: &Payload) -> Result<AFPluginData<T>, Self::Error> {
  70. parse_payload(payload)
  71. }
  72. }
  73. impl<T> std::convert::TryFrom<Payload> for AFPluginData<T>
  74. where
  75. T: AFPluginFromBytes,
  76. {
  77. type Error = DispatchError;
  78. fn try_from(payload: Payload) -> Result<AFPluginData<T>, Self::Error> {
  79. parse_payload(&payload)
  80. }
  81. }
  82. fn parse_payload<T>(payload: &Payload) -> Result<AFPluginData<T>, DispatchError>
  83. where
  84. T: AFPluginFromBytes,
  85. {
  86. match payload {
  87. Payload::None => Err(
  88. InternalError::UnexpectedNone(format!(
  89. "Parse fail, expected payload:{:?}",
  90. std::any::type_name::<T>()
  91. ))
  92. .into(),
  93. ),
  94. Payload::Bytes(bytes) => {
  95. let data = T::parse_from_bytes(bytes.clone())?;
  96. Ok(AFPluginData(data))
  97. },
  98. }
  99. }
  100. impl<T> std::convert::TryInto<Payload> for AFPluginData<T>
  101. where
  102. T: ToBytes,
  103. {
  104. type Error = DispatchError;
  105. fn try_into(self) -> Result<Payload, Self::Error> {
  106. let inner = self.into_inner();
  107. let bytes = inner.into_bytes()?;
  108. Ok(Payload::Bytes(bytes))
  109. }
  110. }
  111. impl ToBytes for AFPluginData<String> {
  112. fn into_bytes(self) -> Result<Bytes, DispatchError> {
  113. Ok(Bytes::from(self.0))
  114. }
  115. }
  116. impl<T> Debug for AFPluginData<T>
  117. where
  118. T: Debug,
  119. {
  120. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  121. self.0.fmt(f)
  122. }
  123. }