payload.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. use bytes::Bytes;
  2. use std::{fmt, fmt::Formatter};
  3. pub enum PayloadError {}
  4. // TODO: support stream data
  5. #[derive(Clone)]
  6. #[cfg_attr(feature = "use_serde", derive(serde::Serialize))]
  7. pub enum Payload {
  8. None,
  9. Bytes(Bytes),
  10. }
  11. impl std::fmt::Debug for Payload {
  12. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
  13. format_payload_print(self, f)
  14. }
  15. }
  16. impl std::fmt::Display for Payload {
  17. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
  18. format_payload_print(self, f)
  19. }
  20. }
  21. fn format_payload_print(payload: &Payload, f: &mut Formatter<'_>) -> fmt::Result {
  22. match payload {
  23. Payload::Bytes(bytes) => f.write_fmt(format_args!("{} bytes", bytes.len())),
  24. Payload::None => f.write_str("Empty"),
  25. }
  26. }
  27. impl std::convert::From<String> for Payload {
  28. fn from(s: String) -> Self {
  29. Payload::Bytes(Bytes::from(s))
  30. }
  31. }
  32. impl std::convert::From<&'_ String> for Payload {
  33. fn from(s: &String) -> Self {
  34. Payload::Bytes(Bytes::from(s.to_owned()))
  35. }
  36. }
  37. impl std::convert::From<Bytes> for Payload {
  38. fn from(bytes: Bytes) -> Self {
  39. Payload::Bytes(bytes)
  40. }
  41. }
  42. impl std::convert::From<()> for Payload {
  43. fn from(_: ()) -> Self {
  44. Payload::None
  45. }
  46. }
  47. impl std::convert::From<Vec<u8>> for Payload {
  48. fn from(bytes: Vec<u8>) -> Self {
  49. Payload::Bytes(Bytes::from(bytes))
  50. }
  51. }
  52. impl std::convert::From<&str> for Payload {
  53. fn from(s: &str) -> Self {
  54. s.to_string().into()
  55. }
  56. }