mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-22 07:14:50 +01:00
chore(lua): auto callback args macro
This commit is contained in:
parent
d5518a7b48
commit
75a0df5e7c
1 changed files with 47 additions and 49 deletions
|
@ -62,55 +62,53 @@ pub(crate) enum LuaCallback {
|
||||||
Invoke(LuaFunction, CallbackArg),
|
Invoke(LuaFunction, CallbackArg),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) enum CallbackArg {
|
macro_rules! callback_args {
|
||||||
Nil,
|
($($name:ident : $t:ty ,)*) => {
|
||||||
Str(String),
|
pub(crate) enum CallbackArg {
|
||||||
VecStr(Vec<String>),
|
Nil,
|
||||||
Client(CodempClient),
|
$(
|
||||||
CursorController(CodempCursorController),
|
$name($t),
|
||||||
BufferController(CodempBufferController),
|
)*
|
||||||
Workspace(CodempWorkspace),
|
|
||||||
Event(CodempEvent),
|
|
||||||
MaybeEvent(Option<CodempEvent>),
|
|
||||||
Cursor(CodempCursor),
|
|
||||||
MaybeCursor(Option<CodempCursor>),
|
|
||||||
TextChange(CodempTextChange),
|
|
||||||
MaybeTextChange(Option<CodempTextChange>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoLua for CallbackArg {
|
|
||||||
// TODO this basically calls .into_lua() on all enum variants
|
|
||||||
// i wish i could do this with a Box<dyn IntoLua> or an impl IntoLua
|
|
||||||
// but IntoLua requires Sized so it can't be made into an object
|
|
||||||
fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
|
|
||||||
match self {
|
|
||||||
CallbackArg::Nil => Ok(LuaValue::Nil),
|
|
||||||
CallbackArg::Str(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::Client(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::CursorController(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::BufferController(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::Workspace(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::VecStr(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::Event(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::MaybeEvent(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::Cursor(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::MaybeCursor(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::TextChange(x) => x.into_lua(lua),
|
|
||||||
CallbackArg::MaybeTextChange(x) => x.into_lua(lua),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
impl IntoLua for CallbackArg {
|
||||||
|
fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
|
||||||
|
match self {
|
||||||
|
Self::Nil => Ok(LuaValue::Nil),
|
||||||
|
$(
|
||||||
|
Self::$name(x) => x.into_lua(lua),
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<()> for CallbackArg {
|
||||||
|
fn from(_value: ()) -> Self {
|
||||||
|
Self::Nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$(
|
||||||
|
impl From<$t> for CallbackArg {
|
||||||
|
fn from(value: $t) -> Self {
|
||||||
|
Self::$name(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<()> for CallbackArg { fn from(_: ()) -> Self { CallbackArg::Nil } }
|
callback_args! {
|
||||||
impl From<String> for CallbackArg { fn from(value: String) -> Self { CallbackArg::Str(value) } }
|
Str: String,
|
||||||
impl From<CodempClient> for CallbackArg { fn from(value: CodempClient) -> Self { CallbackArg::Client(value) } }
|
VecStr: Vec<String>,
|
||||||
impl From<CodempCursorController> for CallbackArg { fn from(value: CodempCursorController) -> Self { CallbackArg::CursorController(value) } }
|
Client: CodempClient,
|
||||||
impl From<CodempBufferController> for CallbackArg { fn from(value: CodempBufferController) -> Self { CallbackArg::BufferController(value) } }
|
CursorController: CodempCursorController,
|
||||||
impl From<CodempWorkspace> for CallbackArg { fn from(value: CodempWorkspace) -> Self { CallbackArg::Workspace(value) } }
|
BufferController: CodempBufferController,
|
||||||
impl From<Vec<String>> for CallbackArg { fn from(value: Vec<String>) -> Self { CallbackArg::VecStr(value) } }
|
Workspace: CodempWorkspace,
|
||||||
impl From<CodempEvent> for CallbackArg { fn from(value: CodempEvent) -> Self { CallbackArg::Event(value) } }
|
Event: CodempEvent,
|
||||||
impl From<CodempCursor> for CallbackArg { fn from(value: CodempCursor) -> Self { CallbackArg::Cursor(value) } }
|
MaybeEvent: Option<CodempEvent>,
|
||||||
impl From<Option<CodempCursor>> for CallbackArg { fn from(value: Option<CodempCursor>) -> Self { CallbackArg::MaybeCursor(value) } }
|
Cursor: CodempCursor,
|
||||||
impl From<CodempTextChange> for CallbackArg { fn from(value: CodempTextChange) -> Self { CallbackArg::TextChange(value) } }
|
MaybeCursor: Option<CodempCursor>,
|
||||||
impl From<Option<CodempTextChange>> for CallbackArg { fn from(value: Option<CodempTextChange>) -> Self { CallbackArg::MaybeTextChange(value) } }
|
TextChange: CodempTextChange,
|
||||||
impl From<Option<CodempEvent>> for CallbackArg { fn from(value: Option<CodempEvent>) -> Self { CallbackArg::MaybeEvent(value) } }
|
MaybeTextChange: Option<CodempTextChange>,
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue