chore(lua): auto callback args macro

This commit is contained in:
əlemi 2024-10-03 03:40:55 +02:00 committed by alemi.dev
parent d5518a7b48
commit 75a0df5e7c

View file

@ -62,55 +62,53 @@ pub(crate) enum LuaCallback {
Invoke(LuaFunction, CallbackArg),
}
macro_rules! callback_args {
($($name:ident : $t:ty ,)*) => {
pub(crate) enum CallbackArg {
Nil,
Str(String),
VecStr(Vec<String>),
Client(CodempClient),
CursorController(CodempCursorController),
BufferController(CodempBufferController),
Workspace(CodempWorkspace),
Event(CodempEvent),
MaybeEvent(Option<CodempEvent>),
Cursor(CodempCursor),
MaybeCursor(Option<CodempCursor>),
TextChange(CodempTextChange),
MaybeTextChange(Option<CodempTextChange>),
$(
$name($t),
)*
}
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),
Self::Nil => Ok(LuaValue::Nil),
$(
Self::$name(x) => x.into_lua(lua),
)*
}
}
}
impl From<()> for CallbackArg { fn from(_: ()) -> Self { CallbackArg::Nil } }
impl From<String> for CallbackArg { fn from(value: String) -> Self { CallbackArg::Str(value) } }
impl From<CodempClient> for CallbackArg { fn from(value: CodempClient) -> Self { CallbackArg::Client(value) } }
impl From<CodempCursorController> for CallbackArg { fn from(value: CodempCursorController) -> Self { CallbackArg::CursorController(value) } }
impl From<CodempBufferController> for CallbackArg { fn from(value: CodempBufferController) -> Self { CallbackArg::BufferController(value) } }
impl From<CodempWorkspace> for CallbackArg { fn from(value: CodempWorkspace) -> Self { CallbackArg::Workspace(value) } }
impl From<Vec<String>> for CallbackArg { fn from(value: Vec<String>) -> Self { CallbackArg::VecStr(value) } }
impl From<CodempEvent> for CallbackArg { fn from(value: CodempEvent) -> Self { CallbackArg::Event(value) } }
impl From<CodempCursor> for CallbackArg { fn from(value: CodempCursor) -> Self { CallbackArg::Cursor(value) } }
impl From<Option<CodempCursor>> for CallbackArg { fn from(value: Option<CodempCursor>) -> Self { CallbackArg::MaybeCursor(value) } }
impl From<CodempTextChange> for CallbackArg { fn from(value: CodempTextChange) -> Self { CallbackArg::TextChange(value) } }
impl From<Option<CodempTextChange>> for CallbackArg { fn from(value: Option<CodempTextChange>) -> Self { CallbackArg::MaybeTextChange(value) } }
impl From<Option<CodempEvent>> for CallbackArg { fn from(value: Option<CodempEvent>) -> Self { CallbackArg::MaybeEvent(value) } }
impl From<()> for CallbackArg {
fn from(_value: ()) -> Self {
Self::Nil
}
}
$(
impl From<$t> for CallbackArg {
fn from(value: $t) -> Self {
Self::$name(value)
}
}
)*
};
}
callback_args! {
Str: String,
VecStr: Vec<String>,
Client: CodempClient,
CursorController: CodempCursorController,
BufferController: CodempBufferController,
Workspace: CodempWorkspace,
Event: CodempEvent,
MaybeEvent: Option<CodempEvent>,
Cursor: CodempCursor,
MaybeCursor: Option<CodempCursor>,
TextChange: CodempTextChange,
MaybeTextChange: Option<CodempTextChange>,
}