s/PolySearcher/PathSearcherPoly, convert/transform
s/add_poly_searcher/add_path_searcher_poly
This commit is contained in:
parent
278a3bb140
commit
53432900c1
@ -77,7 +77,7 @@ impl UserData for StaticSearcher {
|
||||
///
|
||||
/// Facilitates Lua module reloading, and module reloading of any other programming
|
||||
/// language whose source code can be compiled to Lua.
|
||||
struct PolySearcher<P>
|
||||
struct PathSearcherPoly<P>
|
||||
where
|
||||
P: 'static + AsRef<Path> + Send,
|
||||
{
|
||||
@ -85,27 +85,27 @@ where
|
||||
globals: RegistryKey,
|
||||
|
||||
/// Function to read file content as Lua source code.
|
||||
convert: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
|
||||
transform: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
|
||||
}
|
||||
|
||||
impl<P> PolySearcher<P>
|
||||
impl<P> PathSearcherPoly<P>
|
||||
where
|
||||
P: 'static + AsRef<Path> + Send,
|
||||
{
|
||||
fn new(
|
||||
modules: HashMap<String, P>,
|
||||
globals: RegistryKey,
|
||||
convert: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
|
||||
transform: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
|
||||
) -> Self {
|
||||
Self {
|
||||
modules,
|
||||
globals,
|
||||
convert,
|
||||
transform,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> UserData for PolySearcher<P>
|
||||
impl<P> UserData for PathSearcherPoly<P>
|
||||
where
|
||||
P: 'static + AsRef<Path> + Send,
|
||||
{
|
||||
@ -122,7 +122,7 @@ where
|
||||
path.to_path_buf()
|
||||
};
|
||||
|
||||
let content = (this.convert)(path)?;
|
||||
let content = (this.transform)(path)?;
|
||||
|
||||
Ok(Value::Function(
|
||||
lua_ctx
|
||||
@ -155,12 +155,12 @@ pub trait AddSearcher {
|
||||
where
|
||||
P: 'static + AsRef<Path> + Send;
|
||||
|
||||
/// Like `add_path_searcher`, but with user-provided closure for converting source
|
||||
/// code to Lua.
|
||||
fn add_poly_searcher<P>(
|
||||
/// Like `add_path_searcher`, but with user-provided closure for transforming
|
||||
/// source code to Lua.
|
||||
fn add_path_searcher_poly<P>(
|
||||
&self,
|
||||
modules: HashMap<String, P>,
|
||||
convert: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
|
||||
transform: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
|
||||
) -> Result<()>
|
||||
where
|
||||
P: 'static + AsRef<Path> + Send;
|
||||
@ -191,7 +191,7 @@ impl<'a> AddSearcher for Context<'a> {
|
||||
where
|
||||
P: 'static + AsRef<Path> + Send,
|
||||
{
|
||||
let convert = Box::new(|path| {
|
||||
let transform = Box::new(|path| {
|
||||
let mut content = String::new();
|
||||
let mut file = File::open(path)
|
||||
.map_err(|e| LuaError::RuntimeError(format!("io error: {:#?}", e)))?;
|
||||
@ -199,13 +199,13 @@ impl<'a> AddSearcher for Context<'a> {
|
||||
.map_err(|e| LuaError::RuntimeError(format!("io error: {:#?}", e)))?;
|
||||
Ok(content)
|
||||
});
|
||||
self.add_poly_searcher(modules, convert)
|
||||
self.add_path_searcher_poly(modules, transform)
|
||||
}
|
||||
|
||||
fn add_poly_searcher<P>(
|
||||
fn add_path_searcher_poly<P>(
|
||||
&self,
|
||||
modules: HashMap<String, P>,
|
||||
convert: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
|
||||
transform: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
|
||||
) -> Result<()>
|
||||
where
|
||||
P: 'static + AsRef<Path> + Send,
|
||||
@ -213,7 +213,7 @@ impl<'a> AddSearcher for Context<'a> {
|
||||
let globals = self.globals();
|
||||
let searchers: Table = globals.get::<_, Table>("package")?.get("searchers")?;
|
||||
let registry_key = self.create_registry_value(globals)?;
|
||||
let searcher = PolySearcher::new(modules, registry_key, convert);
|
||||
let searcher = PathSearcherPoly::new(modules, registry_key, transform);
|
||||
searchers
|
||||
.set(searchers.len()? + 1, searcher)
|
||||
.map_err(|e| e.into())
|
||||
|
Loading…
Reference in New Issue
Block a user