use anyhow::bail; use mlua::{Lua, Table}; use yazi_shared::event::{Cmd, Data}; pub(super) type OptCallback = Box mlua::Result<()> + Send>; #[derive(Default)] pub struct Opt { pub name: String, pub sync: bool, pub args: Vec, pub cb: Option, } impl TryFrom for Opt { type Error = anyhow::Error; fn try_from(mut c: Cmd) -> Result { let Some(name) = c.take_first_str().filter(|s| !s.is_empty()) else { bail!("plugin name cannot be empty"); }; let args = if let Some(s) = c.str("args") { shell_words::split(s)?.into_iter().map(Data::String).collect() } else { c.take_any::>("args").unwrap_or_default() }; Ok(Self { name, sync: c.bool("sync"), args, cb: c.take_any("callback") }) } } impl From for Cmd { fn from(value: Opt) -> Self { let mut cmd = Cmd::args("", vec![value.name]).with_bool("sync", value.sync).with_any("args", value.args); if let Some(cb) = value.cb { cmd = cmd.with_any("callback", cb); } cmd } }