chore: change to byte slice

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-27 20:16:27 +02:00
parent d3beab5006
commit 541b9b22d2
10 changed files with 829 additions and 746 deletions

View File

@@ -10,3 +10,8 @@ struct LogEvent {
struct Agent {
name @0 :Text;
}
struct Lease {
id @0 :Text;
lease @1 :Text;
}

View File

@@ -3,7 +3,7 @@ use capnp::message::{ReaderOptions, TypedReader};
use capnp::serialize::{self, SliceSegments};
use capnp::traits::Owned;
use churn_domain::{Agent, LogEvent};
use churn_domain::{Agent, Lease, LogEvent};
mod models_capnp;
@@ -11,19 +11,18 @@ pub trait CapnpPackExt {
type Return;
fn serialize_capnp(&self) -> Vec<u8>;
fn deserialize_capnp(content: &Vec<u8>) -> anyhow::Result<Self::Return>;
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>(content: &Vec<u8>) -> TypedReader<SliceSegments, S>
fn string_to_capnp<S>(mut content: &[u8]) -> TypedReader<SliceSegments, S>
where
S: Owned,
{
let log_event =
serialize::read_message_from_flat_slice(&mut content.as_slice(), ReaderOptions::new())
.unwrap();
serialize::read_message_from_flat_slice(&mut content, ReaderOptions::new()).unwrap();
log_event.into_typed::<S>()
}
@@ -43,7 +42,7 @@ impl CapnpPackExt for LogEvent {
Self::capnp_to_string(&builder)
}
fn deserialize_capnp(content: &Vec<u8>) -> anyhow::Result<Self> {
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()?;
@@ -71,7 +70,7 @@ impl CapnpPackExt for Agent {
Self::capnp_to_string(&builder)
}
fn deserialize_capnp(content: &Vec<u8>) -> anyhow::Result<Self::Return> {
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self::Return> {
let item = Self::string_to_capnp::<models_capnp::agent::Owned>(content);
let item = item.get()?;
@@ -80,3 +79,27 @@ impl CapnpPackExt for Agent {
})
}
}
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()?)?,
})
}
}

File diff suppressed because it is too large Load Diff