diff --git a/src/modules/lua.rs b/src/modules/lua.rs index 03eea2e..5a09aaf 100644 --- a/src/modules/lua.rs +++ b/src/modules/lua.rs @@ -4,7 +4,7 @@ use toml::Value; use toml::value::Table; use hlua; -use hlua::{Lua, LuaFunction, AnyHashableLuaValue, AnyLuaValue}; +use hlua::{Lua, LuaFunction, AnyHashableLuaValue, AnyLuaValue, AsMutLua}; use {User, Message, Channel, NullMessageSender}; use event::{Event, Envelope}; @@ -34,16 +34,49 @@ impl LuaModule { } } -fn set (lua: &mut Lua, key: &str, value: &Value) { - match value.clone() { - Value::String(string) => lua.set(key, string), - Value::Integer(integer) => lua.set(key, integer as i32), - Value::Float(float) => lua.set(key, float), - Value::Boolean(boolean) => lua.set(key, boolean), - _ => {} +struct TomlValueWrapper(Value); +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::Integer(integer) => lua.set(key, (integer as i32)), + Value::Float(float) => lua.set(key, float), + 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 for TomlValueWrapper { + type Err = hlua::Void; + + fn push_to_lua (self, lua: L) -> Result, (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 for TomlValueWrapper {}*/ + struct MessageWrapper { envelope: Arc } @@ -90,6 +123,7 @@ implement_lua_push!(SenderWrapper, |mut metatable| { channel: data.get(&AnyHashableLuaValue::LuaString("channel".to_owned())) .and_then(|value| match value { &AnyLuaValue::LuaString (ref string_value) => Some(string_value.to_owned()), + &AnyLuaValue::LuaNumber (ref number_value) => Some(format!("{}", number_value)), _ => None }).map(|channel| Channel { name: channel, @@ -118,7 +152,9 @@ impl EventLoop for LuaModule { lua.openlibs(); 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 { @@ -139,7 +175,9 @@ impl EventLoop for LuaModule { match envelope.event { Event::Configure { ref 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 } => {