Last active
May 18, 2020 21:58
-
-
Save sauercrowd/4bb2c5e22c2770cc8225a0c798a7b6b1 to your computer and use it in GitHub Desktop.
Rust Lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use rlua::{Function, Lua, Table}; | |
fn main(){ | |
let lua = Lua::new(); | |
lua.context(|lua_ctx| { | |
let contents = std::fs::read_to_string("parser.lua").expect("Something went wrong reading the file"); | |
// load chunk | |
lua_ctx.load(&contents).set_name("parser")?.exec()?; | |
// get function | |
let globals = lua_ctx.globals(); | |
let parse: Function = globals.get("parse_fn").expect("Cannot get function parse"); | |
// make the call, parse arguments as tuple, returns a table (basically a hashmap in lua) | |
let result: Table = parse.call::<_, Table>(("hello", "world")).expect("Cannot call on function parse"); | |
// use the results from the table | |
let k: String = result.get("key").expect("no key"); | |
let target_url: String = result.get("target_url").expect("no value"); | |
println!("{}: {}", k, target_url); | |
Ok(()) | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse_fn(a, b) | |
return { | |
key = a .. b, | |
target_url = "https://google.com", | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment