Skip to main content

platform_module/
binding.rs

1//! A module's behavior contract: only what varies across loading sources.
2
3use platform_core::EventHandlerRegistry;
4use platform_runtime::{FunctionRegistry, RuntimeClient};
5use std::sync::Arc;
6
7#[derive(Debug, Clone, Default)]
8pub struct EventHandlerRegistrationContext {
9    runtime: Option<EventHandlerRuntimeContext>,
10}
11
12impl EventHandlerRegistrationContext {
13    #[must_use]
14    pub fn empty() -> Self {
15        Self::default()
16    }
17
18    #[must_use]
19    pub fn with_runtime(
20        runtime_client: RuntimeClient,
21        function_registry: Arc<FunctionRegistry>,
22    ) -> Self {
23        Self {
24            runtime: Some(EventHandlerRuntimeContext {
25                runtime_client,
26                function_registry,
27            }),
28        }
29    }
30
31    #[must_use]
32    pub fn runtime(&self) -> Option<&EventHandlerRuntimeContext> {
33        self.runtime.as_ref()
34    }
35}
36
37#[derive(Debug, Clone)]
38pub struct EventHandlerRuntimeContext {
39    pub runtime_client: RuntimeClient,
40    pub function_registry: Arc<FunctionRegistry>,
41}
42
43/// Narrow by design — pure data lives in [`crate::ModuleManifest`], read
44/// directly by upper layers, never through this trait.
45///
46/// HTTP routing is deliberately EXCLUDED from this cross-source trait: it
47/// carries utoipa `OpenApiRouter` types that out-of-process/Wasm sources cannot
48/// produce. Linked modules can still carry source-specific HTTP behavior on
49/// [`crate::LinkedBinding`].
50pub trait ModuleBinding: std::fmt::Debug + Send + Sync {
51    /// Register this module's runtime functions into the shared registry.
52    fn register_functions(&self, registry: &mut FunctionRegistry);
53
54    /// Register this module's in-process event handlers.
55    fn register_event_handlers(
56        &self,
57        registry: &mut EventHandlerRegistry,
58        context: &EventHandlerRegistrationContext,
59    );
60}