Skip to main content

wasm_sandbox/wrappers/
mod.rs

1//! Code generation for wrapping applications
2
3use std::path::{Path, PathBuf};
4use std::collections::HashMap;
5use serde::{Deserialize, Serialize};
6
7pub mod cli_tool;
8pub mod http_server;
9pub mod mcp_server;
10pub mod generic;
11pub mod http_server_impl;
12
13// Re-export HTTP server generator
14pub use http_server_impl::{HttpServerGenerator, HttpServerConfig};
15
16use crate::error::Result;
17
18/// Wrapper generator for sandboxing applications
19pub trait WrapperGenerator {
20    /// Generate wrapper code for the target application
21    fn generate_wrapper(&self, spec: &WrapperSpec) -> Result<String>;
22    
23    /// Compile the wrapper to WebAssembly
24    fn compile_wrapper(&self, code: &str, output_path: &Path) -> Result<()>;
25    
26    /// Generate and compile in one step
27    fn generate_and_compile(
28        &self,
29        spec: &WrapperSpec,
30        output_path: &Path,
31    ) -> Result<()> {
32        let code = self.generate_wrapper(spec)?;
33        self.compile_wrapper(&code, output_path)
34    }
35}
36
37/// Type of application to wrap
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub enum ApplicationType {
40    /// HTTP server
41    HttpServer { port: u16 },
42    
43    /// MCP server
44    McpServer { port: u16, schema_path: Option<PathBuf> },
45    
46    /// CLI tool
47    CliTool { interactive: bool },
48    
49    /// Generic executable
50    Generic,
51}
52
53/// Communication specification
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct CommunicationSpec {
56    /// Communication protocol
57    pub protocol: CommunicationProtocol,
58    
59    /// Bidirectional channels
60    pub channels: Vec<String>,
61    
62    /// RPC functions
63    pub rpc_functions: HashMap<String, String>,
64}
65
66impl Default for CommunicationSpec {
67    fn default() -> Self {
68        Self {
69            protocol: CommunicationProtocol::Json,
70            channels: vec!["default".to_string()],
71            rpc_functions: HashMap::new(),
72        }
73    }
74}
75
76/// Communication protocol
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub enum CommunicationProtocol {
79    /// JSON
80    Json,
81    
82    /// MessagePack
83    MessagePack,
84    
85    /// Custom protocol
86    Custom(String),
87}
88
89/// Specification for wrapper generation
90#[derive(Debug, Clone, Serialize, Deserialize)]
91pub struct WrapperSpec {
92    /// Type of application to wrap
93    pub app_type: ApplicationType,
94    
95    /// Path to the application
96    pub app_path: PathBuf,
97    
98    /// Command-line arguments
99    pub arguments: Vec<String>,
100    
101    /// Environment variables
102    pub environment: HashMap<String, String>,
103    
104    /// Working directory
105    pub working_directory: Option<PathBuf>,
106    
107    /// Communication protocol settings
108    pub communication: CommunicationSpec,
109    
110    /// Custom template variables
111    pub template_variables: HashMap<String, String>,
112}