feat: with agent db

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-27 19:42:33 +02:00
parent 75d99c2461
commit 43ed89d0d8
10 changed files with 286 additions and 68 deletions

View File

@@ -1,9 +1,9 @@
use capnp::message::{Builder, HeapAllocator};
use capnp::message::{ReaderOptions, TypedReader};
use capnp::serialize::{self, OwnedSegments, SliceSegments};
use capnp::serialize::{self, SliceSegments};
use capnp::traits::{FromPointerReader, Owned};
use churn_domain::LogEvent;
use capnp::traits::{Owned};
use churn_domain::{Agent, LogEvent};
mod models_capnp;
@@ -14,11 +14,11 @@ pub trait CapnpPackExt {
fn deserialize_capnp(content: &Vec<u8>) -> anyhow::Result<Self::Return>;
fn capnp_to_string(builder: &Builder<HeapAllocator>) -> Vec<u8> {
let msg = serialize::write_message_to_words(builder);
msg
serialize::write_message_to_words(builder)
}
fn string_to_capnp<'a, S>(content: &'a Vec<u8>) -> TypedReader<SliceSegments, S>
fn string_to_capnp<S>(content: &Vec<u8>) -> TypedReader<SliceSegments, S>
where
S: Owned,
{
@@ -26,9 +26,9 @@ pub trait CapnpPackExt {
serialize::read_message_from_flat_slice(&mut content.as_slice(), ReaderOptions::new())
.unwrap();
let log_event = log_event.into_typed::<S>();
log_event
log_event.into_typed::<S>()
}
}
@@ -37,7 +37,6 @@ impl CapnpPackExt for LogEvent {
fn serialize_capnp(&self) -> Vec<u8> {
let mut builder = Builder::new_default();
let mut log_event = builder.init_root::<models_capnp::log_event::Builder>();
log_event.set_id(&self.id.to_string());
log_event.set_author(&self.author);
@@ -62,3 +61,25 @@ impl CapnpPackExt for LogEvent {
})
}
}
impl CapnpPackExt for Agent {
type Return = Self;
fn serialize_capnp(&self) -> Vec<u8> {
let mut builder = Builder::new_default();
let mut item = builder.init_root::<models_capnp::agent::Builder>();
item.set_name(&self.name);
Self::capnp_to_string(&builder)
}
fn deserialize_capnp(content: &Vec<u8>) -> anyhow::Result<Self::Return> {
let item = Self::string_to_capnp::<models_capnp::agent::Owned>(content);
let item = item.get()?;
Ok(Self {
name: item.get_name()?.into(),
})
}
}