kotoba_handler/
runtime.rs

1//! Runtime management for different execution environments
2
3use crate::error::{HandlerError, Result};
4use crate::types::HandlerCapabilities;
5use std::sync::Arc;
6
7/// Runtime environment types
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum RuntimeType {
10    Native,
11    Wasm,
12    NodeJs,
13    Deno,
14    Browser,
15}
16
17/// Runtime environment
18pub struct RuntimeEnvironment {
19    runtime_type: RuntimeType,
20    capabilities: HandlerCapabilities,
21    memory_limit: u64,
22    timeout_limit: u64,
23}
24
25impl RuntimeEnvironment {
26    /// Create new runtime environment
27    pub fn new(runtime_type: RuntimeType) -> Self {
28        let capabilities = match runtime_type {
29            RuntimeType::Native => HandlerCapabilities {
30                supports_async: true,
31                supports_streaming: true,
32                supports_websocket: true,
33                supports_file_upload: true,
34                max_payload_size: 100 * 1024 * 1024, // 100MB
35                supported_content_types: vec![
36                    "application/json".to_string(),
37                    "text/plain".to_string(),
38                    "application/x-www-form-urlencoded".to_string(),
39                    "multipart/form-data".to_string(),
40                ],
41            },
42            RuntimeType::Wasm => HandlerCapabilities {
43                supports_async: true,
44                supports_streaming: false,
45                supports_websocket: true,
46                supports_file_upload: false,
47                max_payload_size: 10 * 1024 * 1024, // 10MB
48                supported_content_types: vec![
49                    "application/json".to_string(),
50                    "text/plain".to_string(),
51                ],
52            },
53            RuntimeType::NodeJs | RuntimeType::Deno => HandlerCapabilities {
54                supports_async: true,
55                supports_streaming: true,
56                supports_websocket: true,
57                supports_file_upload: true,
58                max_payload_size: 50 * 1024 * 1024, // 50MB
59                supported_content_types: vec![
60                    "application/json".to_string(),
61                    "text/plain".to_string(),
62                    "application/javascript".to_string(),
63                ],
64            },
65            RuntimeType::Browser => HandlerCapabilities {
66                supports_async: true,
67                supports_streaming: false,
68                supports_websocket: true,
69                supports_file_upload: true,
70                max_payload_size: 5 * 1024 * 1024, // 5MB
71                supported_content_types: vec![
72                    "application/json".to_string(),
73                    "text/plain".to_string(),
74                ],
75            },
76        };
77
78        Self {
79            runtime_type,
80            capabilities,
81            memory_limit: 100 * 1024 * 1024, // 100MB
82            timeout_limit: 30_000, // 30 seconds
83        }
84    }
85
86    /// Get runtime type
87    pub fn runtime_type(&self) -> &RuntimeType {
88        &self.runtime_type
89    }
90
91    /// Get capabilities
92    pub fn capabilities(&self) -> &HandlerCapabilities {
93        &self.capabilities
94    }
95
96    /// Check if runtime supports a specific feature
97    pub fn supports(&self, feature: &str) -> bool {
98        match feature {
99            "async" => self.capabilities.supports_async,
100            "streaming" => self.capabilities.supports_streaming,
101            "websocket" => self.capabilities.supports_websocket,
102            "file_upload" => self.capabilities.supports_file_upload,
103            _ => false,
104        }
105    }
106
107    /// Get memory limit
108    pub fn memory_limit(&self) -> u64 {
109        self.memory_limit
110    }
111
112    /// Get timeout limit
113    pub fn timeout_limit(&self) -> u64 {
114        self.timeout_limit
115    }
116
117    /// Set memory limit
118    pub fn set_memory_limit(&mut self, limit: u64) {
119        self.memory_limit = limit;
120    }
121
122    /// Set timeout limit
123    pub fn set_timeout_limit(&mut self, limit: u64) {
124        self.timeout_limit = limit;
125    }
126}
127
128impl Default for RuntimeEnvironment {
129    fn default() -> Self {
130        Self::new(RuntimeType::Native)
131    }
132}
133
134/// Runtime manager
135pub struct RuntimeManager {
136    environments: std::collections::HashMap<String, Arc<RuntimeEnvironment>>,
137    current: Option<String>,
138}
139
140impl RuntimeManager {
141    /// Create new runtime manager
142    pub fn new() -> Self {
143        let mut environments = std::collections::HashMap::new();
144
145        // Add default environments
146        environments.insert("native".to_string(), Arc::new(RuntimeEnvironment::new(RuntimeType::Native)));
147        environments.insert("wasm".to_string(), Arc::new(RuntimeEnvironment::new(RuntimeType::Wasm)));
148        environments.insert("nodejs".to_string(), Arc::new(RuntimeEnvironment::new(RuntimeType::NodeJs)));
149        environments.insert("deno".to_string(), Arc::new(RuntimeEnvironment::new(RuntimeType::Deno)));
150        environments.insert("browser".to_string(), Arc::new(RuntimeEnvironment::new(RuntimeType::Browser)));
151
152        Self {
153            environments,
154            current: Some("native".to_string()),
155        }
156    }
157
158    /// Register new runtime environment
159    pub fn register_environment(&mut self, name: String, env: Arc<RuntimeEnvironment>) {
160        self.environments.insert(name, env);
161    }
162
163    /// Get runtime environment by name
164    pub fn get_environment(&self, name: &str) -> Option<Arc<RuntimeEnvironment>> {
165        self.environments.get(name).cloned()
166    }
167
168    /// Set current runtime
169    pub fn set_current(&mut self, name: &str) -> Result<()> {
170        if self.environments.contains_key(name) {
171            self.current = Some(name.to_string());
172            Ok(())
173        } else {
174            Err(HandlerError::Config(format!("Runtime '{}' not found", name)))
175        }
176    }
177
178    /// Get current runtime
179    pub fn get_current(&self) -> Option<Arc<RuntimeEnvironment>> {
180        self.current.as_ref()
181            .and_then(|name| self.environments.get(name))
182            .cloned()
183    }
184
185    /// List all available runtimes
186    pub fn list_runtimes(&self) -> Vec<String> {
187        self.environments.keys().cloned().collect()
188    }
189}
190
191impl Default for RuntimeManager {
192    fn default() -> Self {
193        Self::new()
194    }
195}