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 types;
33mod executor;
34mod languages;
35
36#[cfg(feature = "wasm")]
37mod wasm_bindings;
38
39pub use types::*;
40pub use executor::Executor;
41
42/// Re-exports of language-specific executors for advanced use.
43pub mod lang {
44    #[cfg(feature = "rhai")]
45    pub use crate::languages::rhai::RhaiExecutor;
46
47    #[cfg(feature = "lua")]
48    pub use crate::languages::lua::LuaExecutor;
49
50    #[cfg(feature = "javascript")]
51    pub use crate::languages::javascript::JavaScriptExecutor;
52
53    #[cfg(feature = "python")]
54    pub use crate::languages::python::PythonExecutor;
55}
56
57/// Get a list of supported languages based on enabled features
58#[allow(clippy::vec_init_then_push)]
59pub fn supported_languages() -> Vec<Language> {
60    let mut languages = Vec::new();
61
62    #[cfg(feature = "rhai")]
63    languages.push(Language::Rhai);
64
65    #[cfg(feature = "lua")]
66    languages.push(Language::Lua);
67
68    #[cfg(feature = "javascript")]
69    languages.push(Language::JavaScript);
70
71    #[cfg(feature = "python")]
72    languages.push(Language::Python);
73
74    languages
75}
76
77/// Check if a language is supported
78pub fn is_language_supported(language: Language) -> bool {
79    match language {
80        #[cfg(feature = "rhai")]
81        Language::Rhai => true,
82
83        #[cfg(feature = "lua")]
84        Language::Lua => true,
85
86        #[cfg(feature = "javascript")]
87        Language::JavaScript => true,
88
89        #[cfg(feature = "python")]
90        Language::Python => true,
91
92        #[allow(unreachable_patterns)]
93        _ => false,
94    }
95}