yazi/yazi-dds/src/body/custom.rs
2025-07-18 07:48:00 +08:00

36 lines
969 B
Rust

use mlua::{IntoLua, Lua, Value};
use serde::Serialize;
use yazi_shared::event::Data;
use super::Body;
use crate::Sendable;
#[derive(Debug)]
pub struct BodyCustom {
pub kind: String,
pub data: Data,
}
impl BodyCustom {
pub fn from_str(kind: &str, data: &str) -> anyhow::Result<Body<'static>> {
Ok(Self { kind: kind.to_owned(), data: serde_json::from_str(data)? }.into())
}
pub fn from_lua(lua: &Lua, kind: &str, data: Value) -> mlua::Result<Body<'static>> {
Ok(Self { kind: kind.to_owned(), data: Sendable::value_to_data(lua, data)? }.into())
}
}
impl From<BodyCustom> for Body<'_> {
fn from(value: BodyCustom) -> Self { Self::Custom(value) }
}
impl IntoLua for BodyCustom {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> { Sendable::data_to_value(lua, self.data) }
}
impl Serialize for BodyCustom {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serde::Serialize::serialize(&self.data, serializer)
}
}