devalang_core/core/store/
global.rs

1use crate::core::{parser::statement::Statement, preprocessor::module::Module};
2use devalang_types::{FunctionTable, VariableTable, plugin::PluginInfo};
3use std::collections::HashMap;
4
5#[derive(Debug, Clone)]
6pub struct GlobalStore {
7    pub modules: HashMap<String, Module>,
8    pub variables: VariableTable,
9    pub functions: FunctionTable,
10    pub events: HashMap<String, Vec<Statement>>,
11    pub plugins: HashMap<String, (PluginInfo, Vec<u8>)>,
12}
13
14impl Default for GlobalStore {
15    fn default() -> Self {
16        Self::new()
17    }
18}
19
20impl GlobalStore {
21    pub fn new() -> Self {
22        GlobalStore {
23            modules: HashMap::new(),
24            functions: FunctionTable::new(),
25            variables: VariableTable::new(),
26            events: HashMap::new(),
27            plugins: HashMap::new(),
28        }
29    }
30
31    pub fn insert_module(&mut self, path: String, module: Module) {
32        self.modules.insert(path, module);
33    }
34
35    pub fn modules_mut(&mut self) -> &mut HashMap<String, Module> {
36        &mut self.modules
37    }
38
39    pub fn get_module(&self, path: &str) -> Option<&Module> {
40        self.modules.get(path)
41    }
42
43    pub fn remove_module(&mut self, path: &str) -> Option<Module> {
44        self.modules.remove(path)
45    }
46
47    pub fn register_event_handler(&mut self, event: &str, handler: Statement) {
48        self.events
49            .entry(event.to_string())
50            .or_default()
51            .push(handler);
52    }
53
54    pub fn get_event_handlers(&self, event: &str) -> Option<&Vec<Statement>> {
55        self.events.get(event)
56    }
57}