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
use rlua::prelude::*;
use serde_yaml;
use rlua_serde;
use crate::Result;
pub fn init(lua: &Lua) -> Result<()> {
let module = lua.create_table()?;
module.set("to_table", lua.create_function(|lua, text: String| {
let doc: serde_yaml::Value = serde_yaml::from_str(&text).map_err(LuaError::external)?;
let lua_value = rlua_serde::to_value(lua, &doc)?;
Ok(lua_value)
})?)?;
module.set("from_table", lua.create_function(|_, value: LuaValue| {
let lua_value: serde_yaml::Value = rlua_serde::from_value(value)?;
let string = serde_yaml::to_string(&lua_value).map_err(LuaError::external)?;
Ok(string)
})?)?;
lua.globals().set("yaml", module)?;
Ok(())
}