rearchitect PathSearcher as PolySearcher

to genericize conversion of paths to lua source code
- which facilitates module reloading for e.g.
  - fennel
  - teal
  - typescripttolua
This commit is contained in:
Andy Weidenbaum 2021-02-27 11:10:50 +11:00
parent 587dfbabe4
commit 278a3bb140

View File

@ -3,7 +3,7 @@ use rlua::{Context, MetaMethod, RegistryKey, Table, UserData, UserDataMethods, V
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::path::{Path, PathBuf};
use crate::types::Result;
use crate::utils;
@ -72,26 +72,40 @@ impl UserData for StaticSearcher {
}
}
/// Like `Searcher`, but with `modules` values given as paths to files containing Lua
/// source code to facilitate module reloading.
struct PathSearcher<P>
/// Like `Searcher`, but with `modules` values given as paths to files the content of
/// which can be read as Lua source code.
///
/// Facilitates Lua module reloading, and module reloading of any other programming
/// language whose source code can be compiled to Lua.
struct PolySearcher<P>
where
P: 'static + AsRef<Path> + Send,
{
modules: HashMap<String, P>,
globals: RegistryKey,
/// Function to read file content as Lua source code.
convert: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
}
impl<P> PathSearcher<P>
impl<P> PolySearcher<P>
where
P: 'static + AsRef<Path> + Send,
{
fn new(modules: HashMap<String, P>, globals: RegistryKey) -> Self {
Self { modules, globals }
fn new(
modules: HashMap<String, P>,
globals: RegistryKey,
convert: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
) -> Self {
Self {
modules,
globals,
convert,
}
}
}
impl<P> UserData for PathSearcher<P>
impl<P> UserData for PolySearcher<P>
where
P: 'static + AsRef<Path> + Send,
{
@ -108,11 +122,7 @@ where
path.to_path_buf()
};
let mut content = String::new();
let mut file = File::open(path)
.map_err(|e| LuaError::RuntimeError(format!("io error: {:#?}", e)))?;
file.read_to_string(&mut content)
.map_err(|e| LuaError::RuntimeError(format!("io error: {:#?}", e)))?;
let content = (this.convert)(path)?;
Ok(Value::Function(
lua_ctx
@ -144,6 +154,16 @@ pub trait AddSearcher {
fn add_path_searcher<P>(&self, modules: HashMap<String, P>) -> Result<()>
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>(
&self,
modules: HashMap<String, P>,
convert: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
) -> Result<()>
where
P: 'static + AsRef<Path> + Send;
}
impl<'a> AddSearcher for Context<'a> {
@ -168,13 +188,32 @@ impl<'a> AddSearcher for Context<'a> {
}
fn add_path_searcher<P>(&self, modules: HashMap<String, P>) -> Result<()>
where
P: 'static + AsRef<Path> + Send,
{
let convert = Box::new(|path| {
let mut content = String::new();
let mut file = File::open(path)
.map_err(|e| LuaError::RuntimeError(format!("io error: {:#?}", e)))?;
file.read_to_string(&mut content)
.map_err(|e| LuaError::RuntimeError(format!("io error: {:#?}", e)))?;
Ok(content)
});
self.add_poly_searcher(modules, convert)
}
fn add_poly_searcher<P>(
&self,
modules: HashMap<String, P>,
convert: Box<dyn Fn(PathBuf) -> rlua::Result<String> + Send>,
) -> Result<()>
where
P: 'static + AsRef<Path> + Send,
{
let globals = self.globals();
let searchers: Table = globals.get::<_, Table>("package")?.get("searchers")?;
let registry_key = self.create_registry_value(globals)?;
let searcher = PathSearcher::new(modules, registry_key);
let searcher = PolySearcher::new(modules, registry_key, convert);
searchers
.set(searchers.len()? + 1, searcher)
.map_err(|e| e.into())