data.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(InternalError::DeserializeFromBytes(format!("{}", e)).into())),
  40. },
  41. }
  42. }
  43. }
  44. impl<T> AFPluginResponder for AFPluginData<T>
  45. where
  46. T: ToBytes,
  47. {
  48. fn respond_to(self, _request: &AFPluginEventRequest) -> AFPluginEventResponse {
  49. match self.into_inner().into_bytes() {
  50. Ok(bytes) => {
  51. log::trace!("Serialize Data: {:?} to event response", std::any::type_name::<T>());
  52. ResponseBuilder::Ok().data(bytes).build()
  53. }
  54. Err(e) => e.into(),
  55. }
  56. }
  57. }
  58. impl<T> std::convert::TryFrom<&Payload> for AFPluginData<T>
  59. where
  60. T: AFPluginFromBytes,
  61. {
  62. type Error = DispatchError;
  63. fn try_from(payload: &Payload) -> Result<AFPluginData<T>, Self::Error> {
  64. parse_payload(payload)
  65. }
  66. }
  67. impl<T> std::convert::TryFrom<Payload> for AFPluginData<T>
  68. where
  69. T: AFPluginFromBytes,
  70. {
  71. type Error = DispatchError;
  72. fn try_from(payload: Payload) -> Result<AFPluginData<T>, Self::Error> {
  73. parse_payload(&payload)
  74. }
  75. }
  76. fn parse_payload<T>(payload: &Payload) -> Result<AFPluginData<T>, DispatchError>
  77. where
  78. T: AFPluginFromBytes,
  79. {
  80. match payload {
  81. Payload::None => Err(InternalError::UnexpectedNone(format!(
  82. "Parse fail, expected payload:{:?}",
  83. std::any::type_name::<T>()
  84. ))
  85. .into()),
  86. Payload::Bytes(bytes) => {
  87. let data = T::parse_from_bytes(bytes.clone())?;
  88. Ok(AFPluginData(data))
  89. }
  90. }
  91. }
  92. impl<T> std::convert::TryInto<Payload> for AFPluginData<T>
  93. where
  94. T: ToBytes,
  95. {
  96. type Error = DispatchError;
  97. fn try_into(self) -> Result<Payload, Self::Error> {
  98. let inner = self.into_inner();
  99. let bytes = inner.into_bytes()?;
  100. Ok(Payload::Bytes(bytes))
  101. }
  102. }
  103. impl ToBytes for AFPluginData<String> {
  104. fn into_bytes(self) -> Result<Bytes, DispatchError> {
  105. Ok(Bytes::from(self.0))
  106. }
  107. }