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:
parent
587dfbabe4
commit
278a3bb140
@ -3,7 +3,7 @@ use rlua::{Context, MetaMethod, RegistryKey, Table, UserData, UserDataMethods, V
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use crate::types::Result;
|
use crate::types::Result;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
@ -72,26 +72,40 @@ impl UserData for StaticSearcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like `Searcher`, but with `modules` values given as paths to files containing Lua
|
/// Like `Searcher`, but with `modules` values given as paths to files the content of
|
||||||
/// source code to facilitate module reloading.
|
/// which can be read as Lua source code.
|
||||||
struct PathSearcher<P>
|
///
|
||||||
|
/// Facilitates Lua module reloading, and module reloading of any other programming
|
||||||
|
/// language whose source code can be compiled to Lua.
|
||||||
|
struct PolySearcher<P>
|
||||||
where
|
where
|
||||||
P: 'static + AsRef<Path> + Send,
|
P: 'static + AsRef<Path> + Send,
|
||||||
{
|
{
|
||||||
modules: HashMap<String, P>,
|
modules: HashMap<String, P>,
|
||||||
globals: RegistryKey,
|
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
|
where
|
||||||
P: 'static + AsRef<Path> + Send,
|
P: 'static + AsRef<Path> + Send,
|
||||||
{
|
{
|
||||||
fn new(modules: HashMap<String, P>, globals: RegistryKey) -> Self {
|
fn new(
|
||||||
Self { modules, globals }
|
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
|
where
|
||||||
P: 'static + AsRef<Path> + Send,
|
P: 'static + AsRef<Path> + Send,
|
||||||
{
|
{
|
||||||
@ -108,11 +122,7 @@ where
|
|||||||
path.to_path_buf()
|
path.to_path_buf()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut content = String::new();
|
let content = (this.convert)(path)?;
|
||||||
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(Value::Function(
|
Ok(Value::Function(
|
||||||
lua_ctx
|
lua_ctx
|
||||||
@ -144,6 +154,16 @@ pub trait AddSearcher {
|
|||||||
fn add_path_searcher<P>(&self, modules: HashMap<String, P>) -> Result<()>
|
fn add_path_searcher<P>(&self, modules: HashMap<String, P>) -> Result<()>
|
||||||
where
|
where
|
||||||
P: 'static + AsRef<Path> + Send;
|
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> {
|
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<()>
|
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
|
where
|
||||||
P: 'static + AsRef<Path> + Send,
|
P: 'static + AsRef<Path> + Send,
|
||||||
{
|
{
|
||||||
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 = PathSearcher::new(modules, registry_key);
|
let searcher = PolySearcher::new(modules, registry_key, convert);
|
||||||
searchers
|
searchers
|
||||||
.set(searchers.len()? + 1, searcher)
|
.set(searchers.len()? + 1, searcher)
|
||||||
.map_err(|e| e.into())
|
.map_err(|e| e.into())
|
||||||
|
Loading…
Reference in New Issue
Block a user