diff --git a/yazi-plugin/src/config/config.rs b/yazi-plugin/src/config/config.rs
index 50dc3b6b..0e1f853d 100644
--- a/yazi-plugin/src/config/config.rs
+++ b/yazi-plugin/src/config/config.rs
@@ -1,4 +1,4 @@
-use mlua::{IntoLua, Lua, LuaSerdeExt, SerializeOptions, Table, Value};
+use mlua::{IntoLua, Lua, LuaSerdeExt, MetaMethod, SerializeOptions, Table, Value};
use yazi_boot::BOOT;
use yazi_config::{MANAGER, PREVIEW, THEME};
@@ -50,7 +50,7 @@ impl<'a> Config<'a> {
})?;
let fetcher = self.lua.create_table()?;
- fetcher.set_metatable(Some(self.lua.create_table_from([("__index", index)])?));
+ fetcher.set_metatable(Some(self.lua.create_table_from([(MetaMethod::Index.name(), index)])?));
self.lua.globals().raw_set("PLUGIN", fetcher)?;
Ok(self)
diff --git a/yazi-plugin/src/elements/elements.rs b/yazi-plugin/src/elements/elements.rs
index 028b10d5..3de6fa76 100644
--- a/yazi-plugin/src/elements/elements.rs
+++ b/yazi-plugin/src/elements/elements.rs
@@ -1,4 +1,4 @@
-use mlua::{AnyUserData, IntoLua, Lua, Table, Value};
+use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, Table, Value};
use tracing::error;
use super::Renderable;
@@ -34,7 +34,7 @@ pub fn compose(lua: &Lua) -> mlua::Result
{
})?;
let ui = lua.create_table_with_capacity(0, 20)?;
- ui.set_metatable(Some(lua.create_table_from([("__index", index)])?));
+ ui.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?));
Ok(ui)
}
diff --git a/yazi-plugin/src/elements/paragraph.rs b/yazi-plugin/src/elements/paragraph.rs
index a4074603..65490a68 100644
--- a/yazi-plugin/src/elements/paragraph.rs
+++ b/yazi-plugin/src/elements/paragraph.rs
@@ -1,6 +1,6 @@
use std::time::Duration;
-use mlua::{FromLua, Lua, MultiValue, Table, Value};
+use mlua::{FromLua, Lua, MetaMethod, MultiValue, Table, Value};
use super::Rect;
use crate::RtRef;
@@ -34,7 +34,7 @@ impl Paragraph {
pub fn compose(lua: &Lua) -> mlua::Result {
let mt = lua.create_table_from([
(
- "__call",
+ MetaMethod::Call.name(),
lua.create_function(|lua, (_, area, lines): (Table, Rect, Value)| {
warn_deprecated(lua.named_registry_value::("rt")?.current());
lua
@@ -45,7 +45,7 @@ impl Paragraph {
})?,
),
(
- "__index",
+ MetaMethod::Index.name(),
lua.create_function(|lua, (_, key): (Table, mlua::String)| {
warn_deprecated(lua.named_registry_value::("rt")?.current());
lua
diff --git a/yazi-plugin/src/error.rs b/yazi-plugin/src/error.rs
index b79d5626..a31a2d1d 100644
--- a/yazi-plugin/src/error.rs
+++ b/yazi-plugin/src/error.rs
@@ -1,4 +1,6 @@
-use mlua::{IntoLua, Lua, MetaMethod, UserData, UserDataFields, UserDataMethods};
+use std::borrow::Cow;
+
+use mlua::{ExternalError, Lua, MetaMethod, UserData, UserDataFields, UserDataMethods, Value};
pub enum Error {
Io(std::io::Error),
@@ -12,6 +14,14 @@ impl Error {
lua.globals().raw_set("Error", lua.create_table_from([("custom", new)])?)
}
+
+ fn to_string(&self) -> Cow {
+ match self {
+ Error::Io(e) => Cow::Owned(e.to_string()),
+ Error::Serde(e) => Cow::Owned(e.to_string()),
+ Error::Custom(s) => Cow::Borrowed(s),
+ }
+ }
}
impl UserData for Error {
@@ -26,11 +36,20 @@ impl UserData for Error {
fn add_methods>(methods: &mut M) {
methods.add_meta_method(MetaMethod::ToString, |lua, me, ()| {
- Ok(match me {
- Error::Io(e) => e.to_string().into_lua(lua),
- Error::Serde(e) => e.to_string().into_lua(lua),
- Error::Custom(s) => lua.create_string(s)?.into_lua(lua),
- })
+ lua.create_string(me.to_string().as_ref())
+ });
+ methods.add_meta_function(MetaMethod::Concat, |lua, (lhs, rhs): (Value, Value)| {
+ match (lhs, rhs) {
+ (Value::String(l), Value::UserData(r)) => {
+ let r = r.borrow::()?;
+ lua.create_string([l.as_bytes().as_ref(), r.to_string().as_bytes()].concat())
+ }
+ (Value::UserData(l), Value::String(r)) => {
+ let l = l.borrow::()?;
+ lua.create_string([l.to_string().as_bytes(), r.as_bytes().as_ref()].concat())
+ }
+ _ => Err("only string can be concatenated with Error".into_lua_err()),
+ }
});
}
}
diff --git a/yazi-plugin/src/fs/fs.rs b/yazi-plugin/src/fs/fs.rs
index 6faa920b..33c9813b 100644
--- a/yazi-plugin/src/fs/fs.rs
+++ b/yazi-plugin/src/fs/fs.rs
@@ -1,5 +1,5 @@
use globset::GlobBuilder;
-use mlua::{ExternalError, ExternalResult, Function, IntoLua, IntoLuaMulti, Lua, Table, Value};
+use mlua::{ExternalError, ExternalResult, Function, IntoLua, IntoLuaMulti, Lua, MetaMethod, Table, Value};
use tokio::fs;
use yazi_shared::fs::remove_dir_clean;
@@ -22,7 +22,7 @@ pub fn compose(lua: &Lua) -> mlua::Result {
})?;
let fs = lua.create_table_with_capacity(0, 10)?;
- fs.set_metatable(Some(lua.create_table_from([("__index", index)])?));
+ fs.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?));
Ok(fs)
}
@@ -53,11 +53,11 @@ fn write(lua: &Lua) -> mlua::Result {
fn remove(lua: &Lua) -> mlua::Result {
lua.create_async_function(|lua, (type_, url): (mlua::String, UrlRef)| async move {
- let result = match &*type_.to_str()? {
- "file" => fs::remove_file(&*url).await,
- "dir" => fs::remove_dir(&*url).await,
- "dir_all" => fs::remove_dir_all(&*url).await,
- "dir_clean" => Ok(remove_dir_clean(&url).await),
+ let result = match type_.as_bytes().as_ref() {
+ b"file" => fs::remove_file(&*url).await,
+ b"dir" => fs::remove_dir(&*url).await,
+ b"dir_all" => fs::remove_dir_all(&*url).await,
+ b"dir_clean" => Ok(remove_dir_clean(&url).await),
_ => Err("Removal type must be 'file', 'dir', 'dir_all', or 'dir_clean'".into_lua_err())?,
};
diff --git a/yazi-plugin/src/loader/require.rs b/yazi-plugin/src/loader/require.rs
index 1f6c1116..8f45bea2 100644
--- a/yazi-plugin/src/loader/require.rs
+++ b/yazi-plugin/src/loader/require.rs
@@ -1,6 +1,6 @@
use std::sync::Arc;
-use mlua::{ExternalResult, Function, IntoLua, Lua, MultiValue, ObjectLike, Table, Value};
+use mlua::{ExternalResult, Function, IntoLua, Lua, MetaMethod, MultiValue, ObjectLike, Table, Value};
use super::LOADER;
use crate::RtRef;
@@ -44,7 +44,7 @@ impl Require {
let id: Arc = Arc::from(id);
let mt = lua.create_table_from([
(
- "__index",
+ MetaMethod::Index.name(),
lua.create_function(move |lua, (ts, key): (Table, mlua::String)| {
match ts.raw_get::("__mod")?.raw_get::(&key)? {
Value::Function(_) => {
@@ -55,7 +55,7 @@ impl Require {
})?,
),
(
- "__newindex",
+ MetaMethod::NewIndex.name(),
lua.create_function(move |_, (ts, key, value): (Table, mlua::String, Value)| {
ts.raw_get::("__mod")?.raw_set(key, value)
})?,
diff --git a/yazi-plugin/src/pubsub/mod.rs b/yazi-plugin/src/pubsub/mod.rs
index b1354290..9b9eb913 100644
--- a/yazi-plugin/src/pubsub/mod.rs
+++ b/yazi-plugin/src/pubsub/mod.rs
@@ -1,6 +1,6 @@
#![allow(clippy::module_inception)]
-use mlua::{IntoLua, Lua, Table, Value};
+use mlua::{IntoLua, Lua, MetaMethod, Table, Value};
yazi_macro::mod_flat!(pubsub);
@@ -22,7 +22,7 @@ pub(super) fn compose(lua: &Lua) -> mlua::Result {
})?;
let ps = lua.create_table_with_capacity(0, 10)?;
- ps.set_metatable(Some(lua.create_table_from([("__index", index)])?));
+ ps.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?));
Ok(ps)
}
diff --git a/yazi-plugin/src/url/url.rs b/yazi-plugin/src/url/url.rs
index 8cc9d994..bff074a3 100644
--- a/yazi-plugin/src/url/url.rs
+++ b/yazi-plugin/src/url/url.rs
@@ -62,9 +62,9 @@ impl Url {
reg.add_meta_method(MetaMethod::ToString, |lua, me, ()| {
lua.create_string(me.as_os_str().as_encoded_bytes())
});
- reg.add_meta_method(MetaMethod::Concat, |lua, me, other: mlua::String| {
- let me = me.as_os_str().as_encoded_bytes();
- let other = other.as_bytes();
+ reg.add_meta_method(MetaMethod::Concat, |lua, lhs, rhs: mlua::String| {
+ let me = lhs.as_os_str().as_encoded_bytes();
+ let other = rhs.as_bytes();
let mut out = Vec::with_capacity(me.len() + other.len());
out.extend_from_slice(me);
diff --git a/yazi-plugin/src/utils/utils.rs b/yazi-plugin/src/utils/utils.rs
index 3e483cab..7812c86c 100644
--- a/yazi-plugin/src/utils/utils.rs
+++ b/yazi-plugin/src/utils/utils.rs
@@ -1,4 +1,4 @@
-use mlua::{IntoLua, Lua, Table, Value};
+use mlua::{IntoLua, Lua, MetaMethod, Table, Value};
pub(super) struct Utils;
@@ -83,7 +83,7 @@ pub fn compose(lua: &Lua, isolate: bool) -> mlua::Result {
})?;
let ya = lua.create_table_with_capacity(0, 40)?;
- ya.set_metatable(Some(lua.create_table_from([("__index", index)])?));
+ ya.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?));
Ok(ya)
}