From c63a3ec73a97adef1da4bc9ccc623caa037b780a Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 15 Oct 2024 22:20:23 +0200 Subject: [PATCH] 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! --- src/ffi/lua/buffer.rs | 36 ++---------------------------------- src/ffi/lua/client.rs | 22 ++++------------------ src/ffi/lua/cursor.rs | 34 ++-------------------------------- src/ffi/lua/ext/mod.rs | 10 ++++++++-- src/ffi/lua/workspace.rs | 34 +++++----------------------------- 5 files changed, 21 insertions(+), 115 deletions(-) diff --git a/src/ffi/lua/buffer.rs b/src/ffi/lua/buffer.rs index 5bc0813..2392734 100644 --- a/src/ffi/lua/buffer.rs +++ b/src/ffi/lua/buffer.rs @@ -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>(methods: &mut M) { @@ -38,36 +39,3 @@ impl LuaUserData for CodempBufferController { }); } } - -from_lua_serde! { CodempTextChange } -impl LuaUserData for CodempTextChange { - fn add_fields>(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>(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>(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>(methods: &mut M) { - methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| { - Ok(format!("{:?}", this)) - }); - } -} diff --git a/src/ffi/lua/client.rs b/src/ffi/lua/client.rs index 520eaec..cb54eff 100644 --- a/src/ffi/lua/client.rs +++ b/src/ffi/lua/client.rs @@ -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>(fields: &mut F) { - } - fn add_methods>(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>(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)); - } -} diff --git a/src/ffi/lua/cursor.rs b/src/ffi/lua/cursor.rs index 86a3a33..5a7c220 100644 --- a/src/ffi/lua/cursor.rs +++ b/src/ffi/lua/cursor.rs @@ -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>(methods: &mut M) { @@ -29,34 +30,3 @@ impl LuaUserData for CodempCursorController { }); } } - -from_lua_serde! { CodempCursor } -impl LuaUserData for CodempCursor { - fn add_fields>(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>(methods: &mut M) { - methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| { - Ok(format!("{:?}", this)) - }); - } -} - -from_lua_serde! { CodempSelection } -impl LuaUserData for CodempSelection { - fn add_fields>(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>(methods: &mut M) { - methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| { - Ok(format!("{:?}", this)) - }); - } -} diff --git a/src/ffi/lua/ext/mod.rs b/src/ffi/lua/ext/mod.rs index 1cb07c6..a209c0a 100644 --- a/src/ffi/lua/ext/mod.rs +++ b/src/ffi/lua/ext/mod.rs @@ -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 { + lua.to_value(&self) + } + } )* }; } -pub(crate) use from_lua_serde; +pub(crate) use impl_lua_serde; diff --git a/src/ffi/lua/workspace.rs b/src/ffi/lua/workspace.rs index 56769c7..dd80f05 100644 --- a/src/ffi/lua/workspace.rs +++ b/src/ffi/lua/workspace.rs @@ -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>(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>(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>(methods: &mut M) { - methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| { - Ok(format!("{:?}", this)) - }); - } - - fn add_fields>(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()), - }); - } }