s/PolySearcher/PathSearcherPoly, convert/transform

s/add_poly_searcher/add_path_searcher_poly
This commit is contained in:
Andy Weidenbaum 2021-03-04 12:22:40 +11:00
parent 278a3bb140
commit 53432900c1

View File

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