aether/api/
constructors.rs

1use super::Aether;
2use crate::builtins::IOPermissions;
3use crate::evaluator::Evaluator;
4use crate::optimizer::Optimizer;
5use crate::stdlib;
6
7impl Aether {
8    /// 创建新的 Aether 引擎实例
9    ///
10    /// **用于 DSL 嵌入**:IO 操作默认禁用以确保安全性。
11    /// 使用 `with_permissions()` 或 `with_all_permissions()` 来启用 IO。
12    ///
13    /// **用于 CLI 使用**:命令行工具默认使用 `with_all_permissions()`。
14    pub fn new() -> Self {
15        Self::with_permissions(IOPermissions::default())
16    }
17
18    /// 使用自定义 IO 权限创建新的 Aether 引擎
19    pub fn with_permissions(permissions: IOPermissions) -> Self {
20        Aether {
21            evaluator: Evaluator::with_permissions(permissions),
22            cache: crate::cache::ASTCache::new(),
23            optimizer: Optimizer::new(),
24        }
25    }
26
27    /// 创建启用所有 IO 权限的新 Aether 引擎
28    pub fn with_all_permissions() -> Self {
29        Self::with_permissions(IOPermissions::allow_all())
30    }
31
32    /// 创建预加载标准库的新 Aether 引擎
33    ///
34    /// 这将创建一个具有所有权限的引擎,并自动加载
35    /// 所有标准库模块(string_utils、array_utils、validation、datetime、testing)。
36    pub fn with_stdlib() -> Result<Self, String> {
37        let mut engine = Self::with_all_permissions();
38        stdlib::preload_stdlib(&mut engine)?;
39        Ok(engine)
40    }
41}
42
43impl Default for Aether {
44    fn default() -> Self {
45        Self::new()
46    }
47}