crunch/crates/crunch-envelope/src/proto_envelope.rs
kjuulh 8a648c98cc
chore: fix
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-09-24 01:14:58 +02:00

28 lines
691 B
Rust

use prost::Message;
use crate::generated::crunch::*;
use crate::EnvelopeError;
pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8> {
let out = Envelope {
metadata: Some(Metadata {
domain: domain.to_string(),
entity: entity.to_string(),
timestamp: 0,
sequence: 0,
}),
content: content.to_vec(),
};
out.encode_to_vec()
}
pub fn unwrap(message: &[u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
let out = Envelope::decode(message).map_err(EnvelopeError::ProtoError)?;
Ok((
out.content,
out.metadata.ok_or(EnvelopeError::MetadataError())?,
))
}