-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
80 lines (71 loc) · 2.58 KB
/
Copy pathmod.rs
File metadata and controls
80 lines (71 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::script::lua_enter_point::LuaEntryPoint;
use crate::script::storage::{BoxedStorage, StorageManager};
use crate::script::user_editable_config::UserEditConfig;
use mlua::{IntoLua, UserData, UserDataFields, UserDataMethods};
use std::collections::BTreeMap;
use std::ops::Deref;
use std::rc::Rc;
use typed_builder::TypedBuilder;
use self::storage::Storage;
pub mod error;
pub mod lua_enter_point;
pub mod storage;
pub mod user_config_define;
pub mod user_editable_config;
#[derive(Debug, TypedBuilder)]
/// a loaded script
pub struct Script<'lua> {
#[builder(setter(transform = |identity:&str|Rc::from(String::from(identity).into_boxed_str())))]
/// the unique identity of this script
pub identity: Rc<str>,
/// the script name for user using
#[builder(setter(transform = |name:&str| String::from(name)))]
pub name: String,
#[builder(setter(transform = |storage: &BoxedStorage| StorageManager(storage.clone())))]
/// using for set/get local storage
pub storage: StorageManager,
/// user edit able configs
pub configs: Rc<BTreeMap<Rc<str>, Rc<UserEditConfig>>>,
/// entry points
pub entry_point: LuaEntryPoint<'lua>,
}
impl<'lua> Script<'lua> {
pub fn get_config(&self) -> ScriptConfig {
ScriptConfig {
identity: self.identity.clone(),
configs: self.configs.clone(),
storage: self.storage.clone(),
}
}
}
pub struct ScriptConfig {
identity: Rc<str>,
configs: Rc<BTreeMap<Rc<str>, Rc<UserEditConfig>>>,
storage: StorageManager,
}
impl UserData for ScriptConfig {
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("identity", |vm, this| this.identity.into_lua(vm));
fields.add_field_method_get("config", |vm, this| {
let iter = this.configs.iter().map(|(k, v)| (k.deref(), Rc::clone(v)));
vm.create_table_from(iter)
});
}
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_method("storageLoad", |_vm, this, key: String| {
let ret = this
.storage
.load(&this.identity, key.as_str())
.map_err(mlua::Error::external)?;
Ok(ret.map(|v| String::from_utf8_lossy(&v).to_string()))
});
methods.add_method(
"storageStore",
|_vm, this, (key, value): (String, String)| {
this.storage
.store(&this.identity, key.as_str(), value.as_bytes())
.map_err(mlua::Error::external)
},
);
}
}