feat: add codegen lib
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
09dfa6c2e3
commit
207a6f5c44
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -545,6 +545,17 @@ dependencies = [
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crunch-codegen"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"crunch-traits",
|
||||
"tokio",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crunch-envelope"
|
||||
version = "0.1.0"
|
||||
|
@ -23,5 +23,9 @@ nats = "0.24.0"
|
||||
clap = {version = "4.4.4", features = ["derive"]}
|
||||
toml_edit = {version = "0.20.0",features = ["serde"]}
|
||||
serde = {version = "1.0.88", features = ["derive"]}
|
||||
prost = {version = "0.12"}
|
||||
prost-types = {version = "0.12"}
|
||||
bytes = {version = "0.4"}
|
||||
|
||||
|
||||
pretty_assertions = "1.4.0"
|
14
crates/crunch-codegen/Cargo.toml
Normal file
14
crates/crunch-codegen/Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "crunch-codegen"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
crunch-traits.workspace = true
|
||||
|
||||
anyhow.workspace = true
|
||||
tracing.workspace = true
|
||||
tokio.workspace = true
|
||||
async-trait.workspace = true
|
14
crates/crunch-codegen/src/lib.rs
Normal file
14
crates/crunch-codegen/src/lib.rs
Normal file
@ -0,0 +1,14 @@
|
||||
pub fn add(left: usize, right: usize) -> usize {
|
||||
left + right
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
@ -10,12 +10,13 @@ name = "envelope_benchmark"
|
||||
harness = false
|
||||
|
||||
[features]
|
||||
default = ["json", "proto"]
|
||||
default = ["proto", "capnp"]
|
||||
json = ["dep:serde", "dep:serde_json", "dep:base64"]
|
||||
proto = ["dep:prost", "dep:prost-types", "dep:bytes"]
|
||||
capnp = ["dep:capnp"]
|
||||
|
||||
[dependencies]
|
||||
capnp = "0.17.2"
|
||||
capnp = { version = "0.17.2",optional = true}
|
||||
thiserror.workspace = true
|
||||
|
||||
# Json
|
||||
@ -24,9 +25,9 @@ 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}
|
||||
prost = {workspace = true, optional = true}
|
||||
prost-types = {workspace = true, optional = true}
|
||||
bytes = {workspace = true, optional = true}
|
||||
|
||||
[build-dependencies]
|
||||
capnpc = "0.17.2"
|
||||
|
@ -1,4 +1,5 @@
|
||||
#[allow(dead_code)]
|
||||
#[cfg(feature = "capnp")]
|
||||
mod envelope_capnp;
|
||||
|
||||
#[cfg(feature = "json")]
|
||||
@ -18,28 +19,14 @@ pub mod proto {
|
||||
pub use crate::proto_envelope::*;
|
||||
}
|
||||
|
||||
use capnp::message::{Builder, ReaderOptions};
|
||||
use capnp::serialize;
|
||||
use thiserror::Error;
|
||||
#[cfg(feature = "capnp")]
|
||||
pub mod capnp {
|
||||
use capnp::message::{Builder, ReaderOptions};
|
||||
use capnp::serialize;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum EnvelopeError {
|
||||
#[error("capnp failed to serialize or deserialize code")]
|
||||
CapnpError(#[source] capnp::Error),
|
||||
#[cfg(feature = "json")]
|
||||
#[error("serde_json failed to serialize or deserialize code")]
|
||||
JsonError(#[source] serde_json::Error),
|
||||
#[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(),
|
||||
}
|
||||
use crate::{envelope_capnp, EnvelopeError, Metadata};
|
||||
|
||||
pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8> {
|
||||
pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8> {
|
||||
let mut builder = Builder::new_default();
|
||||
let mut envelope = builder.init_root::<envelope_capnp::envelope::Builder>();
|
||||
envelope.set_content(content);
|
||||
@ -49,16 +36,9 @@ pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8>
|
||||
metadata.set_entity(entity);
|
||||
|
||||
serialize::write_message_to_words(&builder)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Metadata {
|
||||
domain: String,
|
||||
entity: String,
|
||||
}
|
||||
|
||||
pub fn unwrap(message: &[u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
|
||||
pub fn unwrap(message: &[u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
|
||||
let mut message = message;
|
||||
let message_builder =
|
||||
serialize::read_message_from_flat_slice(&mut message, ReaderOptions::new())
|
||||
@ -83,11 +63,11 @@ pub fn unwrap(message: &[u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
|
||||
.to_string(),
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{unwrap, wrap};
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_can_serialize() {
|
||||
@ -102,4 +82,32 @@ mod test {
|
||||
assert_eq!(entity, original.1.entity);
|
||||
assert_eq!(message, original.0.as_slice());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum EnvelopeError {
|
||||
#[error("capnp failed to serialize or deserialize code")]
|
||||
#[cfg(feature = "capnp")]
|
||||
CapnpError(#[source] ::capnp::Error),
|
||||
#[cfg(feature = "json")]
|
||||
#[error("serde_json failed to serialize or deserialize code")]
|
||||
JsonError(#[source] serde_json::Error),
|
||||
#[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(),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Metadata {
|
||||
domain: String,
|
||||
entity: String,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user