Skip to main content

smg_wasm/
types.rs

1//! WASM Component Type System
2//!
3//! Provides generic input/output types for WASM component execution
4//! based on attach points.
5
6use wasmtime::{component::ResourceTable, StoreLimits};
7use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
8
9use crate::{
10    module::{MiddlewareAttachPoint, WasmModuleAttachPoint},
11    spec::smg::gateway::middleware_types,
12};
13
14/// Generic input type for WASM component execution
15///
16/// This enum represents all possible input types that can be passed
17/// to a WASM component, determined by the attach_point.
18#[derive(Debug, Clone)]
19pub enum WasmComponentInput {
20    /// Middleware OnRequest input
21    MiddlewareRequest(middleware_types::Request),
22    /// Middleware OnResponse input
23    MiddlewareResponse(middleware_types::Response),
24}
25
26/// Generic output type from WASM component execution
27///
28/// This enum represents all possible output types that can be returned
29/// from a WASM component, determined by the attach_point.
30#[derive(Debug, Clone)]
31pub enum WasmComponentOutput {
32    /// Middleware Action output
33    MiddlewareAction(middleware_types::Action),
34}
35
36impl WasmComponentInput {
37    /// Create input based on attach_point and raw data
38    ///
39    /// This helper function validates that the attach_point matches
40    /// the expected input type.
41    pub fn from_attach_point(attach_point: &WasmModuleAttachPoint) -> Result<Self, String> {
42        match attach_point {
43            WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnRequest) => {
44                // OnRequest expects a Request type, but we can't construct it here
45                // The caller should use MiddlewareRequest variant directly
46                Err("OnRequest requires MiddlewareRequest input. Use WasmComponentInput::MiddlewareRequest directly.".to_string())
47            }
48            WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnResponse) => {
49                // OnResponse expects a Response type
50                Err("OnResponse requires MiddlewareResponse input. Use WasmComponentInput::MiddlewareResponse directly.".to_string())
51            }
52            WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnError) => {
53                Err("OnError attach point not yet implemented".to_string())
54            }
55        }
56    }
57
58    /// Get the expected attach_point for this input type
59    pub fn expected_attach_point(&self) -> WasmModuleAttachPoint {
60        match self {
61            WasmComponentInput::MiddlewareRequest(_) => {
62                WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnRequest)
63            }
64            WasmComponentInput::MiddlewareResponse(_) => {
65                WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnResponse)
66            }
67        }
68    }
69}
70
71impl WasmComponentOutput {
72    /// Get the attach_point that produced this output type
73    pub fn from_attach_point(attach_point: &WasmModuleAttachPoint) -> Result<Self, String> {
74        match attach_point {
75            WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnRequest) => {
76                // This would be set after execution
77                Err("Cannot create output before execution".to_string())
78            }
79            WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnResponse) => {
80                Err("Cannot create output before execution".to_string())
81            }
82            WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnError) => {
83                Err("OnError attach point not yet implemented".to_string())
84            }
85        }
86    }
87}
88
89pub struct WasiState {
90    pub ctx: WasiCtx,
91    pub table: ResourceTable,
92    pub limits: StoreLimits,
93}
94
95impl WasiView for WasiState {
96    fn ctx(&mut self) -> WasiCtxView<'_> {
97        WasiCtxView {
98            ctx: &mut self.ctx,
99            table: &mut self.table,
100        }
101    }
102}