From 7a99ea4409cd9fc02a7ec92e647f7ccc7f343c01 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 1 Apr 2024 00:11:07 +0800 Subject: [PATCH] fix: wrong `state` pointed to and ignore plugin/flavor directory creation errors --- yazi-boot/src/boot.rs | 4 ++-- yazi-config/src/preview/preview.rs | 4 ++-- yazi-dds/src/pubsub.rs | 2 +- yazi-plugin/src/loader/require.rs | 33 ++++++++++++++---------------- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/yazi-boot/src/boot.rs b/yazi-boot/src/boot.rs index 450e461d..9005d493 100644 --- a/yazi-boot/src/boot.rs +++ b/yazi-boot/src/boot.rs @@ -157,8 +157,8 @@ impl Default for Boot { state_dir: Xdg::state_dir(), }; - std::fs::create_dir_all(&boot.flavor_dir).expect("Failed to create flavor directory"); - std::fs::create_dir_all(&boot.plugin_dir).expect("Failed to create plugin directory"); + std::fs::create_dir_all(&boot.flavor_dir).ok(); + std::fs::create_dir_all(&boot.plugin_dir).ok(); boot } } diff --git a/yazi-config/src/preview/preview.rs b/yazi-config/src/preview/preview.rs index b70138ce..690ccb58 100644 --- a/yazi-config/src/preview/preview.rs +++ b/yazi-config/src/preview/preview.rs @@ -1,4 +1,4 @@ -use std::{fs, path::PathBuf, time::{self, SystemTime}}; +use std::{path::PathBuf, time::{self, SystemTime}}; use serde::{Deserialize, Serialize}; use validator::Validate; @@ -53,7 +53,7 @@ impl Default for Preview { preview.cache_dir.filter(|p| !p.is_empty()).map_or_else(Xdg::cache_dir, expand_path); if !cache_dir.is_dir() { - fs::create_dir(&cache_dir).unwrap(); + std::fs::create_dir(&cache_dir).expect("Failed to create cache directory"); } Preview { diff --git a/yazi-dds/src/pubsub.rs b/yazi-dds/src/pubsub.rs index 19535bcc..f4d33f95 100644 --- a/yazi-dds/src/pubsub.rs +++ b/yazi-dds/src/pubsub.rs @@ -5,7 +5,7 @@ use parking_lot::RwLock; use yazi_boot::BOOT; use yazi_shared::{fs::Url, RoCell}; -use crate::{body::{Body, BodyCd, BodyHi, BodyHover, BodyRename, BodyTabs, BodyYank}, Client, Payload, ID, PEERS}; +use crate::{body::{Body, BodyCd, BodyHi, BodyHover, BodyRename, BodyTabs, BodyYank}, Client, ID, PEERS}; pub static LOCAL: RoCell>>>> = RoCell::new(); diff --git a/yazi-plugin/src/loader/require.rs b/yazi-plugin/src/loader/require.rs index bb34108a..4c26f122 100644 --- a/yazi-plugin/src/loader/require.rs +++ b/yazi-plugin/src/loader/require.rs @@ -13,11 +13,11 @@ impl Require { "require", lua.create_function(move |lua, name: mlua::String| { lua.named_registry_value::("rt")?.swap(name.to_str()?); - let module: Table = require.call(&name)?; + let mod_: Table = require.call(&name)?; lua.named_registry_value::("rt")?.reset(); - module.raw_set("_name", &name)?; - Self::create_mt(lua, name, module) + mod_.raw_set("_name", &name)?; + Self::create_mt(lua, name, mod_) })?, )?; @@ -27,38 +27,35 @@ impl Require { fn create_mt( lua: &'static Lua, name: mlua::String<'static>, - module: Table<'static>, + mod_: Table<'static>, ) -> mlua::Result> { let ts = - lua.create_table_from([("name", name.into_lua(lua)?), ("module", module.into_lua(lua)?)])?; + lua.create_table_from([("name", name.into_lua(lua)?), ("mod", mod_.into_lua(lua)?)])?; - let mt = lua.create_table()?; - mt.raw_set( + let mt = lua.create_table_from([( "__index", - lua.create_function(|_, (ts, key): (Table, mlua::String)| { + lua.create_function(|_, (_, key): (Table, mlua::String)| { if key.to_str()? == "setup" { - Ok(RequireSetup { name: ts.raw_get("name")?, module: ts.raw_get("module")? }) + Ok(RequireSetup) } else { - Err("Only `require():setup()` and `require().setup()` are supported").into_lua_err() + Err("Only `require():setup()` is supported").into_lua_err() } })?, - )?; + )])?; ts.set_metatable(Some(mt)); Ok(ts) } } -pub(super) struct RequireSetup { - name: mlua::String<'static>, - module: Table<'static>, -} +pub(super) struct RequireSetup; impl UserData for RequireSetup { fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) { - methods.add_meta_method(MetaMethod::Call, |lua, me, args: Variadic| { - lua.named_registry_value::("rt")?.swap(me.name.to_str()?); - let result = me.module.call_function::<_, Variadic>("setup", args); + methods.add_meta_method(MetaMethod::Call, |lua, _, (ts, args): (Table, Variadic)| { + let (name, mod_): (mlua::String, Table) = (ts.raw_get("name")?, ts.raw_get("mod")?); + lua.named_registry_value::("rt")?.swap(name.to_str()?); + let result = mod_.call_method::<_, Variadic>("setup", args); lua.named_registry_value::("rt")?.reset(); result });