1use std::rc::Rc;
6
7use crate::proxy_wasm::traits::RootContext;
8
9use crate::bootstrap::Launcher;
10use crate::context::root::AsyncRootContext;
11use crate::extract::context::{ConfigureContext, FilterContext};
12use crate::handler::{IntoHandler, IntoHandlerResult};
13use crate::middleware::EventHandlerStack;
14use crate::types::RootCid;
15
16pub trait Entrypoint<C, I> {
18 fn create_root_context(
20 self,
21 event_handlers: EventHandlerStack,
22 context_id: u32,
23 ) -> Box<dyn RootContext>;
24}
25
26impl<H, I> Entrypoint<FilterContext, I> for H
27where
28 for<'a> H: IntoHandler<FilterContext, I, Output = ()> + Clone + 'a,
29{
30 fn create_root_context(
31 self,
32 event_handlers: EventHandlerStack,
33 context_id: u32,
34 ) -> Box<dyn RootContext> {
35 let entrypoint = move |launcher: Launcher| launcher.launch(self.clone());
36 entrypoint.create_root_context(event_handlers, context_id)
37 }
38}
39
40impl<H, I> Entrypoint<ConfigureContext, I> for H
41where
42 H: IntoHandler<ConfigureContext, I>,
43 H::Handler: 'static,
44 H::Output: IntoHandlerResult,
45{
46 fn create_root_context(
47 self,
48 event_handlers: EventHandlerStack,
49 context_id: u32,
50 ) -> Box<dyn RootContext> {
51 Box::new(AsyncRootContext::new(
52 RootCid::from(context_id),
53 Rc::new(crate::host::DefaultHost),
54 Rc::new(crate::host::clock::DefaultClock),
55 Rc::new(crate::host::grpc::DefaultGrpcHost),
56 Rc::new(crate::host::shared_data::DefaultSharedData),
57 #[cfg(feature = "experimental_metrics")]
58 Rc::new(crate::host::metrics::DefaultMetricsHost),
59 event_handlers,
60 self.into_handler(),
61 ))
62 }
63}