rings_node/extension/ext/
envelope.rs1#![warn(missing_docs)]
2use bytes::Bytes;
5use serde::Deserialize;
6use serde::Serialize;
7
8use crate::error::Error;
9use crate::error::Result;
10
11#[derive(Clone, Debug, Serialize, Deserialize)]
14pub struct Envelope {
15 pub namespace: String,
17 pub payload: Bytes,
19}
20
21impl Envelope {
22 pub fn new(namespace: impl Into<String>, payload: Bytes) -> Self {
24 Self {
25 namespace: namespace.into(),
26 payload,
27 }
28 }
29
30 pub fn encode(&self) -> Result<Vec<u8>> {
32 bincode::serialize(self).map_err(|_| Error::EncodeError)
33 }
34
35 pub fn decode(bytes: &[u8]) -> Result<Self> {
37 bincode::deserialize(bytes).map_err(|_| Error::DecodeError)
38 }
39}