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",
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crunch-codegen"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"async-trait",
|
||||||
|
"crunch-traits",
|
||||||
|
"tokio",
|
||||||
|
"tracing",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crunch-envelope"
|
name = "crunch-envelope"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
@ -23,5 +23,9 @@ nats = "0.24.0"
|
|||||||
clap = {version = "4.4.4", features = ["derive"]}
|
clap = {version = "4.4.4", features = ["derive"]}
|
||||||
toml_edit = {version = "0.20.0",features = ["serde"]}
|
toml_edit = {version = "0.20.0",features = ["serde"]}
|
||||||
serde = {version = "1.0.88", features = ["derive"]}
|
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"
|
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
|
harness = false
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["json", "proto"]
|
default = ["proto", "capnp"]
|
||||||
json = ["dep:serde", "dep:serde_json", "dep:base64"]
|
json = ["dep:serde", "dep:serde_json", "dep:base64"]
|
||||||
proto = ["dep:prost", "dep:prost-types", "dep:bytes"]
|
proto = ["dep:prost", "dep:prost-types", "dep:bytes"]
|
||||||
|
capnp = ["dep:capnp"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
capnp = "0.17.2"
|
capnp = { version = "0.17.2",optional = true}
|
||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
|
|
||||||
# Json
|
# Json
|
||||||
@ -24,9 +25,9 @@ serde_json = {version = "1.0.107",optional = true}
|
|||||||
base64 = {version = "0.21.4",optional = true}
|
base64 = {version = "0.21.4",optional = true}
|
||||||
|
|
||||||
# Proto
|
# Proto
|
||||||
prost = {version = "0.12", optional = true}
|
prost = {workspace = true, optional = true}
|
||||||
prost-types = {version = "0.12", optional = true}
|
prost-types = {workspace = true, optional = true}
|
||||||
bytes = {version = "0.4", optional = true}
|
bytes = {workspace = true, optional = true}
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
capnpc = "0.17.2"
|
capnpc = "0.17.2"
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
#[cfg(feature = "capnp")]
|
||||||
mod envelope_capnp;
|
mod envelope_capnp;
|
||||||
|
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
@ -18,28 +19,14 @@ pub mod proto {
|
|||||||
pub use crate::proto_envelope::*;
|
pub use crate::proto_envelope::*;
|
||||||
}
|
}
|
||||||
|
|
||||||
use capnp::message::{Builder, ReaderOptions};
|
#[cfg(feature = "capnp")]
|
||||||
use capnp::serialize;
|
pub mod capnp {
|
||||||
use thiserror::Error;
|
use capnp::message::{Builder, ReaderOptions};
|
||||||
|
use capnp::serialize;
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
use crate::{envelope_capnp, EnvelopeError, Metadata};
|
||||||
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(),
|
|
||||||
}
|
|
||||||
|
|
||||||
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 builder = Builder::new_default();
|
||||||
let mut envelope = builder.init_root::<envelope_capnp::envelope::Builder>();
|
let mut envelope = builder.init_root::<envelope_capnp::envelope::Builder>();
|
||||||
envelope.set_content(content);
|
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);
|
metadata.set_entity(entity);
|
||||||
|
|
||||||
serialize::write_message_to_words(&builder)
|
serialize::write_message_to_words(&builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
pub fn unwrap(message: &[u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Metadata {
|
|
||||||
domain: String,
|
|
||||||
entity: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn unwrap(message: &[u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
|
|
||||||
let mut message = message;
|
let mut message = message;
|
||||||
let message_builder =
|
let message_builder =
|
||||||
serialize::read_message_from_flat_slice(&mut message, ReaderOptions::new())
|
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(),
|
.to_string(),
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::{unwrap, wrap};
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_can_serialize() {
|
fn test_can_serialize() {
|
||||||
@ -102,4 +82,32 @@ mod test {
|
|||||||
assert_eq!(entity, original.1.entity);
|
assert_eq!(entity, original.1.entity);
|
||||||
assert_eq!(message, original.0.as_slice());
|
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