main.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use std::collections::BTreeMap;
  2. struct VDG {
  3. template_dir: Option<String>,
  4. }
  5. pub struct Config {
  6. lua: rlua::Lua,
  7. }
  8. impl Config {
  9. fn new() -> Config {
  10. let cfg = Config { lua: rlua::Lua::new() };
  11. cfg.lua.context(|ctx| {
  12. if let Err(msg) = Config::setup(&ctx) {
  13. eprintln!("Error setting up context: {:?}", msg);
  14. }
  15. });
  16. cfg
  17. }
  18. fn setup(ctx: &rlua::Context) -> Result<(), rlua::Error> {
  19. ctx.globals().set("out", ctx.create_table()?)?;
  20. ctx.globals().set("template", ctx.create_function(|_ctx, (path, params): (String, BTreeMap<String, String>)| {
  21. println!("template of {} and {:?}!", path, params);
  22. Ok(())
  23. })?)?;
  24. ctx.globals().set("markdown", ctx.create_function(|_ctx, path: String| {
  25. println!("markdown of {}!", path);
  26. Ok(format!("markdown({})", path))
  27. })?)?;
  28. ctx.globals().set("config", ctx.create_function(|_ctx, params: BTreeMap<String, String>| {
  29. println!("config of {:?}!", params);
  30. Ok(())
  31. })?)?;
  32. Ok(())
  33. }
  34. fn load(&mut self, path: &str) -> VDG {
  35. let buf = std::fs::read_to_string(path).unwrap();
  36. self.lua.context(|ctx| {
  37. ctx.load(&buf).exec().unwrap();
  38. });
  39. VDG {
  40. template_dir: None,
  41. }
  42. }
  43. }
  44. fn main() {
  45. Config::new().load("example/simple/vdg.lua");
  46. }