2021-07-03 22:24:02 +08:00
|
|
|
use std::{fmt, fmt::Formatter};
|
|
|
|
|
2023-10-30 12:35:06 +08:00
|
|
|
use bytes::Bytes;
|
2021-06-24 16:32:36 +08:00
|
|
|
|
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 {
|
2023-02-13 09:29:49 +08:00
|
|
|
None,
|
|
|
|
Bytes(Bytes),
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
2021-07-03 22:24:02 +08:00
|
|
|
|
2023-01-17 16:27:17 +08:00
|
|
|
impl Payload {
|
2023-02-13 09:29:49 +08:00
|
|
|
pub fn to_vec(self) -> Vec<u8> {
|
|
|
|
match self {
|
|
|
|
Payload::None => vec![],
|
|
|
|
Payload::Bytes(bytes) => bytes.to_vec(),
|
2023-01-17 16:27:17 +08:00
|
|
|
}
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2023-01-17 16:27:17 +08:00
|
|
|
}
|
|
|
|
|
2021-07-09 14:02:42 +08:00
|
|
|
impl std::fmt::Debug for Payload {
|
2023-02-13 09:29:49 +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 {
|
2023-02-13 09:29:49 +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 {
|
2023-02-13 09:29:49 +08:00
|
|
|
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 {
|
2023-02-13 09:29:49 +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 {
|
2023-02-13 09:29:49 +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 {
|
2023-02-13 09:29:49 +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 {
|
2023-02-13 09:29:49 +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 {
|
2023-02-13 09:29:49 +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 {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn from(s: &str) -> Self {
|
|
|
|
s.to_string().into()
|
|
|
|
}
|
2021-07-03 22:24:02 +08:00
|
|
|
}
|