Skip to main content

aether/
stdlib.rs

1// src/stdlib.rs
2//! Aether Standard Library
3//!
4//! 内置的标准库,在编译时嵌入二进制文件中
5
6/// 字符串工具库
7pub const STRING_UTILS: &str = include_str!("../stdlib/string_utils.aether");
8
9/// 数组工具库
10pub const ARRAY_UTILS: &str = include_str!("../stdlib/array_utils.aether");
11
12/// 数据验证库
13pub const VALIDATION: &str = include_str!("../stdlib/validation.aether");
14
15/// 日期时间库
16pub const DATETIME: &str = include_str!("../stdlib/datetime.aether");
17
18/// 测试框架
19pub const TESTING: &str = include_str!("../stdlib/testing.aether");
20
21/// 集合(Set)数据结构
22pub const SET: &str = include_str!("../stdlib/set.aether");
23
24/// 队列(Queue)数据结构
25pub const QUEUE: &str = include_str!("../stdlib/queue.aether");
26
27/// 栈(Stack)数据结构
28pub const STACK: &str = include_str!("../stdlib/stack.aether");
29
30/// 堆(Heap)数据结构
31pub const HEAP: &str = include_str!("../stdlib/heap.aether");
32
33/// 排序算法
34pub const SORTING: &str = include_str!("../stdlib/sorting.aether");
35
36/// JSON 处理工具
37pub const JSON: &str = include_str!("../stdlib/json.aether");
38
39/// CSV 数据处理
40pub const CSV: &str = include_str!("../stdlib/csv.aether");
41
42/// 函数式编程工具
43pub const FUNCTIONAL: &str = include_str!("../stdlib/functional.aether");
44
45/// CLI 工具库
46pub const CLI_UTILS: &str = include_str!("../stdlib/cli_utils.aether");
47
48/// 文本模板引擎
49pub const TEXT_TEMPLATE: &str = include_str!("../stdlib/text_template.aether");
50
51/// 正则风格文本处理
52pub const REGEX_UTILS: &str = include_str!("../stdlib/regex_utils.aether");
53
54/// 所有标准库模块的列表
55pub const ALL_MODULES: &[(&str, &str)] = &[
56    ("string_utils", STRING_UTILS),
57    ("array_utils", ARRAY_UTILS),
58    ("validation", VALIDATION),
59    ("datetime", DATETIME),
60    ("testing", TESTING),
61    ("set", SET),
62    ("queue", QUEUE),
63    ("stack", STACK),
64    ("heap", HEAP),
65    ("sorting", SORTING),
66    ("json", JSON),
67    ("csv", CSV),
68    ("functional", FUNCTIONAL),
69    ("cli_utils", CLI_UTILS),
70    ("text_template", TEXT_TEMPLATE),
71    ("regex_utils", REGEX_UTILS),
72];
73
74/// 获取指定模块的代码(单一数据源:`ALL_MODULES` 查表)
75pub fn get_module(name: &str) -> Option<&'static str> {
76    ALL_MODULES
77        .iter()
78        .find(|(module_name, _)| *module_name == name)
79        .map(|(_, code)| *code)
80}
81
82/// 获取所有标准库代码(合并为一个字符串)
83pub fn get_all_stdlib() -> String {
84    let mut result = String::new();
85    result.push_str("// Aether Standard Library - Auto-loaded\n\n");
86
87    for (name, code) in ALL_MODULES {
88        result.push_str(&format!("// ========== {} ==========\n", name));
89        result.push_str(code);
90        result.push_str("\n\n");
91    }
92
93    result
94}
95
96/// 标准库预加载器
97///
98/// 用于在 Aether 引擎初始化时加载标准库
99pub fn preload_stdlib(engine: &mut crate::Aether) -> Result<(), String> {
100    for (name, code) in ALL_MODULES {
101        engine
102            .eval(code)
103            .map_err(|e| format!("Failed to load stdlib module '{}': {}", name, e))?;
104    }
105    Ok(())
106}