feat: with complete example and generated code
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
@@ -64,28 +64,28 @@ impl Node {
|
||||
let padding = " ".repeat(indent * 4);
|
||||
|
||||
let mut message_tokens = Vec::new();
|
||||
|
||||
if let Some(file) = &self.file {
|
||||
if let Some(messages) = &self.messages {
|
||||
for message in messages.iter() {
|
||||
let tokens: genco::lang::rust::Tokens = quote! {
|
||||
$['\r']$(&padding)impl ::crunch::Serializer for $(message) {
|
||||
$['\r']$(&padding)impl ::crunch::traits::Serializer for $(message) {
|
||||
$['\r']$(&padding) fn serialize(&self) -> Result<Vec<u8>, ::crunch::errors::SerializeError> {
|
||||
$['\r']$(&padding) todo!()
|
||||
$['\r']$(&padding) Ok(self.encode_to_vec())
|
||||
$['\r']$(&padding) }
|
||||
$['\r']$(&padding)}
|
||||
$['\r']$(&padding)impl ::crunch::Deserializer for $(message) {
|
||||
$['\r']$(&padding) fn deserialize(_raw: Vec<u8>) -> Result<Self, ::crunch::errors::DeserializeError>
|
||||
$['\r']$(&padding)impl ::crunch::traits::Deserializer for $(message) {
|
||||
$['\r']$(&padding) fn deserialize(raw: Vec<u8>) -> Result<Self, ::crunch::errors::DeserializeError>
|
||||
$['\r']$(&padding) where
|
||||
$['\r']$(&padding) Self: Sized,
|
||||
$['\r']$(&padding) {
|
||||
$['\r']$(&padding) todo!()
|
||||
$['\r']$(&padding) let output = Self::decode(raw.as_slice()).map_err(|e| ::crunch::errors::DeserializeError::ProtoErr(e))?;
|
||||
$['\r']$(&padding) Ok(output)
|
||||
$['\r']$(&padding) }
|
||||
$['\r']$(&padding)}
|
||||
$['\r']$(&padding)
|
||||
$['\r']$(&padding)impl Event for $(message) {
|
||||
$['\r']$(&padding)impl crunch::traits::Event for $(message) {
|
||||
$['\r']$(&padding) fn event_info() -> ::crunch::traits::EventInfo {
|
||||
$['\r']$(&padding) EventInfo {
|
||||
$['\r']$(&padding) ::crunch::traits::EventInfo {
|
||||
$['\r']$(&padding) domain: "my-domain",
|
||||
$['\r']$(&padding) entity_type: "my-entity-type",
|
||||
$['\r']$(&padding) event_name: "my-event-name",
|
||||
@@ -100,6 +100,7 @@ impl Node {
|
||||
|
||||
quote! {
|
||||
$['\r']$(&padding)pub mod $(&self.segment) {
|
||||
use prost::Message;
|
||||
$['\r']$(&padding)include!($(quoted(file)));
|
||||
$['\r']$(&padding)$(for tokens in message_tokens join ($['\r']) => $tokens)
|
||||
$['\r']$(&padding)}
|
||||
|
@@ -12,3 +12,4 @@ thiserror.workspace = true
|
||||
async-trait.workspace = true
|
||||
uuid.workspace = true
|
||||
futures.workspace = true
|
||||
prost.workspace = true
|
||||
|
@@ -8,8 +8,10 @@ pub enum SerializeError {
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum DeserializeError {
|
||||
#[error("failed to serialize {0}")]
|
||||
#[error("failed to deserialize {0}")]
|
||||
FailedToDeserialize(anyhow::Error),
|
||||
#[error("failed to deserialize {0}")]
|
||||
ProtoErr(prost::DecodeError),
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
|
@@ -1,4 +1,4 @@
|
||||
use std::{fmt::Display};
|
||||
use std::fmt::Display;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use errors::{DeserializeError, PersistenceError, SerializeError};
|
||||
|
@@ -56,13 +56,17 @@ impl crunch_traits::Persistence for InMemoryPersistence {
|
||||
}
|
||||
|
||||
async fn get(&self, event_id: &str) -> Result<Option<(EventInfo, Vec<u8>)>, PersistenceError> {
|
||||
Ok(self
|
||||
.store
|
||||
.read()
|
||||
.await
|
||||
.get(event_id)
|
||||
.filter(|m| m.state == MsgState::Pending).cloned()
|
||||
.map(|m| (m.info, m.msg)))
|
||||
let store = self.store.read().await;
|
||||
|
||||
let event = match store.get(event_id).filter(|m| m.state == MsgState::Pending) {
|
||||
Some(event) => event,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let (content, _) = crunch_envelope::proto::unwrap(event.msg.as_slice())
|
||||
.map_err(|e| PersistenceError::GetErr(anyhow::anyhow!(e)))?;
|
||||
|
||||
Ok(Some((event.info, content)))
|
||||
}
|
||||
|
||||
async fn update_published(&self, event_id: &str) -> Result<(), PersistenceError> {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
use crunch_traits::{Event};
|
||||
use crunch_traits::Event;
|
||||
use futures::StreamExt;
|
||||
|
||||
use crate::{errors, Transport};
|
||||
|
Reference in New Issue
Block a user