44 lines
1.1 KiB
Rust
Raw Normal View History

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 {}
// TODO: support stream data
2021-07-03 22:24:02 +08:00
#[derive(Clone, Debug, serde::Serialize)]
pub enum Payload {
2021-06-24 16:32:36 +08:00
None,
Bytes(Vec<u8>),
2021-06-24 16:32:36 +08:00
}
2021-07-03 22:24:02 +08:00
impl std::fmt::Display for Payload {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Payload::Bytes(bytes) => f.write_fmt(format_args!("{} bytes", bytes.len())),
Payload::None => f.write_str("Empty"),
}
}
}
impl std::convert::Into<Payload> for String {
fn into(self) -> Payload { Payload::Bytes(self.into_bytes()) }
}
impl std::convert::Into<Payload> for &'_ String {
fn into(self) -> Payload { Payload::Bytes(self.to_owned().into_bytes()) }
}
impl std::convert::Into<Payload> for Bytes {
fn into(self) -> Payload {
// Opti(nathan): do not copy the bytes?
Payload::Bytes(self.as_ref().to_vec())
}
}
impl std::convert::Into<Payload> for Vec<u8> {
fn into(self) -> Payload { Payload::Bytes(self) }
}
impl std::convert::Into<Payload> for &str {
fn into(self) -> Payload { self.to_string().into() }
}