use std::collections::BTreeMap; struct VDG { template_dir: Option, } pub struct Config { lua: rlua::Lua, } impl Config { fn new() -> Config { let cfg = Config { lua: rlua::Lua::new() }; cfg.lua.context(|ctx| { if let Err(msg) = Config::setup(&ctx) { eprintln!("Error setting up context: {:?}", msg); } }); cfg } fn setup(ctx: &rlua::Context) -> Result<(), rlua::Error> { ctx.globals().set("out", ctx.create_table()?)?; ctx.globals().set("template", ctx.create_function(|_ctx, (path, params): (String, BTreeMap)| { println!("template of {} and {:?}!", path, params); Ok(()) })?)?; ctx.globals().set("markdown", ctx.create_function(|_ctx, path: String| { println!("markdown of {}!", path); Ok(format!("markdown({})", path)) })?)?; ctx.globals().set("config", ctx.create_function(|_ctx, params: BTreeMap| { println!("config of {:?}!", params); Ok(()) })?)?; Ok(()) } fn load(&mut self, path: &str) -> VDG { let buf = std::fs::read_to_string(path).unwrap(); self.lua.context(|ctx| { ctx.load(&buf).exec().unwrap(); }); VDG { template_dir: None, } } } fn main() { Config::new().load("example/simple/vdg.lua"); }