add CatSearcher
This commit is contained in:
@@ -5,7 +5,7 @@ use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::types::Result;
|
||||
use crate::types::{CatMap, Result};
|
||||
|
||||
/// Stores Lua modules indexed by module name, and provides an `rlua::MetaMethod` to
|
||||
/// enable `require`ing the stored modules by name in an `rlua::Context`.
|
||||
@@ -118,7 +118,7 @@ where
|
||||
/// `rlua::Context` with Rust code.
|
||||
///
|
||||
/// Enables exposing `UserData` types to an `rlua::Context`.
|
||||
pub struct ClosureSearcher {
|
||||
struct ClosureSearcher {
|
||||
/// Closures must accept three parameters:
|
||||
///
|
||||
/// 1. An `rlua::Context`, which the closure can do what it wants with.
|
||||
@@ -142,7 +142,7 @@ pub struct ClosureSearcher {
|
||||
}
|
||||
|
||||
impl ClosureSearcher {
|
||||
pub fn new(
|
||||
fn new(
|
||||
modules: HashMap<
|
||||
Cow<'static, str>,
|
||||
Box<
|
||||
@@ -182,7 +182,7 @@ impl UserData for ClosureSearcher {
|
||||
/// up an `rlua::Context` with Rust code.
|
||||
///
|
||||
/// Enables exposing `UserData` types to an `rlua::Context`.
|
||||
pub struct FunctionSearcher {
|
||||
struct FunctionSearcher {
|
||||
/// Functions must accept three parameters:
|
||||
///
|
||||
/// 1. An `rlua::Context`, which the function body can do what it wants with.
|
||||
@@ -203,7 +203,7 @@ pub struct FunctionSearcher {
|
||||
}
|
||||
|
||||
impl FunctionSearcher {
|
||||
pub fn new(
|
||||
fn new(
|
||||
modules: HashMap<
|
||||
Cow<'static, str>,
|
||||
for<'ctx> fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>,
|
||||
@@ -236,6 +236,45 @@ impl UserData for FunctionSearcher {
|
||||
}
|
||||
}
|
||||
|
||||
/// Like `Searcher`, but with `CatMap` to facilitate indexing heterogenous strings and paths -
|
||||
/// all presumed to resolve to Lua module content - by module names in `modules`.
|
||||
struct CatSearcher {
|
||||
modules: CatMap,
|
||||
globals: RegistryKey,
|
||||
}
|
||||
|
||||
impl CatSearcher {
|
||||
fn new(modules: CatMap, globals: RegistryKey) -> Self {
|
||||
Self { modules, globals }
|
||||
}
|
||||
}
|
||||
|
||||
impl UserData for CatSearcher {
|
||||
fn add_methods<'lua, M>(methods: &mut M)
|
||||
where
|
||||
M: UserDataMethods<'lua, Self>,
|
||||
{
|
||||
methods.add_meta_method(MetaMethod::Call, |lua_ctx, this, name: String| {
|
||||
let name = Cow::from(name);
|
||||
match this.modules.get(&name) {
|
||||
Some(content) => {
|
||||
let content = content
|
||||
.cat()
|
||||
.map_err(|e| rlua::Error::RuntimeError(format!("io error: {}", e)))?;
|
||||
Ok(Value::Function(
|
||||
lua_ctx
|
||||
.load(&content)
|
||||
.set_name(name.as_ref())?
|
||||
.set_environment(lua_ctx.registry_value::<Table>(&this.globals)?)?
|
||||
.into_function()?,
|
||||
))
|
||||
}
|
||||
None => Ok(Value::Nil),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Extend `rlua::Context` to support `require`ing Lua modules by name.
|
||||
pub trait AddSearcher {
|
||||
/// Add a `HashMap` of Lua modules indexed by module name to Lua's `package.searchers`
|
||||
@@ -279,6 +318,10 @@ pub trait AddSearcher {
|
||||
for<'ctx> fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>,
|
||||
>,
|
||||
) -> Result<()>;
|
||||
|
||||
/// Like `add_searcher`, except `modules` can contain heterogenous strings and paths
|
||||
/// indexed by module name.
|
||||
fn add_cat_searcher(&self, modules: CatMap) -> Result<()>;
|
||||
}
|
||||
|
||||
impl<'a> AddSearcher for Context<'a> {
|
||||
@@ -358,4 +401,14 @@ impl<'a> AddSearcher for Context<'a> {
|
||||
.set(searchers.len()? + 1, searcher)
|
||||
.map_err(|e| e.into())
|
||||
}
|
||||
|
||||
fn add_cat_searcher(&self, modules: CatMap) -> Result<()> {
|
||||
let globals = self.globals();
|
||||
let searchers: Table = globals.get::<_, Table>("package")?.get("searchers")?;
|
||||
let registry_key = self.create_registry_value(globals)?;
|
||||
let searcher = CatSearcher::new(modules, registry_key);
|
||||
searchers
|
||||
.set(searchers.len()? + 1, searcher)
|
||||
.map_err(|e| e.into())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user