2021-02-04 01:09:05 +01:00
|
|
|
# rlua-searcher
|
|
|
|
|
|
|
|
`require` Lua modules by name
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
|
|
|
Encode a Lua module as a `HashMap` of Lua strings indexed by module
|
|
|
|
name. In an `rlua::Context`, pass the `HashMap` to `add_searcher()`,
|
|
|
|
and `require` the module.
|
|
|
|
|
|
|
|
## Synopsis
|
|
|
|
|
|
|
|
```rust
|
2021-02-04 02:16:07 +01:00
|
|
|
use rlua::Lua;
|
|
|
|
use rlua_searcher::{AddSearcher, Result};
|
|
|
|
use std::collections::HashMap;
|
2021-02-04 01:09:05 +01:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let lume = read_lume_to_string();
|
2021-02-04 02:16:07 +01:00
|
|
|
let name = "lume".to_string();
|
|
|
|
let mut map = HashMap::new();
|
2021-02-04 01:09:05 +01:00
|
|
|
map.insert(name, lume);
|
|
|
|
|
2021-02-04 02:16:07 +01:00
|
|
|
let lua = Lua::new();
|
2021-02-04 01:09:05 +01:00
|
|
|
|
2021-02-04 02:16:07 +01:00
|
|
|
let hello = lua
|
|
|
|
.context::<_, Result<String>>(|lua_ctx| {
|
|
|
|
lua_ctx.add_searcher(map)?;
|
|
|
|
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
|
|
|
})
|
|
|
|
.unwrap();
|
2021-02-04 01:09:05 +01:00
|
|
|
|
|
|
|
// prints "hello lume"
|
|
|
|
println!("{}", hello);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_lume_to_string() -> String {
|
|
|
|
r#"return "hello lume""#.to_string()
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
[MIT](LICENSE)
|