Add separate case for empty result

This commit is contained in:
Dominik Werder
2025-02-25 11:33:31 +01:00
parent 9aeb6ecf2f
commit fb155eb6ac
3 changed files with 36 additions and 26 deletions

View File

@@ -21,12 +21,13 @@ use std::task::Context;
use std::task::Poll;
use std::time::Instant;
#[derive(Debug, thiserror::Error)]
#[cstm(name = "CollectDyn")]
pub enum Error {
Msg(String),
NoResultNoCollector,
}
autoerr::create_error_v1!(
name(Error, "CollectDyn"),
enum variants {
Msg(String),
NoResultNoCollector,
},
);
struct ErrMsg<E>(E)
where
@@ -46,6 +47,7 @@ pub enum CollectResult<T>
where
T: fmt::Debug,
{
Empty,
Timeout,
Some(T),
}
@@ -203,7 +205,7 @@ where
}
None => {
debug!("no result because no collector was created");
Ready(Ok(CollectResult::Timeout))
Ready(Ok(CollectResult::Empty))
}
}
} else {

View File

@@ -22,13 +22,14 @@ use serde_json::Value as JsonValue;
use std::time::Duration;
use std::time::Instant;
#[derive(Debug, thiserror::Error)]
#[cstm(name = "PlainEventsJson")]
pub enum Error {
Stream(#[from] crate::plaineventsstream::Error),
Json(#[from] serde_json::Error),
Collect(#[from] crate::collect::Error),
}
autoerr::create_error_v1!(
name(Error, "PlainEventsJson"),
enum variants {
Stream(#[from] crate::plaineventsstream::Error),
Json(#[from] serde_json::Error),
Collect(#[from] crate::collect::Error),
},
);
pub async fn plain_events_json(
evq: &PlainEventsQuery,
@@ -57,15 +58,22 @@ pub async fn plain_events_json(
)
.await?;
warn!("plain_events_json collected {:?}", collected);
if let CollectResult::Some(x) = collected {
let x = x.into_user_facing_api_type_box();
let val = x.into_serializable_json();
let jsval = serde_json::to_string(&val)?;
debug!("plain_events_json json serialized");
Ok(CollectResult::Some(JsonBytes::new(jsval)))
} else {
debug!("plain_events_json timeout");
Ok(CollectResult::Timeout)
match collected {
CollectResult::Some(x) => {
let x = x.into_user_facing_api_type_box();
let val = x.into_serializable_json();
let jsval = serde_json::to_string(&val)?;
debug!("plain_events_json json serialized");
Ok(CollectResult::Some(JsonBytes::new(jsval)))
}
CollectResult::Empty => {
debug!("plain_events_json empty");
Ok(CollectResult::Empty)
}
CollectResult::Timeout => {
debug!("plain_events_json timeout");
Ok(CollectResult::Timeout)
}
}
}

View File

@@ -342,12 +342,12 @@ pub async fn timebinned_json(
let collected: BoxFuture<_> = Box::pin(collected);
let collres = collected.await?;
match collres {
CollectResult::Some(collres) => {
let x = collres.into_user_facing_api_type_box();
let val = x.into_serializable_json();
CollectResult::Some(res) => {
let val = res.into_user_facing_api_type_box().into_serializable_json();
let jsval = serde_json::to_string(&val)?;
Ok(CollectResult::Some(JsonBytes::new(jsval)))
}
CollectResult::Empty => Ok(CollectResult::Empty),
CollectResult::Timeout => Ok(CollectResult::Timeout),
}
}