@@ -1,6 +1,6 @@
|
||||
use capnp::message::{Builder, HeapAllocator};
|
||||
use capnp::message::{ReaderOptions, TypedReader};
|
||||
use capnp::serialize::{self, OwnedSegments};
|
||||
use capnp::serialize::{self, OwnedSegments, SliceSegments};
|
||||
|
||||
use capnp::traits::{FromPointerReader, Owned};
|
||||
use churn_domain::LogEvent;
|
||||
@@ -10,21 +10,21 @@ mod models_capnp;
|
||||
pub trait CapnpPackExt {
|
||||
type Return;
|
||||
|
||||
fn serialize_capnp(&self) -> String;
|
||||
fn deserialize_capnp(content: &str) -> anyhow::Result<Self::Return>;
|
||||
fn serialize_capnp(&self) -> Vec<u8>;
|
||||
fn deserialize_capnp(content: &Vec<u8>) -> anyhow::Result<Self::Return>;
|
||||
|
||||
fn capnp_to_string(builder: &Builder<HeapAllocator>) -> String {
|
||||
fn capnp_to_string(builder: &Builder<HeapAllocator>) -> Vec<u8> {
|
||||
let msg = serialize::write_message_to_words(builder);
|
||||
std::str::from_utf8(msg.as_slice())
|
||||
.expect("to be able to parse capnp as utf8")
|
||||
.to_string()
|
||||
msg
|
||||
}
|
||||
|
||||
fn string_to_capnp<'a, S>(content: &'a str) -> TypedReader<OwnedSegments, S>
|
||||
fn string_to_capnp<'a, S>(content: &'a Vec<u8>) -> TypedReader<SliceSegments, S>
|
||||
where
|
||||
S: Owned,
|
||||
{
|
||||
let log_event = serialize::read_message(content.as_bytes(), ReaderOptions::new()).unwrap();
|
||||
let log_event =
|
||||
serialize::read_message_from_flat_slice(&mut content.as_slice(), ReaderOptions::new())
|
||||
.unwrap();
|
||||
|
||||
let log_event = log_event.into_typed::<S>();
|
||||
|
||||
@@ -35,18 +35,19 @@ pub trait CapnpPackExt {
|
||||
impl CapnpPackExt for LogEvent {
|
||||
type Return = Self;
|
||||
|
||||
fn serialize_capnp(&self) -> String {
|
||||
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: &str) -> anyhow::Result<Self> {
|
||||
fn deserialize_capnp(content: &Vec<u8>) -> anyhow::Result<Self> {
|
||||
let log_event = Self::string_to_capnp::<models_capnp::log_event::Owned>(content);
|
||||
let log_event = log_event.get()?;
|
||||
|
||||
@@ -54,6 +55,10 @@ impl CapnpPackExt for LogEvent {
|
||||
id: uuid::Uuid::parse_str(log_event.get_id()?)?,
|
||||
author: log_event.get_author()?.into(),
|
||||
content: log_event.get_content()?.into(),
|
||||
timestamp: chrono::DateTime::<chrono::Utc>::from_utc(
|
||||
chrono::NaiveDateTime::from_timestamp_opt(log_event.get_datetime(), 0).unwrap(),
|
||||
chrono::Utc,
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user