pdk-classy 1.9.1-alpha.2

PDK Classy
Documentation
// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

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;

/// Trait that defines the entrypoint for a [`Plugin`](crate::plugin::Plugin).
pub trait Entrypoint<C, I> {
    /// Generate a [`RootContext`] with the given event handlers and context id.
    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(),
        ))
    }
}