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 crate::proxy_wasm::traits::RootContext;

use crate::{bootstrap::Launcher, handler::IntoHandler, middleware::EventHandlerStack, Entrypoint};

use super::{
    context::{RequestContext, ResponseContext},
    on_request, on_response, IntoFlow,
};

impl<H, T> Entrypoint<RequestContext, T> for H
where
    for<'a> H: IntoHandler<RequestContext, T> + Clone + 'a,
    H::Output: IntoFlow<RequestData = ()>,
{
    fn create_root_context(
        self,
        event_handlers: EventHandlerStack,
        context_id: u32,
    ) -> Box<dyn RootContext> {
        let entrypoint = move |launcher: Launcher| launcher.launch(on_request(self.clone()));
        entrypoint.create_root_context(event_handlers, context_id)
    }
}

impl<H, I> Entrypoint<ResponseContext<()>, I> for H
where
    for<'a> H: IntoHandler<ResponseContext<()>, 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(on_response(self.clone()));
        entrypoint.create_root_context(event_handlers, context_id)
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        client::HttpClient,
        hl::{RequestState, ResponseState},
        middleware::EventHandlerStack,
        Entrypoint,
    };

    #[test]
    fn request_entrypoint() {
        async fn on_request(_: RequestState) {}

        on_request.create_root_context(EventHandlerStack::default(), 100);
    }

    #[test]
    fn injected_request_entrypoint() {
        async fn on_request(_: RequestState, _: HttpClient) {}

        on_request.create_root_context(EventHandlerStack::default(), 100);
    }

    #[test]
    fn response_entrypoint() {
        async fn on_response(_: ResponseState) {}

        on_response.create_root_context(EventHandlerStack::default(), 100);
    }

    #[test]
    fn injected_response_entrypoint() {
        async fn on_response(_: RequestState, _: HttpClient) {}

        on_response.create_root_context(EventHandlerStack::default(), 100);
    }
}