devalang_core/core/store/
global.rs

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