use wasmtime::{component::ResourceTable, StoreLimits};
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
use crate::{
module::{MiddlewareAttachPoint, WasmModuleAttachPoint},
spec::smg::gateway::middleware_types,
};
#[derive(Debug, Clone)]
pub enum WasmComponentInput {
MiddlewareRequest(middleware_types::Request),
MiddlewareResponse(middleware_types::Response),
}
#[derive(Debug, Clone)]
pub enum WasmComponentOutput {
MiddlewareAction(middleware_types::Action),
}
impl WasmComponentInput {
pub fn from_attach_point(attach_point: &WasmModuleAttachPoint) -> Result<Self, String> {
match attach_point {
WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnRequest) => {
Err("OnRequest requires MiddlewareRequest input. Use WasmComponentInput::MiddlewareRequest directly.".to_string())
}
WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnResponse) => {
Err("OnResponse requires MiddlewareResponse input. Use WasmComponentInput::MiddlewareResponse directly.".to_string())
}
WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnError) => {
Err("OnError attach point not yet implemented".to_string())
}
}
}
pub fn expected_attach_point(&self) -> WasmModuleAttachPoint {
match self {
WasmComponentInput::MiddlewareRequest(_) => {
WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnRequest)
}
WasmComponentInput::MiddlewareResponse(_) => {
WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnResponse)
}
}
}
}
impl WasmComponentOutput {
pub fn from_attach_point(attach_point: &WasmModuleAttachPoint) -> Result<Self, String> {
match attach_point {
WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnRequest) => {
Err("Cannot create output before execution".to_string())
}
WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnResponse) => {
Err("Cannot create output before execution".to_string())
}
WasmModuleAttachPoint::Middleware(MiddlewareAttachPoint::OnError) => {
Err("OnError attach point not yet implemented".to_string())
}
}
}
}
pub struct WasiState {
pub ctx: WasiCtx,
pub table: ResourceTable,
pub limits: StoreLimits,
}
impl WasiView for WasiState {
fn ctx(&mut self) -> WasiCtxView<'_> {
WasiCtxView {
ctx: &mut self.ctx,
table: &mut self.table,
}
}
}