use std::rc::Rc;
use crate::proxy_wasm::traits::RootContext;
use crate::bootstrap::Launcher;
use crate::context::root::AsyncRootContext;
use crate::extract::context::{ConfigureContext, FilterContext};
use crate::handler::{IntoHandler, IntoHandlerResult};
use crate::middleware::EventHandlerStack;
use crate::types::RootCid;
pub trait Entrypoint<C, I> {
fn create_root_context(
self,
event_handlers: EventHandlerStack,
context_id: u32,
) -> Box<dyn RootContext>;
}
impl<H, I> Entrypoint<FilterContext, I> for H
where
for<'a> H: IntoHandler<FilterContext, I, Output = ()> + Clone + 'a,
{
fn create_root_context(
self,
event_handlers: EventHandlerStack,
context_id: u32,
) -> Box<dyn RootContext> {
let entrypoint = move |launcher: Launcher| launcher.launch(self.clone());
entrypoint.create_root_context(event_handlers, context_id)
}
}
impl<H, I> Entrypoint<ConfigureContext, I> for H
where
H: IntoHandler<ConfigureContext, I>,
H::Handler: 'static,
H::Output: IntoHandlerResult,
{
fn create_root_context(
self,
event_handlers: EventHandlerStack,
context_id: u32,
) -> Box<dyn RootContext> {
Box::new(AsyncRootContext::new(
RootCid::from(context_id),
Rc::new(crate::host::DefaultHost),
Rc::new(crate::host::clock::DefaultClock),
Rc::new(crate::host::grpc::DefaultGrpcHost),
Rc::new(crate::host::shared_data::DefaultSharedData),
#[cfg(feature = "experimental_metrics")]
Rc::new(crate::host::metrics::DefaultMetricsHost),
event_handlers,
self.into_handler(),
))
}
}