use std::future::Future;
use std::sync::Arc;
use serde::Deserialize;
use crate::{Context, RuntimeError};
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct Event<T> {
pub id: String,
pub source: String,
pub detail_type: String,
pub time: Option<String>,
pub detail: T,
}
pub async fn run<App, T, E, H, F>(app: Arc<App>, handler: H) -> Result<(), RuntimeError>
where
App: Send + Sync + 'static,
T: serde::de::DeserializeOwned + Send,
E: Into<crate::runtime::Diagnostic>,
H: Fn(Arc<App>, Event<T>, Context<()>) -> F + Send + Sync,
F: Future<Output = Result<(), E>> + Send,
{
let handler = &handler;
crate::runtime::run(app, move |app, event: Event<T>, context| async move {
handler(app, event, context)
.await
.map(|()| serde_json::Value::Null)
})
.await
}