Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support derive #123

Open
yyy33 opened this issue Dec 14, 2023 · 1 comment
Open

support derive #123

yyy33 opened this issue Dec 14, 2023 · 1 comment

Comments

@yyy33
Copy link

yyy33 commented Dec 14, 2023

Add derive macros for some commonly used trails like FromObject

Where set_emap is the function that I expose to the user lua user, wishing to receive the string or table type of the lua

fn set_emap(
    mode: Mode,
    lhs: ,
    rhs: ,
    opts: ,
) -> oxi::Result<()> {
    //TODO
}


#[derive(FromObject)]
enum Mode {
    ByString(String)
    ByTable(Dictionary)
}
@SCJangra
Copy link
Contributor

Yea, derive macros would be great! In the meantime, I use this.

#[macro_export]
macro_rules! lua_interop {
    ($type:ty) => {
        impl nvim_oxi::conversion::FromObject for $type {
            fn from_object(object: nvim_oxi::Object) -> Result<Self, nvim_oxi::conversion::Error> {
                use nvim_oxi::serde::Deserializer;
                use serde::Deserialize;

                Self::deserialize(Deserializer::new(object)).map_err(Into::into)
            }
        }

        impl nvim_oxi::conversion::ToObject for $type {
            fn to_object(self) -> Result<nvim_oxi::Object, nvim_oxi::conversion::Error> {
                use nvim_oxi::serde::Serializer;
                use serde::Serialize;

                self.serialize(Serializer::new()).map_err(Into::into)
            }
        }

        impl nvim_oxi::lua::Poppable for $type {
            unsafe fn pop(
                lstate: *mut nvim_oxi::lua::ffi::lua_State,
            ) -> Result<Self, nvim_oxi::lua::Error> {
                use nvim_oxi::{conversion::FromObject, Object};

                let object = Object::pop(lstate)?;
                Self::from_object(object)
                    .map_err(nvim_oxi::lua::Error::pop_error_from_err::<Self, _>)
            }
        }

        impl nvim_oxi::lua::Pushable for $type {
            unsafe fn push(
                self,
                lstate: *mut nvim_oxi::lua::ffi::lua_State,
            ) -> Result<std::ffi::c_int, nvim_oxi::lua::Error> {
                use nvim_oxi::conversion::ToObject;

                self.to_object()
                    .map_err(nvim_oxi::lua::Error::push_error_from_err::<Self, _>)?
                    .push(lstate)
            }
        }
    };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants