54 lines
1.4 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) }
}
2021-07-06 14:14:47 +08:00
// = note: conflicting implementation in crate `core`:
// - impl<T, U> TryInto<U> for T where U: TryFrom<T>;
//
// impl std::convert::TryInto<Payload> for Vec<u8> {
// type Error = String;
// fn try_into(self) -> Result<Payload, Self::Error> {
// Ok(Payload::Bytes(self))
// }
// }
2021-07-03 22:24:02 +08:00
impl std::convert::Into<Payload> for &str {
fn into(self) -> Payload { self.to_string().into() }
}