initial stab at LuaPush implementation, doesn't currently work right now though. Also don't set type as that is a lua builtin.

This commit is contained in:
Adrian Malacoda 2018-02-26 23:31:17 -06:00
parent 44b6eecd70
commit 80bcfe0580

View File

@ -4,7 +4,7 @@ use toml::Value;
use toml::value::Table; use toml::value::Table;
use hlua; use hlua;
use hlua::{Lua, LuaFunction, AnyHashableLuaValue, AnyLuaValue}; use hlua::{Lua, LuaFunction, AnyHashableLuaValue, AnyLuaValue, AsMutLua};
use {User, Message, Channel, NullMessageSender}; use {User, Message, Channel, NullMessageSender};
use event::{Event, Envelope}; use event::{Event, Envelope};
@ -34,15 +34,48 @@ impl LuaModule {
} }
} }
fn set (lua: &mut Lua, key: &str, value: &Value) { struct TomlValueWrapper(Value);
match value.clone() { implement_lua_read!(TomlValueWrapper);
impl TomlValueWrapper {
pub fn lua_set (self, lua: &mut Lua, key: &str) {
match self.0 {
Value::String(string) => lua.set(key, string), Value::String(string) => lua.set(key, string),
Value::Integer(integer) => lua.set(key, integer as i32), Value::Integer(integer) => lua.set(key, (integer as i32)),
Value::Float(float) => lua.set(key, float), Value::Float(float) => lua.set(key, float),
Value::Boolean(boolean) => lua.set(key, boolean), Value::Boolean(boolean) => lua.set(key, boolean),
_ => {} Value::Table(table) => {},
Value::Array(array) => {},
Value::Datetime(datetime) => lua.set(key, datetime.to_string())
} }
} }
}
/*impl<'lua, L: AsMutLua<'lua>> hlua::Push<L> for TomlValueWrapper {
type Err = hlua::Void;
fn push_to_lua (self, lua: L) -> Result<hlua::PushGuard<L>, (hlua::Void, L)> {
match self.0 {
Value::String(string) => string.push_to_lua(lua),
Value::Integer(integer) => (integer as i32).push_to_lua(lua),
Value::Float(float) => float.push_to_lua(lua),
Value::Boolean(boolean) => boolean.push_to_lua(lua),
Value::Table(table) => {
let hashmap: HashMap<_, _> = table.into_iter().map(|(key, value)| {
(key, TomlValueWrapper(value))
}).collect();
hashmap.push_to_lua(lua)
},
Value::Array(array) => {
let vec: Vec<_> = array.into_iter().map(TomlValueWrapper).collect();
vec.push_to_lua(lua)
},
Value::Datetime(datetime) => datetime.to_string().push_to_lua(lua)
}
}
}
impl<'lua, L: AsMutLua<'lua>> hlua::PushOne<L> for TomlValueWrapper {}*/
struct MessageWrapper { struct MessageWrapper {
envelope: Arc<Envelope> envelope: Arc<Envelope>
@ -90,6 +123,7 @@ implement_lua_push!(SenderWrapper, |mut metatable| {
channel: data.get(&AnyHashableLuaValue::LuaString("channel".to_owned())) channel: data.get(&AnyHashableLuaValue::LuaString("channel".to_owned()))
.and_then(|value| match value { .and_then(|value| match value {
&AnyLuaValue::LuaString (ref string_value) => Some(string_value.to_owned()), &AnyLuaValue::LuaString (ref string_value) => Some(string_value.to_owned()),
&AnyLuaValue::LuaNumber (ref number_value) => Some(format!("{}", number_value)),
_ => None _ => None
}).map(|channel| Channel { }).map(|channel| Channel {
name: channel, name: channel,
@ -118,7 +152,9 @@ impl EventLoop for LuaModule {
lua.openlibs(); lua.openlibs();
for (key, value) in &self.variables { for (key, value) in &self.variables {
set(&mut lua, key, value); if key != "type" {
TomlValueWrapper(value.clone()).lua_set(&mut lua, key);
}
} }
lua.set("sender", SenderWrapper { lua.set("sender", SenderWrapper {
@ -139,7 +175,9 @@ impl EventLoop for LuaModule {
match envelope.event { match envelope.event {
Event::Configure { ref configuration } => { Event::Configure { ref configuration } => {
for (key, value) in configuration { for (key, value) in configuration {
set(&mut lua, key, value); if key != "type" {
TomlValueWrapper(value.clone()).lua_set(&mut lua, key);
}
} }
}, },
Event::Message { ref message } => { Event::Message { ref message } => {