feat(lua): use serde for IntoLua too

so now it creates a table on the fly and the thing that lua receives
behaves like you would expect instead of being weird!
This commit is contained in:
əlemi 2024-10-15 22:20:23 +02:00
parent a721e17024
commit c63a3ec73a
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 21 additions and 115 deletions

View file

@ -3,7 +3,8 @@ use mlua::prelude::*;
use mlua_codemp_patch as mlua;
use super::ext::a_sync::a_sync;
use super::ext::from_lua_serde;
super::ext::impl_lua_serde! { CodempTextChange CodempBufferUpdate }
impl LuaUserData for CodempBufferController {
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
@ -38,36 +39,3 @@ impl LuaUserData for CodempBufferController {
});
}
}
from_lua_serde! { CodempTextChange }
impl LuaUserData for CodempTextChange {
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("content", |_, this| Ok(this.content.clone()));
fields.add_field_method_get("start", |_, this| Ok(this.start));
fields.add_field_method_get("end", |_, this| Ok(this.end));
// add a 'finish' accessor too because in Lua 'end' is reserved
fields.add_field_method_get("finish", |_, this| Ok(this.end));
}
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| {
Ok(format!("{:?}", this))
});
methods.add_method("apply", |_, this, (txt,): (String,)| Ok(this.apply(&txt)));
}
}
from_lua_serde! { CodempBufferUpdate }
impl LuaUserData for CodempBufferUpdate {
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("hash", |_, this| Ok(this.hash));
fields.add_field_method_get("version", |_, this| Ok(this.version.clone()));
fields.add_field_method_get("change", |_, this| Ok(this.change.clone()));
}
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| {
Ok(format!("{:?}", this))
});
}
}

View file

@ -3,20 +3,17 @@ use mlua::prelude::*;
use mlua_codemp_patch as mlua;
use super::ext::a_sync::a_sync;
use super::ext::from_lua_serde;
super::ext::impl_lua_serde! { CodempConfig CodempUser }
impl LuaUserData for CodempClient {
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
}
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| {
Ok(format!("{:?}", this))
});
fields.add_method("user", |_, this| Ok(this.user().id.to_string()));
fields.add_method("username", |_, this| Ok(this.my_user().name.clone()));
fields.add_method("active_workspaces", |_, this| Ok(this.active_workspaces()));
methods.add_method("current_user", |_, this, ()| Ok(this.current_user().clone()));
methods.add_method("active_workspaces", |_, this, ()| Ok(this.active_workspaces()));
methods.add_method(
"refresh",
@ -55,14 +52,3 @@ impl LuaUserData for CodempClient {
});
}
}
from_lua_serde! { CodempConfig }
impl LuaUserData for CodempConfig {
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("username", |_, this| Ok(this.username.clone()));
fields.add_field_method_get("password", |_, this| Ok(this.password.clone()));
fields.add_field_method_get("host", |_, this| Ok(this.host.clone()));
fields.add_field_method_get("port", |_, this| Ok(this.port));
fields.add_field_method_get("tls", |_, this| Ok(this.tls));
}
}

View file

@ -3,7 +3,8 @@ use mlua::prelude::*;
use mlua_codemp_patch as mlua;
use super::ext::a_sync::a_sync;
use super::ext::from_lua_serde;
super::ext::impl_lua_serde! { CodempCursor CodempSelection }
impl LuaUserData for CodempCursorController {
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
@ -29,34 +30,3 @@ impl LuaUserData for CodempCursorController {
});
}
}
from_lua_serde! { CodempCursor }
impl LuaUserData for CodempCursor {
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("user", |_, this| Ok(this.user.clone()));
fields.add_field_method_get("sel", |_, this| Ok(this.sel.clone()));
}
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| {
Ok(format!("{:?}", this))
});
}
}
from_lua_serde! { CodempSelection }
impl LuaUserData for CodempSelection {
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("buffer", |_, this| Ok(this.buffer.clone()));
fields.add_field_method_get("start_row", |_, this| Ok(this.start_row));
fields.add_field_method_get("start_col", |_, this| Ok(this.start_col));
fields.add_field_method_get("end_row", |_, this| Ok(this.end_row));
fields.add_field_method_get("end_col", |_, this| Ok(this.end_col));
}
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| {
Ok(format!("{:?}", this))
});
}
}

View file

@ -5,7 +5,7 @@ pub mod log;
pub(crate) use a_sync::tokio;
pub(crate) use callback::callback;
macro_rules! from_lua_serde {
macro_rules! impl_lua_serde {
($($t:ty)*) => {
$(
impl FromLua for $t {
@ -13,8 +13,14 @@ macro_rules! from_lua_serde {
lua.from_value(value)
}
}
impl IntoLua for $t {
fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
lua.to_value(&self)
}
}
)*
};
}
pub(crate) use from_lua_serde;
pub(crate) use impl_lua_serde;

View file

@ -4,6 +4,8 @@ use mlua_codemp_patch as mlua;
use super::ext::a_sync::a_sync;
super::ext::impl_lua_serde! { CodempEvent }
impl LuaUserData for CodempWorkspace {
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| {
@ -48,9 +50,9 @@ impl LuaUserData for CodempWorkspace {
},
);
fields.add_method("id", |_, this, ()| Ok(this.id()));
fields.add_method("cursor", |_, this, ()| Ok(this.cursor()));
fields.add_method("active_buffers", |_, this, ()| Ok(this.active_buffers()));
methods.add_method("id", |_, this, ()| Ok(this.id()));
methods.add_method("cursor", |_, this, ()| Ok(this.cursor()));
methods.add_method("active_buffers", |_, this, ()| Ok(this.active_buffers()));
methods.add_method("user_list", |_, this, ()| Ok(this.user_list()));
methods.add_method("recv", |_, this, ()| a_sync! { this => this.recv().await? });
@ -70,30 +72,4 @@ impl LuaUserData for CodempWorkspace {
methods.add_method("clear_callback", |_, this, ()| Ok(this.clear_callback()));
}
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
// fields.add_field_method_get("users", |_, this| Ok(this.0.users())); // TODO
}
}
from_lua_serde! { CodempEvent }
impl LuaUserData for CodempEvent {
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| {
Ok(format!("{:?}", this))
});
}
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("type", |_, this| match this {
CodempEvent::FileTreeUpdated(_) => Ok("filetree"),
CodempEvent::UserJoin(_) => Ok("join"),
CodempEvent::UserLeave(_) => Ok("leave"),
});
fields.add_field_method_get("value", |_, this| match this {
CodempEvent::FileTreeUpdated(x)
| CodempEvent::UserJoin(x)
| CodempEvent::UserLeave(x) => Ok(x.clone()),
});
}
}