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/// 获取指定模块的代码
75pub fn get_module(name: &str) -> Option<&'static str> {
76    match name {
77        "string_utils" => Some(STRING_UTILS),
78        "array_utils" => Some(ARRAY_UTILS),
79        "validation" => Some(VALIDATION),
80        "datetime" => Some(DATETIME),
81        "testing" => Some(TESTING),
82        "set" => Some(SET),
83        "queue" => Some(QUEUE),
84        "stack" => Some(STACK),
85        "heap" => Some(HEAP),
86        "sorting" => Some(SORTING),
87        "json" => Some(JSON),
88        "csv" => Some(CSV),
89        "functional" => Some(FUNCTIONAL),
90        "cli_utils" => Some(CLI_UTILS),
91        "text_template" => Some(TEXT_TEMPLATE),
92        "regex_utils" => Some(REGEX_UTILS),
93        _ => None,
94    }
95}
96
97/// 获取所有标准库代码(合并为一个字符串)
98pub fn get_all_stdlib() -> String {
99    let mut result = String::new();
100    result.push_str("// Aether Standard Library - Auto-loaded\n\n");
101
102    for (name, code) in ALL_MODULES {
103        result.push_str(&format!("// ========== {} ==========\n", name));
104        result.push_str(code);
105        result.push_str("\n\n");
106    }
107
108    result
109}
110
111/// 标准库预加载器
112///
113/// 用于在 Aether 引擎初始化时加载标准库
114pub fn preload_stdlib(engine: &mut crate::Aether) -> Result<(), String> {
115    for (name, code) in ALL_MODULES {
116        engine
117            .eval(code)
118            .map_err(|e| format!("Failed to load stdlib module '{}': {}", name, e))?;
119    }
120    Ok(())
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126
127    #[test]
128    fn test_module_exists() {
129        assert!(STRING_UTILS.len() > 0);
130        assert!(ARRAY_UTILS.len() > 0);
131        assert!(VALIDATION.len() > 0);
132        assert!(DATETIME.len() > 0);
133        assert!(TESTING.len() > 0);
134        assert!(SET.len() > 0);
135        assert!(QUEUE.len() > 0);
136        assert!(STACK.len() > 0);
137        assert!(HEAP.len() > 0);
138        assert!(SORTING.len() > 0);
139    }
140
141    #[test]
142    fn test_get_module() {
143        assert!(get_module("string_utils").is_some());
144        assert!(get_module("array_utils").is_some());
145        assert!(get_module("set").is_some());
146        assert!(get_module("queue").is_some());
147        assert!(get_module("stack").is_some());
148        assert!(get_module("heap").is_some());
149        assert!(get_module("sorting").is_some());
150        assert!(get_module("unknown").is_none());
151    }
152}