From d750909a9dbe8969213ffc0936b3c2b596e36728 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 3 Jan 2024 01:11:07 +0800 Subject: [PATCH] Switch to `uzers` crate from `users` --- Cargo.lock | 22 ++++++------- yazi-plugin/Cargo.toml | 2 +- yazi-plugin/src/os/os.rs | 0 yazi-plugin/src/utils/mod.rs | 3 +- yazi-plugin/src/utils/unix_user.rs | 52 ------------------------------ yazi-plugin/src/utils/user.rs | 39 ++++++++++++++++++++++ yazi-plugin/src/utils/utils.rs | 5 ++- 7 files changed, 54 insertions(+), 69 deletions(-) create mode 100644 yazi-plugin/src/os/os.rs delete mode 100644 yazi-plugin/src/utils/unix_user.rs create mode 100644 yazi-plugin/src/utils/user.rs diff --git a/Cargo.lock b/Cargo.lock index 4e77318f..908e9afc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2191,22 +2191,22 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "users" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032" -dependencies = [ - "libc", - "log", -] - [[package]] name = "utf8parse" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "uzers" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76d283dc7e8c901e79e32d077866eaf599156cbf427fffa8289aecc52c5c3f63" +dependencies = [ + "libc", + "log", +] + [[package]] name = "validator" version = "0.16.1" @@ -2736,7 +2736,7 @@ dependencies = [ "tokio-util", "tracing", "unicode-width", - "users", + "uzers", "yazi-adaptor", "yazi-config", "yazi-prebuild", diff --git a/yazi-plugin/Cargo.toml b/yazi-plugin/Cargo.toml index f25c8c2c..3cb58b5b 100644 --- a/yazi-plugin/Cargo.toml +++ b/yazi-plugin/Cargo.toml @@ -30,5 +30,5 @@ tokio = { version = "^1", features = [ "parking_lot", "rt-multi-thread" tokio-util = "^0" tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] } unicode-width = "^0" +uzers = "^0" yazi-prebuild = "0.1.2" -users = "0.11" \ No newline at end of file diff --git a/yazi-plugin/src/os/os.rs b/yazi-plugin/src/os/os.rs new file mode 100644 index 00000000..e69de29b diff --git a/yazi-plugin/src/utils/mod.rs b/yazi-plugin/src/utils/mod.rs index faa02f0b..e4b94e6a 100644 --- a/yazi-plugin/src/utils/mod.rs +++ b/yazi-plugin/src/utils/mod.rs @@ -9,8 +9,7 @@ mod preview; mod target; mod text; mod time; -#[cfg(unix)] -mod unix_user; +mod user; mod utils; pub use preview::*; diff --git a/yazi-plugin/src/utils/unix_user.rs b/yazi-plugin/src/utils/unix_user.rs deleted file mode 100644 index 83f0a179..00000000 --- a/yazi-plugin/src/utils/unix_user.rs +++ /dev/null @@ -1,52 +0,0 @@ -use std::sync::Arc; - -use mlua::{Lua, Table}; -use users::{Groups, Users, UsersCache}; - -use super::Utils; - -impl Utils { - pub(super) fn unix_user(lua: &Lua, ya: &Table) -> mlua::Result<()> { - let arc_cache = Arc::new(UsersCache::new()); - - { - let cache = Arc::clone(&arc_cache); - ya.set("get_current_uid", lua.create_function(move |_, ()| Ok(cache.get_current_uid()))?)?; - } - - { - let cache = Arc::clone(&arc_cache); - ya.set("get_current_gid", lua.create_function(move |_, ()| Ok(cache.get_current_gid()))?)?; - } - - { - let cache = Arc::clone(&arc_cache); - ya.set( - "get_user_name_by_uid", - lua.create_function(move |_, uid: u32| { - Ok( - cache - .get_user_by_uid(uid) - .and_then(|u| u.name().to_str().and_then(|n| Some(n.to_owned()))), - ) - })?, - )?; - } - - { - let cache = Arc::clone(&arc_cache); - ya.set( - "get_group_name_by_gid", - lua.create_function(move |_, gid: u32| { - Ok( - cache - .get_group_by_gid(gid) - .and_then(|g| g.name().to_str().and_then(|n| Some(n.to_owned()))), - ) - })?, - )?; - } - - Ok(()) - } -} diff --git a/yazi-plugin/src/utils/user.rs b/yazi-plugin/src/utils/user.rs new file mode 100644 index 00000000..0a49ad37 --- /dev/null +++ b/yazi-plugin/src/utils/user.rs @@ -0,0 +1,39 @@ +use mlua::{Lua, Table}; +use uzers::{Groups, Users, UsersCache}; +use yazi_shared::RoCell; + +use super::Utils; + +static CACHE: RoCell = RoCell::new(); + +impl Utils { + pub(super) fn user(lua: &Lua, ya: &Table) -> mlua::Result<()> { + CACHE.with(Default::default); + + ya.set("uid", lua.create_function(|_, ()| Ok(CACHE.get_current_uid()))?)?; + + ya.set("gid", lua.create_function(|_, ()| Ok(CACHE.get_current_gid()))?)?; + + ya.set( + "user_name", + lua.create_function(|lua, uid: Option| { + CACHE + .get_user_by_uid(uid.unwrap_or_else(|| CACHE.get_current_uid())) + .map(|s| lua.create_string(s.name().as_encoded_bytes())) + .transpose() + })?, + )?; + + ya.set( + "group_name", + lua.create_function(|lua, gid: Option| { + CACHE + .get_group_by_gid(gid.unwrap_or_else(|| CACHE.get_current_gid())) + .map(|s| lua.create_string(s.name().as_encoded_bytes())) + .transpose() + })?, + )?; + + Ok(()) + } +} diff --git a/yazi-plugin/src/utils/utils.rs b/yazi-plugin/src/utils/utils.rs index c2d56639..a5a060d8 100644 --- a/yazi-plugin/src/utils/utils.rs +++ b/yazi-plugin/src/utils/utils.rs @@ -12,10 +12,9 @@ pub fn install(lua: &Lua) -> mlua::Result<()> { Utils::plugin(lua, &ya)?; Utils::preview(lua, &ya)?; Utils::target(lua, &ya)?; - Utils::time(lua, &ya)?; Utils::text(lua, &ya)?; - #[cfg(unix)] - Utils::unix_user(lua, &ya)?; + Utils::time(lua, &ya)?; + Utils::user(lua, &ya)?; lua.globals().set("ya", ya) }