feat: add subscriptions

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-09-23 18:21:39 +02:00
parent c662d65799
commit 98550ace16
21 changed files with 431 additions and 130 deletions

View File

@@ -8,5 +8,11 @@ fn main() {
.run()
.unwrap();
prost_build::compile_protos(&["src/envelope.proto"], &["src/"]).unwrap();
std::fs::create_dir_all("src/generated").unwrap();
let mut config = prost_build::Config::default();
config.out_dir("src/generated/");
config
.compile_protos(&["src/envelope.proto"], &["src/"])
.unwrap();
}

View File

@@ -0,0 +1,18 @@
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Envelope {
#[prost(message, optional, tag="1")]
pub metadata: ::std::option::Option<Metadata>,
#[prost(bytes, tag="2")]
pub content: std::vec::Vec<u8>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Metadata {
#[prost(string, tag="1")]
pub domain: std::string::String,
#[prost(string, tag="2")]
pub entity: std::string::String,
#[prost(uint64, tag="3")]
pub timestamp: u64,
#[prost(uint64, tag="4")]
pub sequence: u64,
}

View File

@@ -0,0 +1,3 @@
pub mod crunch {
include!("crunch.envelope.rs");
}

View File

@@ -3,6 +3,7 @@ mod envelope_capnp;
#[cfg(feature = "json")]
mod json_envelope;
mod generated;
#[cfg(feature = "proto")]
mod proto_envelope;

View File

@@ -1,14 +1,11 @@
pub mod envelope {
include!(concat!(env!("OUT_DIR"), "/crunch.envelope.rs"));
}
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::Envelope {
metadata: Some(envelope::Metadata {
let out = Envelope {
metadata: Some(Metadata {
domain: domain.to_string(),
entity: entity.to_string(),
timestamp: 0,
@@ -20,8 +17,8 @@ pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8>
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)?;
pub fn unwrap<'a>(message: &'a [u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
let out = Envelope::decode(message).map_err(EnvelopeError::ProtoError)?;
Ok((
out.content,