12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- use std::collections::BTreeMap;
- struct VDG {
- template_dir: Option<String>,
- }
- 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<String, String>)| {
- 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<String, String>| {
- 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");
- }
|