Skip to main content

aether/api/
stdlib.rs

1use super::Aether;
2use crate::stdlib;
3
4impl Aether {
5    /// 加载特定的标准库模块
6    ///
7    /// 可用模块见 `stdlib::ALL_MODULES`(string_utils、array_utils、
8    /// validation、datetime、testing、set、queue、stack、heap、sorting、
9    /// json、csv、functional、cli_utils、text_template、regex_utils)。
10    /// 未知模块名返回 Err。
11    pub fn load_stdlib_module(&mut self, module_name: &str) -> Result<(), String> {
12        if let Some(code) = stdlib::get_module(module_name) {
13            self.eval(code)?;
14            Ok(())
15        } else {
16            Err(format!("Unknown stdlib module: {}", module_name))
17        }
18    }
19
20    /// 加载所有标准库模块
21    pub fn load_all_stdlib(&mut self) -> Result<(), String> {
22        stdlib::preload_stdlib(self)
23    }
24
25    /// 加载特定标准库模块(可链式调用),未知模块名报错
26    ///
27    /// ```no_run
28    /// # use aether::Aether;
29    /// let engine = Aether::new()
30    ///     .with_stdlib_module("string_utils")
31    ///     .expect("模块加载失败");
32    /// ```
33    pub fn with_stdlib_module(mut self, module_name: &str) -> Result<Self, String> {
34        self.load_stdlib_module(module_name)?;
35        Ok(self)
36    }
37}