15
crates/crunch-envelope/src/envelope.proto
Normal file
15
crates/crunch-envelope/src/envelope.proto
Normal file
@@ -0,0 +1,15 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package crunch.envelope;
|
||||
|
||||
message Envelope {
|
||||
Metadata metadata = 1;
|
||||
bytes content = 2;
|
||||
}
|
||||
|
||||
message Metadata {
|
||||
string domain = 1;
|
||||
string entity = 2;
|
||||
uint64 timestamp = 3;
|
||||
uint64 sequence = 4;
|
||||
}
|
@@ -3,11 +3,19 @@ mod envelope_capnp;
|
||||
#[cfg(feature = "json")]
|
||||
mod json_envelope;
|
||||
|
||||
#[cfg(feature = "proto")]
|
||||
mod proto_envelope;
|
||||
|
||||
#[cfg(feature = "json")]
|
||||
pub mod json {
|
||||
pub use crate::json_envelope::*;
|
||||
}
|
||||
|
||||
#[cfg(feature = "proto")]
|
||||
pub mod proto {
|
||||
pub use crate::proto_envelope::*;
|
||||
}
|
||||
|
||||
use capnp::message::{Builder, ReaderOptions};
|
||||
use capnp::serialize;
|
||||
use thiserror::Error;
|
||||
@@ -22,6 +30,11 @@ pub enum EnvelopeError {
|
||||
#[cfg(feature = "json")]
|
||||
#[error("base64 failed to serialize or deserialize code")]
|
||||
Base64Error(#[source] base64::DecodeError),
|
||||
#[cfg(feature = "proto")]
|
||||
#[error("prost failed to serialize or deserialize code")]
|
||||
ProtoError(#[source] prost::DecodeError),
|
||||
#[error("metadata is missing from field")]
|
||||
MetadataError(),
|
||||
}
|
||||
|
||||
pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8> {
|
||||
|
30
crates/crunch-envelope/src/proto_envelope.rs
Normal file
30
crates/crunch-envelope/src/proto_envelope.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
pub mod envelope {
|
||||
include!(concat!(env!("OUT_DIR"), "/crunch.envelope.rs"));
|
||||
}
|
||||
|
||||
use prost::Message;
|
||||
|
||||
use crate::EnvelopeError;
|
||||
|
||||
pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8> {
|
||||
let out = envelope::Envelope {
|
||||
metadata: Some(envelope::Metadata {
|
||||
domain: domain.to_string(),
|
||||
entity: entity.to_string(),
|
||||
timestamp: 0,
|
||||
sequence: 0,
|
||||
}),
|
||||
content: content.to_vec(),
|
||||
};
|
||||
|
||||
out.encode_to_vec()
|
||||
}
|
||||
|
||||
pub fn unwrap<'a>(message: &'a [u8]) -> Result<(Vec<u8>, envelope::Metadata), EnvelopeError> {
|
||||
let out = envelope::Envelope::decode(message).map_err(EnvelopeError::ProtoError)?;
|
||||
|
||||
Ok((
|
||||
out.content,
|
||||
out.metadata.ok_or(EnvelopeError::MetadataError())?,
|
||||
))
|
||||
}
|
Reference in New Issue
Block a user