@@ -13,18 +13,27 @@ harness = false
|
||||
debug = true
|
||||
|
||||
[features]
|
||||
default = ["json"]
|
||||
default = ["json", "proto"]
|
||||
json = ["dep:serde", "dep:serde_json", "dep:base64"]
|
||||
proto = ["dep:prost", "dep:prost-types", "dep:bytes"]
|
||||
|
||||
[dependencies]
|
||||
capnp = "0.17.2"
|
||||
thiserror.workspace = true
|
||||
|
||||
# Json
|
||||
serde = { version = "1.0.188" ,optional = true, features = ["derive"] }
|
||||
serde_json = {version = "1.0.107",optional = true}
|
||||
base64 = {version = "0.21.4",optional = true}
|
||||
|
||||
# Proto
|
||||
prost = {version = "0.12", optional = true}
|
||||
prost-types = {version = "0.12", optional = true}
|
||||
bytes = {version = "0.4", optional = true}
|
||||
|
||||
[build-dependencies]
|
||||
capnpc = "0.17.2"
|
||||
prost-build = "0.5"
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = { version = "0.4" }
|
@@ -13,6 +13,12 @@ fn envelope_json_benchmark(content: &[u8]) -> () {
|
||||
let _ = crunch_envelope::json::unwrap(&out).expect("to be able to unwrap capnp message");
|
||||
}
|
||||
|
||||
fn envelope_proto_benchmark(content: &[u8]) -> () {
|
||||
let out = crunch_envelope::proto::wrap("some-domain", "some-entity", content);
|
||||
|
||||
let _ = crunch_envelope::proto::unwrap(&out).expect("to be able to unwrap capnp message");
|
||||
}
|
||||
|
||||
fn criterion_benchmark(c: &mut Criterion) {
|
||||
let large_content: [u8; 10000] = [0; 10000];
|
||||
|
||||
@@ -22,6 +28,9 @@ fn criterion_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("envelope::json", |b| {
|
||||
b.iter(|| envelope_json_benchmark(&large_content))
|
||||
});
|
||||
c.bench_function("envelope::proto", |b| {
|
||||
b.iter(|| envelope_proto_benchmark(&large_content))
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(benches, criterion_benchmark);
|
||||
|
@@ -7,4 +7,6 @@ fn main() {
|
||||
.file("schemas/envelope.capnp")
|
||||
.run()
|
||||
.unwrap();
|
||||
|
||||
prost_build::compile_protos(&["src/envelope.proto"], &["src/"]).unwrap();
|
||||
}
|
||||
|
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