feat: with complete example and generated code

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-09-24 21:57:24 +02:00
parent c32aab5630
commit dd024ec8fb
13 changed files with 106 additions and 114 deletions

View File

@@ -5,4 +5,4 @@ codegen = ["rust"]
[[publish]]
schema-path = "schemas/crunch"
output-path = "src/crunch"
output-path = "src/gencrunch"

View File

@@ -1,58 +0,0 @@
pub mod basic {
pub mod my_event {
include!("basic.my_event.rs");
impl ::crunch::Serializer for MyEvent {
fn serialize(&self) -> Result<Vec<u8>, ::crunch::errors::SerializeError> {
todo!()
}
}
impl ::crunch::Deserializer for MyEvent {
fn deserialize(_raw: Vec<u8>) -> Result<Self, ::crunch::errors::DeserializeError>
where
Self: Sized,
{
todo!()
}
}
impl Event for MyEvent {
fn event_info() -> ::crunch::traits::EventInfo {
EventInfo {
domain: "my-domain",
entity_type: "my-entity-type",
event_name: "my-event-name",
}
}
}
}
pub mod includes {
pub mod my_include {
include!("basic.includes.my_include.rs");
impl ::crunch::Serializer for MyInclude {
fn serialize(&self) -> Result<Vec<u8>, ::crunch::errors::SerializeError> {
todo!()
}
}
impl ::crunch::Deserializer for MyInclude {
fn deserialize(_raw: Vec<u8>) -> Result<Self, ::crunch::errors::DeserializeError>
where
Self: Sized,
{
todo!()
}
}
impl Event for MyInclude {
fn event_info() -> ::crunch::traits::EventInfo {
EventInfo {
domain: "my-domain",
entity_type: "my-entity-type",
event_name: "my-event-name",
}
}
}
}
}
}

View File

@@ -0,0 +1,60 @@
pub mod basic {
pub mod includes {
pub mod my_include { use prost::Message;
include!("basic.includes.my_include.rs");
impl ::crunch::traits::Serializer for MyInclude {
fn serialize(&self) -> Result<Vec<u8>, ::crunch::errors::SerializeError> {
Ok(self.encode_to_vec())
}
}
impl ::crunch::traits::Deserializer for MyInclude {
fn deserialize(raw: Vec<u8>) -> Result<Self, ::crunch::errors::DeserializeError>
where
Self: Sized,
{
let output = Self::decode(raw.as_slice()).map_err(|e| ::crunch::errors::DeserializeError::ProtoErr(e))?;
Ok(output)
}
}
impl crunch::traits::Event for MyInclude {
fn event_info() -> ::crunch::traits::EventInfo {
::crunch::traits::EventInfo {
domain: "my-domain",
entity_type: "my-entity-type",
event_name: "my-event-name",
}
}
}
}
}
pub mod my_event { use prost::Message;
include!("basic.my_event.rs");
impl ::crunch::traits::Serializer for MyEvent {
fn serialize(&self) -> Result<Vec<u8>, ::crunch::errors::SerializeError> {
Ok(self.encode_to_vec())
}
}
impl ::crunch::traits::Deserializer for MyEvent {
fn deserialize(raw: Vec<u8>) -> Result<Self, ::crunch::errors::DeserializeError>
where
Self: Sized,
{
let output = Self::decode(raw.as_slice()).map_err(|e| ::crunch::errors::DeserializeError::ProtoErr(e))?;
Ok(output)
}
}
impl crunch::traits::Event for MyEvent {
fn event_info() -> ::crunch::traits::EventInfo {
::crunch::traits::EventInfo {
domain: "my-domain",
entity_type: "my-entity-type",
event_name: "my-event-name",
}
}
}
}
}

View File

@@ -1,49 +1,30 @@
mod crunch;
mod gencrunch;
use ::crunch::traits::{Deserializer, Event, EventInfo, Serializer};
struct MyEvent {}
impl Serializer for MyEvent {
fn serialize(&self) -> Result<Vec<u8>, ::crunch::errors::SerializeError> {
todo!()
}
}
impl Deserializer for MyEvent {
fn deserialize(_raw: Vec<u8>) -> Result<Self, ::crunch::errors::DeserializeError>
where
Self: Sized,
{
todo!()
}
}
impl Event for MyEvent {
fn event_info() -> ::crunch::traits::EventInfo {
EventInfo {
domain: "my-domain",
entity_type: "my-entity-type",
event_name: "my-event-name",
}
}
}
use gencrunch::basic::{includes::my_include::MyInclude, my_event::MyEvent};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
crunch::basic::my_event::MyEvent {
name: "some-name".into(),
include: Some(crunch::basic::includes::my_include::MyInclude {
name: "some-name".into(),
}),
};
let crunch = ::crunch::builder::Builder::default().build()?;
crunch
.subscribe(|_item: MyEvent| async move { Ok(()) })
.subscribe(|item: MyEvent| async move {
println!("received item: {:?}", item);
Ok(())
})
.await?;
crunch.publish(MyEvent {}).await?;
crunch
.publish(MyEvent {
name: "some-name".into(),
include: Some(MyInclude {
name: "some-name".into(),
}),
})
.await?;
// Sleep a while to let subscriber catch item
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
Ok(())
}