use std::any::Any;
use std::fmt::Debug;
use bytes::Bytes;
use serde_json::Value;
pub trait Payload: Debug + Any {
fn as_bytes(&self) -> Bytes;
fn as_string(&self) -> String;
fn as_json(&self) -> Option<Value> {
None
}
}
#[derive(Clone, Debug)]
pub struct StringPayload(pub String);
impl Payload for StringPayload {
fn as_bytes(&self) -> Bytes {
Bytes::from(self.0.clone())
}
fn as_string(&self) -> String {
self.0.clone()
}
}
#[derive(Clone, Debug)]
pub struct EmptyPayload;
impl Payload for EmptyPayload {
fn as_bytes(&self) -> Bytes {
Bytes::new()
}
fn as_string(&self) -> String {
String::new()
}
}
#[derive(Clone, Debug)]
pub struct JsonPayload(pub Value);
impl Payload for JsonPayload {
fn as_bytes(&self) -> Bytes {
Bytes::from(self.0.to_string())
}
fn as_string(&self) -> String {
self.0.to_string()
}
fn as_json(&self) -> Option<Value> {
Some(self.0.clone())
}
}