mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-21 23:01:05 +00:00
Some checks are pending
Cachix / Publish Flake (push) Waiting to run
Check / clippy (push) Waiting to run
Check / rustfmt (push) Waiting to run
Check / stylua (push) Waiting to run
Draft / build-unix (gcc-aarch64-linux-gnu, ubuntu-latest, aarch64-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (gcc-i686-linux-gnu, ubuntu-latest, i686-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (gcc-riscv64-linux-gnu, ubuntu-latest, riscv64gc-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (gcc-sparc64-linux-gnu, ubuntu-latest, sparc64-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (macos-latest, aarch64-apple-darwin) (push) Waiting to run
Draft / build-unix (macos-latest, x86_64-apple-darwin) (push) Waiting to run
Draft / build-unix (ubuntu-latest, x86_64-unknown-linux-gnu) (push) Waiting to run
Draft / build-windows (windows-latest, aarch64-pc-windows-msvc) (push) Waiting to run
Draft / build-windows (windows-latest, x86_64-pc-windows-msvc) (push) Waiting to run
Draft / build-musl (aarch64-unknown-linux-musl) (push) Waiting to run
Draft / build-musl (x86_64-unknown-linux-musl) (push) Waiting to run
Draft / build-snap (amd64, ubuntu-latest) (push) Waiting to run
Draft / build-snap (arm64, ubuntu-24.04-arm) (push) Waiting to run
Draft / snap (push) Blocked by required conditions
Draft / draft (push) Blocked by required conditions
Draft / nightly (push) Blocked by required conditions
Test / test (macos-latest) (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
Test / test (windows-latest) (push) Waiting to run
57 lines
1.4 KiB
Rust
57 lines
1.4 KiB
Rust
use mlua::{AnyUserData, ExternalError, FromLua, Lua, Table, UserData, UserDataMethods, UserDataRegistry, Value};
|
|
use yazi_binding::{impl_file_fields, impl_file_methods};
|
|
|
|
use crate::file::{File, FileInventory, FileRef};
|
|
|
|
const EXPECTED: &str = "expected a table, File, or fs::File";
|
|
|
|
impl File {
|
|
pub fn install(lua: &Lua) -> mlua::Result<()> {
|
|
lua.globals().raw_set("File", lua.create_function(|_, file: Self| Ok(file))?)
|
|
}
|
|
}
|
|
|
|
impl TryFrom<Table> for File {
|
|
type Error = mlua::Error;
|
|
|
|
fn try_from(value: Table) -> Result<Self, Self::Error> {
|
|
Ok(Self {
|
|
url: value.raw_get("url")?,
|
|
cha: value.raw_get("cha")?,
|
|
extra: value.try_into()?,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl TryFrom<AnyUserData> for File {
|
|
type Error = mlua::Error;
|
|
|
|
fn try_from(value: AnyUserData) -> Result<Self, Self::Error> {
|
|
match value.take::<Self>() {
|
|
Ok(me) => Ok(me),
|
|
Err(mlua::Error::UserDataTypeMismatch) => FileRef(value).try_into(),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl FromLua for File {
|
|
fn from_lua(value: Value, _: &Lua) -> mlua::Result<Self> {
|
|
match value {
|
|
Value::Table(tbl) => Self::try_from(tbl),
|
|
Value::UserData(ud) => Self::try_from(ud),
|
|
_ => Err(EXPECTED.into_lua_err())?,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl UserData for File {
|
|
fn register(registry: &mut UserDataRegistry<Self>) {
|
|
impl_file_fields!(registry);
|
|
impl_file_methods!(registry);
|
|
|
|
for inv in inventory::iter::<FileInventory>() {
|
|
(inv.register)(registry);
|
|
}
|
|
}
|
|
}
|