churn/crates/churn-capnp/src/lib.rs
kjuulh 7e22a7f3ab
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing
feat: update
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-04-06 21:30:49 +02:00

106 lines
3.2 KiB
Rust

use capnp::message::{Builder, HeapAllocator};
use capnp::message::{ReaderOptions, TypedReader};
use capnp::serialize::{self, BufferSegments};
use capnp::traits::Owned;
use churn_domain::{Agent, Lease, LogEvent};
mod models_capnp;
pub trait CapnpPackExt {
type Return;
fn serialize_capnp(&self) -> Vec<u8>;
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self::Return>;
fn capnp_to_string(builder: &Builder<HeapAllocator>) -> Vec<u8> {
serialize::write_message_to_words(builder)
}
fn string_to_capnp<S>(mut content: &[u8]) -> TypedReader<BufferSegments<&[u8]>, S>
where
S: Owned,
{
let log_event =
serialize::read_message_from_flat_slice(&mut content, ReaderOptions::new()).unwrap();
log_event.into_typed::<S>()
}
}
impl CapnpPackExt for LogEvent {
type Return = Self;
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);
log_event.set_content(&self.content);
log_event.set_datetime(self.timestamp.timestamp());
Self::capnp_to_string(&builder)
}
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self> {
let log_event = Self::string_to_capnp::<models_capnp::log_event::Owned>(content);
let log_event = log_event.get()?;
Ok(Self {
id: uuid::Uuid::parse_str(log_event.get_id()?.to_str()?)?,
author: log_event.get_author()?.to_string()?,
content: log_event.get_content()?.to_string()?,
timestamp: chrono::DateTime::<chrono::Utc>::from_utc(
chrono::NaiveDateTime::from_timestamp_opt(log_event.get_datetime(), 0).unwrap(),
chrono::Utc,
),
})
}
}
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: &[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()?.to_string()?,
})
}
}
impl CapnpPackExt for Lease {
type Return = Self;
fn serialize_capnp(&self) -> Vec<u8> {
let mut builder = Builder::new_default();
let mut item = builder.init_root::<models_capnp::lease::Builder>();
item.set_id(&self.id.to_string());
item.set_lease(&self.lease.to_string());
Self::capnp_to_string(&builder)
}
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self::Return> {
let item = Self::string_to_capnp::<models_capnp::lease::Owned>(content);
let item = item.get()?;
Ok(Self {
id: uuid::Uuid::parse_str(item.get_id()?.to_str()?)?,
lease: uuid::Uuid::parse_str(item.get_lease()?.to_str()?)?,
})
}
}