devalang_wasm/language/preprocessor/loader/
mod.rs1use crate::language::syntax::ast::{Statement, StatementKind, Value};
2use crate::language::syntax::parser::driver::SimpleParser;
3use anyhow::Result;
5use std::collections::HashMap;
6use std::path::Path;
7
8pub fn load_module_from_path(_path: &Path) -> Result<()> {
10 Ok(())
12}
13
14pub fn inject_dependencies() -> Result<()> {
16 Ok(())
18}
19
20pub struct ModuleExports {
22 pub variables: HashMap<String, Value>,
23 pub groups: HashMap<String, Vec<Statement>>,
24 pub patterns: HashMap<String, Statement>,
25}
26
27pub fn load_module_exports(path: &Path) -> Result<ModuleExports> {
28 let mut variables: HashMap<String, Value> = HashMap::new();
29 let mut groups: HashMap<String, Vec<Statement>> = HashMap::new();
30 let mut patterns: HashMap<String, Statement> = HashMap::new();
31
32 let stmts = SimpleParser::parse_file(path)?;
33
34 let mut exports: Vec<String> = Vec::new();
36 for s in &stmts {
37 if let StatementKind::Export { names, .. } = &s.kind {
38 for n in names {
39 exports.push(n.clone());
40 }
41 }
42 }
43
44 for s in &stmts {
45 match &s.kind {
46 StatementKind::Let { name, value }
47 | StatementKind::Var { name, value }
48 | StatementKind::Const { name, value } => {
49 if exports.contains(name) {
50 if let Some(v) = value {
51 variables.insert(name.clone(), v.clone());
52 }
53 }
54 }
55 StatementKind::Group {
56 name,
57 body,
58 duration: _,
59 } => {
60 if exports.contains(name) {
61 groups.insert(name.clone(), body.clone());
62 }
63 }
64 StatementKind::Pattern { name, .. } => {
65 if exports.contains(name) {
66 patterns.insert(name.clone(), s.clone());
67 }
68 }
69 _ => {}
70 }
71 }
72
73 Ok(ModuleExports {
74 variables,
75 groups,
76 patterns,
77 })
78}