Skip to main content

brainwires_code_interpreters/
lib.rs

1#![deny(missing_docs)]
2//! # Code Interpreters
3//!
4//! Sandboxed code execution for multiple programming languages.
5//! Designed to work both natively and compiled to WASM for browser execution.
6//!
7//! ## Supported Languages
8//!
9//! | Language | Feature | Speed | Power | Notes |
10//! |----------|---------|-------|-------|-------|
11//! | Rhai | `rhai` | ⚡⚡⚡⚡ | ⭐⭐ | Native Rust, fastest startup |
12//! | Lua | `lua` | ⚡⚡⚡ | ⭐⭐⭐ | Small runtime, good stdlib |
13//! | JavaScript | `javascript` | ⚡⚡ | ⭐⭐⭐⭐ | ECMAScript compliant (Boa) |
14//! | Python | `python` | ⚡ | ⭐⭐⭐⭐⭐ | CPython 3.12 compatible |
15//!
16//! ## Example
17//!
18//! ```rust
19//! use brainwires_code_interpreters::{Executor, ExecutionRequest, Language};
20//!
21//! let executor = Executor::new();
22//! let result = executor.execute(ExecutionRequest {
23//!     language: Language::Rhai,
24//!     code: "let x = 1 + 2; x".to_string(),
25//!     ..Default::default()
26//! });
27//!
28//! assert!(result.success);
29//! assert_eq!(result.stdout, "3");
30//! ```
31
32mod executor;
33mod languages;
34mod types;
35
36#[cfg(feature = "docker")]
37pub mod docker;
38
39#[cfg(feature = "wasm")]
40mod wasm_bindings;
41
42pub use executor::Executor;
43pub use types::*;
44
45/// Re-exports of language-specific executors for advanced use.
46pub mod lang {
47    #[cfg(feature = "rhai")]
48    pub use crate::languages::rhai::RhaiExecutor;
49
50    #[cfg(feature = "lua")]
51    pub use crate::languages::lua::LuaExecutor;
52
53    #[cfg(feature = "javascript")]
54    pub use crate::languages::javascript::JavaScriptExecutor;
55
56    #[cfg(feature = "python")]
57    pub use crate::languages::python::PythonExecutor;
58}
59
60/// Get a list of supported languages based on enabled features
61#[allow(clippy::vec_init_then_push)]
62pub fn supported_languages() -> Vec<Language> {
63    let mut languages = Vec::new();
64
65    #[cfg(feature = "rhai")]
66    languages.push(Language::Rhai);
67
68    #[cfg(feature = "lua")]
69    languages.push(Language::Lua);
70
71    #[cfg(feature = "javascript")]
72    languages.push(Language::JavaScript);
73
74    #[cfg(feature = "python")]
75    languages.push(Language::Python);
76
77    languages
78}
79
80/// Check if a language is supported
81pub fn is_language_supported(language: Language) -> bool {
82    match language {
83        #[cfg(feature = "rhai")]
84        Language::Rhai => true,
85
86        #[cfg(feature = "lua")]
87        Language::Lua => true,
88
89        #[cfg(feature = "javascript")]
90        Language::JavaScript => true,
91
92        #[cfg(feature = "python")]
93        Language::Python => true,
94
95        #[allow(unreachable_patterns)]
96        _ => false,
97    }
98}