Skip to main content

seqc/
stdlib_embed.rs

1//! Embedded Standard Library
2//!
3//! Contains stdlib modules embedded at compile time.
4//! This makes seqc fully self-contained - no need for external stdlib files.
5
6use std::collections::HashMap;
7use std::sync::LazyLock;
8
9/// Embedded stdlib files (name -> content)
10static STDLIB: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
11    let mut m = HashMap::new();
12    m.insert("imath", include_str!("../stdlib/imath.seq"));
13    m.insert("fmath", include_str!("../stdlib/fmath.seq"));
14    m.insert("json", include_str!("../stdlib/json.seq"));
15    m.insert("yaml", include_str!("../stdlib/yaml.seq"));
16    m.insert("http", include_str!("../stdlib/http.seq"));
17    m.insert("stack-utils", include_str!("../stdlib/stack-utils.seq"));
18    m.insert("map", include_str!("../stdlib/map.seq"));
19    m.insert("list", include_str!("../stdlib/list.seq"));
20    m.insert("son", include_str!("../stdlib/son.seq"));
21    m.insert("signal", include_str!("../stdlib/signal.seq"));
22    m.insert("zipper", include_str!("../stdlib/zipper.seq"));
23    m.insert("loops", include_str!("../stdlib/loops.seq"));
24    m
25});
26
27/// Get an embedded stdlib module by name
28pub fn get_stdlib(name: &str) -> Option<&'static str> {
29    STDLIB.get(name).copied()
30}
31
32/// Check if a stdlib module exists (embedded)
33pub fn has_stdlib(name: &str) -> bool {
34    STDLIB.contains_key(name)
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_imath_stdlib_exists() {
43        assert!(has_stdlib("imath"));
44        let content = get_stdlib("imath").unwrap();
45        assert!(content.contains("abs"));
46    }
47
48    #[test]
49    fn test_fmath_stdlib_exists() {
50        assert!(has_stdlib("fmath"));
51        let content = get_stdlib("fmath").unwrap();
52        assert!(content.contains("f.abs"));
53    }
54
55    #[test]
56    fn test_loops_stdlib_exists() {
57        assert!(has_stdlib("loops"));
58        let content = get_stdlib("loops").unwrap();
59        assert!(content.contains("times"));
60        assert!(content.contains("each-integer"));
61        assert!(content.contains("integer-fold"));
62    }
63
64    #[test]
65    fn test_nonexistent_stdlib() {
66        assert!(!has_stdlib("nonexistent"));
67        assert!(get_stdlib("nonexistent").is_none());
68    }
69}