2021-07-03 22:24:02 +08:00
|
|
|
use bytes::Bytes;
|
|
|
|
use std::{fmt, fmt::Formatter};
|
|
|
|
|
2021-06-24 16:32:36 +08:00
|
|
|
pub enum PayloadError {}
|
|
|
|
|
2021-06-30 15:33:49 +08:00
|
|
|
// TODO: support stream data
|
2022-07-06 13:27:33 +08:00
|
|
|
#[derive(Clone)]
|
2022-07-09 23:28:15 +08:00
|
|
|
#[cfg_attr(feature = "use_serde", derive(serde::Serialize))]
|
2021-06-30 15:33:49 +08:00
|
|
|
pub enum Payload {
|
2021-06-24 16:32:36 +08:00
|
|
|
None,
|
2021-08-20 22:00:03 +08:00
|
|
|
Bytes(Bytes),
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
2021-07-03 22:24:02 +08:00
|
|
|
|
2021-07-09 14:02:42 +08:00
|
|
|
impl std::fmt::Debug for Payload {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
format_payload_print(self, f)
|
|
|
|
}
|
2021-07-09 14:02:42 +08:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:24:02 +08:00
|
|
|
impl std::fmt::Display for Payload {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
format_payload_print(self, f)
|
|
|
|
}
|
2021-07-09 14:02:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn format_payload_print(payload: &Payload, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
match payload {
|
|
|
|
Payload::Bytes(bytes) => f.write_fmt(format_args!("{} bytes", bytes.len())),
|
|
|
|
Payload::None => f.write_str("Empty"),
|
2021-07-03 22:24:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 19:19:41 +08:00
|
|
|
impl std::convert::From<String> for Payload {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn from(s: String) -> Self {
|
|
|
|
Payload::Bytes(Bytes::from(s))
|
|
|
|
}
|
2021-07-03 22:24:02 +08:00
|
|
|
}
|
|
|
|
|
2021-11-27 19:19:41 +08:00
|
|
|
impl std::convert::From<&'_ String> for Payload {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn from(s: &String) -> Self {
|
|
|
|
Payload::Bytes(Bytes::from(s.to_owned()))
|
|
|
|
}
|
2021-07-03 22:24:02 +08:00
|
|
|
}
|
|
|
|
|
2021-11-27 19:19:41 +08:00
|
|
|
impl std::convert::From<Bytes> for Payload {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn from(bytes: Bytes) -> Self {
|
|
|
|
Payload::Bytes(bytes)
|
|
|
|
}
|
2021-07-03 22:24:02 +08:00
|
|
|
}
|
|
|
|
|
2021-11-27 19:19:41 +08:00
|
|
|
impl std::convert::From<()> for Payload {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn from(_: ()) -> Self {
|
|
|
|
Payload::None
|
|
|
|
}
|
2021-07-11 17:38:03 +08:00
|
|
|
}
|
2021-11-27 19:19:41 +08:00
|
|
|
impl std::convert::From<Vec<u8>> for Payload {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn from(bytes: Vec<u8>) -> Self {
|
|
|
|
Payload::Bytes(Bytes::from(bytes))
|
|
|
|
}
|
2021-07-03 22:24:02 +08:00
|
|
|
}
|
|
|
|
|
2021-11-27 19:19:41 +08:00
|
|
|
impl std::convert::From<&str> for Payload {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn from(s: &str) -> Self {
|
|
|
|
s.to_string().into()
|
|
|
|
}
|
2021-07-03 22:24:02 +08:00
|
|
|
}
|