2023-08-27 16:49:53 +02:00
|
|
|
use capnp::message::{Builder, HeapAllocator};
|
|
|
|
use capnp::message::{ReaderOptions, TypedReader};
|
2023-08-27 19:42:33 +02:00
|
|
|
use capnp::serialize::{self, SliceSegments};
|
2023-08-27 16:49:53 +02:00
|
|
|
|
2023-08-27 19:57:22 +02:00
|
|
|
use capnp::traits::Owned;
|
2023-08-27 20:16:27 +02:00
|
|
|
use churn_domain::{Agent, Lease, LogEvent};
|
2023-08-27 16:49:53 +02:00
|
|
|
|
|
|
|
mod models_capnp;
|
|
|
|
|
|
|
|
pub trait CapnpPackExt {
|
|
|
|
type Return;
|
|
|
|
|
2023-08-27 18:15:25 +02:00
|
|
|
fn serialize_capnp(&self) -> Vec<u8>;
|
2023-08-27 20:16:27 +02:00
|
|
|
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self::Return>;
|
2023-08-27 16:49:53 +02:00
|
|
|
|
2023-08-27 18:15:25 +02:00
|
|
|
fn capnp_to_string(builder: &Builder<HeapAllocator>) -> Vec<u8> {
|
2023-08-27 19:42:33 +02:00
|
|
|
serialize::write_message_to_words(builder)
|
2023-08-27 16:49:53 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 20:16:27 +02:00
|
|
|
fn string_to_capnp<S>(mut content: &[u8]) -> TypedReader<SliceSegments, S>
|
2023-08-27 16:49:53 +02:00
|
|
|
where
|
|
|
|
S: Owned,
|
|
|
|
{
|
2023-08-27 18:15:25 +02:00
|
|
|
let log_event =
|
2023-08-27 20:16:27 +02:00
|
|
|
serialize::read_message_from_flat_slice(&mut content, ReaderOptions::new()).unwrap();
|
2023-08-27 16:49:53 +02:00
|
|
|
|
2023-08-27 19:42:33 +02:00
|
|
|
log_event.into_typed::<S>()
|
2023-08-27 16:49:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CapnpPackExt for LogEvent {
|
|
|
|
type Return = Self;
|
|
|
|
|
2023-08-27 18:15:25 +02:00
|
|
|
fn serialize_capnp(&self) -> Vec<u8> {
|
2023-08-27 16:49:53 +02:00
|
|
|
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);
|
2023-08-27 18:15:25 +02:00
|
|
|
log_event.set_datetime(self.timestamp.timestamp());
|
2023-08-27 16:49:53 +02:00
|
|
|
|
|
|
|
Self::capnp_to_string(&builder)
|
|
|
|
}
|
|
|
|
|
2023-08-27 20:16:27 +02:00
|
|
|
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self> {
|
2023-08-27 16:49:53 +02:00
|
|
|
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()?)?,
|
|
|
|
author: log_event.get_author()?.into(),
|
|
|
|
content: log_event.get_content()?.into(),
|
2023-08-27 18:15:25 +02:00
|
|
|
timestamp: chrono::DateTime::<chrono::Utc>::from_utc(
|
|
|
|
chrono::NaiveDateTime::from_timestamp_opt(log_event.get_datetime(), 0).unwrap(),
|
|
|
|
chrono::Utc,
|
|
|
|
),
|
2023-08-27 16:49:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-08-27 19:42:33 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-08-27 20:16:27 +02:00
|
|
|
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self::Return> {
|
2023-08-27 19:42:33 +02:00
|
|
|
let item = Self::string_to_capnp::<models_capnp::agent::Owned>(content);
|
|
|
|
let item = item.get()?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
name: item.get_name()?.into(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-08-27 20:16:27 +02:00
|
|
|
|
|
|
|
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()?)?,
|
|
|
|
lease: uuid::Uuid::parse_str(item.get_lease()?)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|