pub mod go_runtime;
pub mod python_runtime;
pub mod rust_native;
use anyhow::Result;
use go_runtime::GoRuntime;
use python_runtime::PythonRuntime;
use rust_native::RustOptimizer;
use std::sync::Arc;
#[derive(Debug)]
pub struct MultiLanguageRuntime {
python: Option<PythonRuntime>,
go: Option<GoRuntime>,
rust: Arc<RustOptimizer>,
}
impl MultiLanguageRuntime {
pub fn new() -> Self {
MultiLanguageRuntime {
python: None,
go: None,
rust: Arc::new(RustOptimizer::new()),
}
}
pub fn init_python(&mut self, amber_api: Arc<go_runtime::AmberAPI>) -> Result<()> {
self.python = Some(PythonRuntime::new(amber_api)?);
Ok(())
}
pub fn init_go(&mut self, amber_api: Arc<go_runtime::AmberAPI>) -> Result<()> {
self.go = Some(GoRuntime::new(amber_api)?);
Ok(())
}
pub async fn execute(&self, language: &str, code: &str) -> Result<String> {
match language.to_lowercase().as_str() {
"python" | "py" => {
if let Some(python) = &self.python {
python.execute_python(code).await
} else {
Err(anyhow::anyhow!("Python runtime not initialized"))
}
}
"go" => {
if let Some(go) = &self.go {
go.execute_go(code).await
} else {
Err(anyhow::anyhow!("Go runtime not initialized"))
}
}
"rust" | "rs" => {
let optimized: _ = self.rust.optimize_hot_path(code).await?;
Ok(optimized.optimized)
}
_ => Err(anyhow::anyhow!("Unsupported language: {}", language)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::multilang::go_runtime::AmberRuntimeInterface;
#[tokio::test]
async fn test_multilang_runtime() {
let mut runtime = MultiLanguageRuntime::new();
let python_api: _ = Arc::new(go_runtime::AmberAPI {
runtime: Arc::new(MockAmberRuntime),
});
runtime.init_python(python_api).unwrap();
let result: _ = runtime.execute("python", "print('Hello Python')").await;
assert!(result.is_ok());
let go_api: _ = Arc::new(go_runtime::AmberAPI {
runtime: Arc::new(MockAmberRuntime),
});
runtime.init_go(go_api).unwrap();
let result: _ = runtime.execute("go", "fmt.Println('Hello Go')").await;
assert!(result.is_ok());
let result: _ = runtime
.execute("rust", "fn main() { println!('Hello'); }")
.await;
assert!(result.is_ok());
}
struct MockAmberRuntime;
impl AmberRuntimeInterface for MockAmberRuntime {
fn execute_script(&self, script: &str) -> Result<String> {
Ok(format!("Executed: {}", script))
}
fn get_variable(&self, _name: &str) -> Result<String> {
Ok("mock_value".to_string())
}
fn set_variable(&self, _name: &str, _value: &str) -> Result<()> {
Ok(())
}
}
}