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
use rlua::prelude::*; use base64; pub fn init(lua: &Lua) -> crate::Result<()> { let module = lua.create_table()?; module.set("encode", lua.create_function(|_, text: String| { Ok(base64::encode(&text)) })?)?; module.set("decode", lua.create_function(|_, text: String| { let val = base64::decode(&text).map_err(LuaError::external)?; String::from_utf8(val).map_err(LuaError::external) })?)?; lua.globals().set("base64", module)?; Ok(()) } #[cfg(test)] mod tests { use super::*; #[test] fn lua_base64_encode() { let lua = Lua::new(); init(&lua).unwrap(); lua.exec::<_, ()>(r#" local val = base64.encode("Hello, World!") assert(val == "SGVsbG8sIFdvcmxkIQ==") "#, None).unwrap(); } #[test] fn lua_base64_decode() { let lua = Lua::new(); init(&lua).unwrap(); lua.exec::<_, ()>(r#" local val = base64.decode("SGVsbG8sIFdvcmxkIQ==") assert(val == "Hello, World!") "#, None).unwrap(); } }