summaryrefslogtreecommitdiff
path: root/src/state/mod.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-08-02 00:36:10 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-08-02 00:36:10 +0200
commitfe0938b1de0c46fc2afcaa3dcd6a0f4ec870d21a (patch)
tree8db7509894842395cfb309f00c41b7f4d173888c /src/state/mod.rs
parent1e1eb95926f556e666bc20355327abd24d264858 (diff)
add state which is shared with the lua environment
Diffstat (limited to 'src/state/mod.rs')
-rw-r--r--src/state/mod.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/state/mod.rs b/src/state/mod.rs
new file mode 100644
index 0000000..5efd770
--- /dev/null
+++ b/src/state/mod.rs
@@ -0,0 +1,36 @@
+use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
+
+use mlua::{UserData, UserDataFields};
+
+
+pub mod sheetview;
+
+#[derive(Debug, Default)]
+pub struct GlobalState {
+ pub sheetview: sheetview::SheetViewState,
+}
+
+static GLOBAL_STATE: RwLock<GlobalState> = RwLock::new(GlobalState::new());
+const DUMMY_STATE: GlobalState = GlobalState::new();
+
+impl GlobalState {
+ const fn new() -> Self {
+ Self {
+ sheetview: sheetview::SheetViewState::new()
+ }
+ }
+
+ pub fn instance() -> RwLockReadGuard<'static, Self> {
+ GLOBAL_STATE.read().unwrap()
+ }
+
+ pub fn instance_mut() -> RwLockWriteGuard<'static, Self> {
+ GLOBAL_STATE.write().unwrap()
+ }
+}
+
+impl UserData for GlobalState {
+ fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
+ fields.add_field_function_get("sheetview", |_, _| Ok(DUMMY_STATE.sheetview))
+ }
+}