- Removes closures and function calls for types that implement default: ```rust // Change let _my_str = get_str().unwrap_or(String::new); // To let _my_str = get_str().unwrap_or_default(); ``` - Uses the `.cloned()/.copied()` methods where possible - Use function pointer instead of simple closure May improve performace, as closures generate more code, and this might unlock some inlining opportunities.
55 lines
1.7 KiB
Rust
55 lines
1.7 KiB
Rust
use anyhow::anyhow;
|
|
use config::lua::get_or_create_module;
|
|
use config::lua::mlua::{self, Lua};
|
|
use smol::prelude::*;
|
|
|
|
pub fn register(lua: &Lua) -> anyhow::Result<()> {
|
|
let wezterm_mod = get_or_create_module(lua, "wezterm")?;
|
|
wezterm_mod.set("read_dir", lua.create_async_function(read_dir)?)?;
|
|
wezterm_mod.set("glob", lua.create_async_function(glob)?)?;
|
|
Ok(())
|
|
}
|
|
|
|
async fn read_dir<'lua>(_: &'lua Lua, path: String) -> mlua::Result<Vec<String>> {
|
|
let mut dir = smol::fs::read_dir(path)
|
|
.await
|
|
.map_err(mlua::Error::external)?;
|
|
let mut entries = vec![];
|
|
while let Some(entry) = dir.next().await {
|
|
let entry = entry.map_err(mlua::Error::external)?;
|
|
if let Some(utf8) = entry.path().to_str() {
|
|
entries.push(utf8.to_string());
|
|
} else {
|
|
return Err(mlua::Error::external(anyhow!(
|
|
"path entry {} is not representable as utf8",
|
|
entry.path().display()
|
|
)));
|
|
}
|
|
}
|
|
Ok(entries)
|
|
}
|
|
|
|
async fn glob<'lua>(
|
|
_: &'lua Lua,
|
|
(pattern, path): (String, Option<String>),
|
|
) -> mlua::Result<Vec<String>> {
|
|
let entries = smol::unblock(move || {
|
|
let mut entries = vec![];
|
|
let glob = filenamegen::Glob::new(&pattern)?;
|
|
for path in glob.walk(path.as_deref().unwrap_or(".")) {
|
|
if let Some(utf8) = path.to_str() {
|
|
entries.push(utf8.to_string());
|
|
} else {
|
|
return Err(anyhow!(
|
|
"path entry {} is not representable as utf8",
|
|
path.display()
|
|
));
|
|
}
|
|
}
|
|
Ok(entries)
|
|
})
|
|
.await
|
|
.map_err(mlua::Error::external)?;
|
|
Ok(entries)
|
|
}
|