data.rs 2.9 KB

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