use bytes::Bytes;
use super::meta::EventMeta;
pub trait IntoRedexPayload {
fn into_redex_payload(self) -> (EventMeta, Bytes);
}
#[derive(Debug, Clone)]
pub struct EventEnvelope {
pub meta: EventMeta,
pub payload: Bytes,
}
impl EventEnvelope {
pub fn new(meta: EventMeta, payload: impl Into<Bytes>) -> Self {
Self {
meta,
payload: payload.into(),
}
}
}
impl IntoRedexPayload for EventEnvelope {
fn into_redex_payload(self) -> (EventMeta, Bytes) {
(self.meta, self.payload)
}
}
impl IntoRedexPayload for (EventMeta, Bytes) {
fn into_redex_payload(self) -> (EventMeta, Bytes) {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_envelope_into_payload_roundtrip() {
let meta = EventMeta::new(0x10, 0, 0xAA, 1, 0xBB);
let tail = Bytes::from_static(b"hello");
let env = EventEnvelope::new(meta, tail.clone());
let (meta_out, tail_out) = env.into_redex_payload();
assert_eq!(meta_out, meta);
assert_eq!(tail_out, tail);
}
#[test]
fn test_tuple_impl() {
let meta = EventMeta::new(0, 0, 0, 0, 0);
let tail = Bytes::from_static(b"x");
let (m, t) = (meta, tail.clone()).into_redex_payload();
assert_eq!(m, meta);
assert_eq!(t, tail);
}
}