yazi/yazi-fs/src/trash/lua.rs
三咲雅 misaki masa 58f1013348
Some checks failed
Cachix / Publish Flake (push) Has been cancelled
Check / clippy (push) Has been cancelled
Check / rustfmt (push) Has been cancelled
Check / stylua (push) Has been cancelled
Draft / build-unix (gcc-aarch64-linux-gnu, ubuntu-latest, aarch64-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (gcc-i686-linux-gnu, ubuntu-latest, i686-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (gcc-riscv64-linux-gnu, ubuntu-latest, riscv64gc-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (gcc-sparc64-linux-gnu, ubuntu-latest, sparc64-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (macos-latest, aarch64-apple-darwin) (push) Has been cancelled
Draft / build-unix (macos-latest, x86_64-apple-darwin) (push) Has been cancelled
Draft / build-unix (ubuntu-latest, x86_64-unknown-linux-gnu) (push) Has been cancelled
Draft / build-windows (windows-latest, aarch64-pc-windows-msvc) (push) Has been cancelled
Draft / build-windows (windows-latest, x86_64-pc-windows-msvc) (push) Has been cancelled
Draft / build-musl (aarch64-unknown-linux-musl) (push) Has been cancelled
Draft / build-musl (x86_64-unknown-linux-musl) (push) Has been cancelled
Draft / build-snap (amd64, ubuntu-latest) (push) Has been cancelled
Draft / build-snap (arm64, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
Draft / snap (push) Has been cancelled
Draft / draft (push) Has been cancelled
Draft / nightly (push) Has been cancelled
feat: trash bin (#4144)
2026-07-23 01:59:39 +08:00

73 lines
2.6 KiB
Rust

use std::io;
use mlua::{ExternalError, ExternalResult, IntoLuaMulti, LuaString, UserData, UserDataMethods, Value};
use tokio::task::spawn_blocking;
use yazi_binding::Error;
use super::{Trash, TrashNode, TrashNodes};
use crate::file::File;
impl UserData for Trash {
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
methods.add_async_function("empty", |lua, ()| async move {
match spawn_blocking(|| Trash::new()?.empty()).await.into_lua_err()? {
Ok(()) => true.into_lua_multi(&lua),
Err(e) => (false, Error::Io(e)).into_lua_multi(&lua),
}
});
methods.add_async_function("list", |lua, node: Option<TrashNode>| async move {
match spawn_blocking(move || Trash::new()?.list(node.as_ref())).await.into_lua_err()? {
Ok(items) => lua.create_sequence_from(items)?.into_lua_multi(&lua),
Err(e) => (Value::Nil, Error::Io(e)).into_lua_multi(&lua),
}
});
methods.add_async_function("entry", |lua, node: TrashNode| async move {
match spawn_blocking(move || Trash::new()?.entry(&node)).await.into_lua_err()? {
Ok(entry) => entry.into_lua_multi(&lua),
Err(e) => (Value::Nil, Error::Io(e)).into_lua_multi(&lua),
}
});
methods.add_async_function("metadata", |lua, (node, follow): (TrashNode, bool)| async move {
match spawn_blocking(move || Trash::new()?.metadata(&node, follow)).await.into_lua_err()? {
Ok(cha) => cha.into_lua_multi(&lua),
Err(e) => (Value::Nil, Error::Io(e)).into_lua_multi(&lua),
}
});
methods.add_async_function(
"revalidate",
|lua, (node, file): (Option<TrashNode>, File)| async move {
match spawn_blocking(move || Trash::new()?.revalidate(node.as_ref(), &file))
.await
.into_lua_err()?
{
Ok(file) => file.into_lua_multi(&lua),
Err(e) => (Value::Nil, Error::Io(e)).into_lua_multi(&lua),
}
},
);
methods.add_async_function("remove", |lua, (kind, node): (LuaString, TrashNode)| async move {
let f: fn(&Trash, &TrashNode) -> io::Result<()> = match &*kind.as_bytes() {
b"file" => Trash::remove_file,
b"dir" => Trash::remove_dir,
_ => Err("Removal type must be 'file' or 'dir'".into_lua_err())?,
};
match spawn_blocking(move || f(&Trash::new()?, &node)).await.into_lua_err()? {
Ok(()) => true.into_lua_multi(&lua),
Err(e) => (false, Error::Io(e)).into_lua_multi(&lua),
}
});
methods.add_async_function("restore", |lua, nodes: TrashNodes| async move {
match spawn_blocking(move || Trash::new()?.restore(nodes)).await.into_lua_err()? {
Ok(()) => true.into_lua_multi(&lua),
Err(e) => (false, Error::Io(e)).into_lua_multi(&lua),
}
});
}
}