payload.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use bytes::Bytes;
  2. use std::{fmt, fmt::Formatter};
  3. pub enum PayloadError {}
  4. // TODO: support stream data
  5. #[derive(Clone, Debug, serde::Serialize)]
  6. pub enum Payload {
  7. None,
  8. Bytes(Vec<u8>),
  9. }
  10. impl std::fmt::Display for Payload {
  11. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
  12. match self {
  13. Payload::Bytes(bytes) => f.write_fmt(format_args!("{} bytes", bytes.len())),
  14. Payload::None => f.write_str("Empty"),
  15. }
  16. }
  17. }
  18. impl std::convert::Into<Payload> for String {
  19. fn into(self) -> Payload { Payload::Bytes(self.into_bytes()) }
  20. }
  21. impl std::convert::Into<Payload> for &'_ String {
  22. fn into(self) -> Payload { Payload::Bytes(self.to_owned().into_bytes()) }
  23. }
  24. impl std::convert::Into<Payload> for Bytes {
  25. fn into(self) -> Payload {
  26. // Opti(nathan): do not copy the bytes?
  27. Payload::Bytes(self.as_ref().to_vec())
  28. }
  29. }
  30. impl std::convert::Into<Payload> for Vec<u8> {
  31. fn into(self) -> Payload { Payload::Bytes(self) }
  32. }
  33. impl std::convert::Into<Payload> for &str {
  34. fn into(self) -> Payload { self.to_string().into() }
  35. }