From ec322b5f50394b4febf654ce60771dded66e02c5 Mon Sep 17 00:00:00 2001 From: Dominik Werder Date: Tue, 26 Nov 2024 16:28:30 +0100 Subject: [PATCH] WIP --- Cargo.toml | 1 + src/apitypes.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ src/collect_s.rs | 25 +++++++++--------------- src/lib.rs | 1 + src/merge.rs | 2 ++ src/timebin.rs | 24 ++++++++++++++++++++++-- 6 files changed, 84 insertions(+), 18 deletions(-) create mode 100644 src/apitypes.rs diff --git a/Cargo.toml b/Cargo.toml index a62deab..3810506 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ serde = { version = "1", features = ["derive"] } erased-serde = "0.4" typetag = "0.2" serde_json = "1" +ciborium = "0.2.1" bincode = "1.3.3" bytes = "1.8.0" futures-util = "0.3.24" diff --git a/src/apitypes.rs b/src/apitypes.rs new file mode 100644 index 0000000..9a30bbc --- /dev/null +++ b/src/apitypes.rs @@ -0,0 +1,49 @@ +use crate::collect_s::ToCborValue; +use core::fmt; +use serde::Serialize; +use std::collections::VecDeque; + +pub trait ContPayload: fmt::Debug + Serialize + Send {} + +impl ContPayload for T where T: fmt::Debug + Serialize + Send {} + +pub trait UserApiType: ToCborValue {} + +pub trait ToUserFacingApiType { + fn to_user_facing_api_type(self) -> Box; +} + +#[derive(Serialize)] +pub struct ContainerEventsApi +where + EVT: ContPayload, +{ + pub tss: VecDeque, + pub values: VecDeque, +} + +impl fmt::Debug for ContainerEventsApi +where + EVT: ContPayload, +{ + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("ContainerEventsApi") + // .field("tss", &self.tss) + // .field("values", &self.values) + .finish() + } +} + +impl ToCborValue for ContainerEventsApi +where + EVT: ContPayload, +{ + fn to_cbor_value(&self) -> Result { + // let mut out = Vec::new(); + // ciborium::ser::into_writer(self, &mut out).unwrap(); + let val = ciborium::value::Value::serialized(self).unwrap(); + Ok(val) + } +} + +impl UserApiType for ContainerEventsApi where EVT: ContPayload {} diff --git a/src/collect_s.rs b/src/collect_s.rs index 8d109f2..9358d6d 100644 --- a/src/collect_s.rs +++ b/src/collect_s.rs @@ -15,13 +15,12 @@ use std::any; use std::any::Any; use std::fmt; -// TODO check usage of this trait -pub trait ToJsonBytes { - fn to_json_bytes(&self) -> Result, Error>; +pub trait ToJsonValue: fmt::Debug + Send { + fn to_json_value(&self) -> Result; } -pub trait ToJsonResult: fmt::Debug + AsAnyRef + AsAnyMut + Send { - fn to_json_value(&self) -> Result; +pub trait ToCborValue: fmt::Debug + Send { + fn to_cbor_value(&self) -> Result; } impl AsAnyRef for serde_json::Value { @@ -36,23 +35,17 @@ impl AsAnyMut for serde_json::Value { } } -impl ToJsonResult for serde_json::Value { +impl ToJsonValue for serde_json::Value { fn to_json_value(&self) -> Result { Ok(self.clone()) } } -impl ToJsonBytes for serde_json::Value { - fn to_json_bytes(&self) -> Result, Error> { - Ok(serde_json::to_vec(self)?) - } -} +pub trait CollectedDyn: fmt::Debug + TypeName + Send + AsAnyRef + WithLen + ToJsonValue {} -pub trait CollectedDyn: fmt::Debug + TypeName + Send + AsAnyRef + WithLen + ToJsonResult {} - -impl ToJsonResult for Box { +impl ToJsonValue for Box { fn to_json_value(&self) -> Result { - ToJsonResult::to_json_value(self.as_ref()) + ToJsonValue::to_json_value(self.as_ref()) } } @@ -72,7 +65,7 @@ impl CollectedDyn for Box {} pub trait CollectorTy: fmt::Debug + Send + Unpin + WithLen + ByteEstimate { type Input: CollectableDyn; - type Output: CollectedDyn + ToJsonResult + Serialize; + type Output: CollectedDyn + ToJsonValue + Serialize; fn ingest(&mut self, src: &mut Self::Input); fn set_range_complete(&mut self); diff --git a/src/lib.rs b/src/lib.rs index 8f7e2e1..01a75f8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod apitypes; pub mod collect_s; pub mod container; pub mod events; diff --git a/src/merge.rs b/src/merge.rs index c45b952..5314e55 100644 --- a/src/merge.rs +++ b/src/merge.rs @@ -47,6 +47,7 @@ pub trait MergeableTy: fmt::Debug + WithLen + ByteEstimate + Unpin + Sized { fn tss_for_testing(&self) -> Vec; fn drain_into(&mut self, dst: &mut Self, range: Range) -> DrainIntoDstResult; fn drain_into_new(&mut self, range: Range) -> DrainIntoNewResult; + fn is_consistent(&self) -> bool; } pub trait MergeableDyn: fmt::Debug + WithLen + ByteEstimate + Unpin + AsAnyMut { @@ -59,4 +60,5 @@ pub trait MergeableDyn: fmt::Debug + WithLen + ByteEstimate + Unpin + AsAnyMut { fn drain_into(&mut self, dst: &mut dyn MergeableDyn, range: Range) -> DrainIntoDstResult; fn drain_into_new(&mut self, range: Range) -> DrainIntoNewDynResult; + fn is_consistent(&self) -> bool; } diff --git a/src/timebin.rs b/src/timebin.rs index 601658b..cb4a391 100644 --- a/src/timebin.rs +++ b/src/timebin.rs @@ -1,4 +1,7 @@ +use crate::apitypes::ToUserFacingApiType; use crate::collect_s::CollectableDyn; +use crate::collect_s::ToCborValue; +use crate::collect_s::ToJsonValue; use crate::container::ByteEstimate; use crate::merge::DrainIntoDstResult; use crate::merge::DrainIntoNewDynResult; @@ -86,7 +89,15 @@ where } pub trait BinningggContainerEventsDyn: - fmt::Debug + Send + AsAnyRef + WithLen + ByteEstimate + MergeableDyn + fmt::Debug + + Send + + AsAnyRef + + WithLen + + ByteEstimate + + MergeableDyn + + ToJsonValue + + ToCborValue + + ToUserFacingApiType { fn type_name(&self) -> &'static str; fn binned_events_timeweight_traitobj( @@ -98,10 +109,15 @@ pub trait BinningggContainerEventsDyn: fn serde_id(&self) -> u32; fn nty_id(&self) -> u32; fn eq(&self, rhs: &dyn BinningggContainerEventsDyn) -> bool; - fn verify(&self) -> bool; fn as_mergeable_dyn_mut(&mut self) -> &mut dyn MergeableDyn; } +impl ToUserFacingApiType for Box { + fn to_user_facing_api_type(self) -> Box { + let inner = *self; + } +} + impl MergeableDyn for Box where T: MergeableDyn, @@ -141,6 +157,10 @@ where fn drain_into_new(&mut self, range: Range) -> DrainIntoNewDynResult { self.as_mut().drain_into_new(range) } + + fn is_consistent(&self) -> bool { + self.as_ref().is_consistent() + } } pub trait BinningggContainerBinsDyn: