1use 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#[derive(Debug, Clone)]
19pub enum WasmComponentInput {
20 MiddlewareRequest(middleware_types::Request),
22 MiddlewareResponse(middleware_types::Response),
24}
25
26#[derive(Debug, Clone)]
31pub enum WasmComponentOutput {
32 MiddlewareAction(middleware_types::Action),
34}
35
36impl WasmComponentInput {
37 pub fn from_attach_point(attach_point: &WasmModuleAttachPoint) -> Result<Self, String> {
42 match attach_point {
43 WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnRequest) => {
44 Err("OnRequest requires MiddlewareRequest input. Use WasmComponentInput::MiddlewareRequest directly.".to_string())
47 }
48 WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnResponse) => {
49 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 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 pub fn from_attach_point(attach_point: &WasmModuleAttachPoint) -> Result<Self, String> {
74 match attach_point {
75 WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnRequest) => {
76 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}