This commit is contained in:
Dominik Werder
2024-11-26 16:28:30 +01:00
parent 03e52b6bc6
commit ec322b5f50
6 changed files with 84 additions and 18 deletions

View File

@@ -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"

49
src/apitypes.rs Normal file
View File

@@ -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<T> ContPayload for T where T: fmt::Debug + Serialize + Send {}
pub trait UserApiType: ToCborValue {}
pub trait ToUserFacingApiType {
fn to_user_facing_api_type(self) -> Box<dyn UserApiType>;
}
#[derive(Serialize)]
pub struct ContainerEventsApi<EVT>
where
EVT: ContPayload,
{
pub tss: VecDeque<u64>,
pub values: VecDeque<EVT>,
}
impl<EVT> fmt::Debug for ContainerEventsApi<EVT>
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<EVT> ToCborValue for ContainerEventsApi<EVT>
where
EVT: ContPayload,
{
fn to_cbor_value(&self) -> Result<ciborium::Value, ciborium::value::Error> {
// let mut out = Vec::new();
// ciborium::ser::into_writer(self, &mut out).unwrap();
let val = ciborium::value::Value::serialized(self).unwrap();
Ok(val)
}
}
impl<EVT> UserApiType for ContainerEventsApi<EVT> where EVT: ContPayload {}

View File

@@ -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<Vec<u8>, Error>;
pub trait ToJsonValue: fmt::Debug + Send {
fn to_json_value(&self) -> Result<serde_json::Value, serde_json::Error>;
}
pub trait ToJsonResult: fmt::Debug + AsAnyRef + AsAnyMut + Send {
fn to_json_value(&self) -> Result<serde_json::Value, serde_json::Error>;
pub trait ToCborValue: fmt::Debug + Send {
fn to_cbor_value(&self) -> Result<ciborium::Value, ciborium::value::Error>;
}
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<serde_json::Value, serde_json::Error> {
Ok(self.clone())
}
}
impl ToJsonBytes for serde_json::Value {
fn to_json_bytes(&self) -> Result<Vec<u8>, 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<dyn CollectedDyn> {
impl ToJsonValue for Box<dyn CollectedDyn> {
fn to_json_value(&self) -> Result<serde_json::Value, serde_json::Error> {
ToJsonResult::to_json_value(self.as_ref())
ToJsonValue::to_json_value(self.as_ref())
}
}
@@ -72,7 +65,7 @@ impl CollectedDyn for Box<dyn CollectedDyn> {}
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);

View File

@@ -1,3 +1,4 @@
pub mod apitypes;
pub mod collect_s;
pub mod container;
pub mod events;

View File

@@ -47,6 +47,7 @@ pub trait MergeableTy: fmt::Debug + WithLen + ByteEstimate + Unpin + Sized {
fn tss_for_testing(&self) -> Vec<TsMs>;
fn drain_into(&mut self, dst: &mut Self, range: Range<usize>) -> DrainIntoDstResult;
fn drain_into_new(&mut self, range: Range<usize>) -> DrainIntoNewResult<Self>;
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<usize>)
-> DrainIntoDstResult;
fn drain_into_new(&mut self, range: Range<usize>) -> DrainIntoNewDynResult;
fn is_consistent(&self) -> bool;
}

View File

@@ -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<dyn BinningggContainerEventsDyn> {
fn to_user_facing_api_type(self) -> Box<dyn crate::apitypes::UserApiType> {
let inner = *self;
}
}
impl<T> MergeableDyn for Box<T>
where
T: MergeableDyn,
@@ -141,6 +157,10 @@ where
fn drain_into_new(&mut self, range: Range<usize>) -> DrainIntoNewDynResult {
self.as_mut().drain_into_new(range)
}
fn is_consistent(&self) -> bool {
self.as_ref().is_consistent()
}
}
pub trait BinningggContainerBinsDyn: