nvim_meta/
wrap.rs

1#[macro_export]
2macro_rules! api {
3    {
4        $vis:vis async fn $fn_name:ident(
5            $nvim:ident: $nvim_ty:ty,
6            $($arg:tt: $type:tt),*)
7            -> $result_ty:ident<() $(, $err_ty:tt)?>;
8    } => {
9        $vis async fn $fn_name($nvim: $nvim_ty, $($arg: $type),*) -> $result_ty<$ok_ty $(, $err_ty)?> {
10            use nvim_rs::rpc::unpack::TryUnpack;
11            use nvim_rs::Value;
12
13            $(let $arg = Value::from($arg);)*
14            $nvim.call(stringify!($fn_name), vec![$($arg),*]).await?;
15        }
16    };
17
18    {
19        $vis:vis async fn $fn_name:ident(
20            $nvim:ident: $nvim_ty:ty,
21            $($arg:tt: $type:ty),*)
22            -> $result_ty:ident<$ok_ty:tt $(, $err_ty:tt)?>;
23    } => {
24        $vis async fn $fn_name($nvim: $nvim_ty, $($arg: $type),*) -> $result_ty<$ok_ty $(, $err_ty)?> {
25            use nvim_rs::rpc::unpack::TryUnpack;
26            use nvim_rs::Value;
27
28            $(let $arg = Value::from($arg);)*
29            let res = $nvim.call(stringify!($fn_name), vec![$($arg),*]).await?.expect("Failed to get value");
30            Ok(res.try_unpack().expect("Failed to unpack"))
31        }
32    };
33
34    {
35        $(
36            $vis:vis async fn $fn_name:ident($($stuff:tt)*) -> $result_ty:ident<$ok_ty:tt $(, $err_ty:tt)?>;
37        )*
38    } => {
39        $(wrap_api! { $vis async fn $fn_name($($stuff)*) -> $result_ty<$ok_ty $(, $err_ty)?>; })*
40    };
41}
42
43#[macro_export]
44macro_rules! function {
45    {
46        $vis:vis async fn $fn_name:ident(
47            $nvim:ident: $nvim_ty:ty,
48            $($arg:tt: $type:ty),*)
49            -> $result_ty:ident<() $(, $err_ty:tt)?>;
50    } => {
51        $vis async fn $fn_name($nvim: $nvim_ty, $($arg: $type),*) -> $result_ty<$ok_ty $(, $err_ty)?> {
52            use nvim_rs::rpc::unpack::TryUnpack;
53            use nvim_rs::Value;
54
55            $(let $arg = Value::from($arg);)*
56            $nvim.call_function(stringify!($fn_name), vec![$($arg),*]).await?;
57        }
58    };
59
60    {
61        $vis:vis async fn $fn_name:ident(
62            $nvim:ident: $nvim_ty:ty,
63            $($arg:tt: $type:tt),*)
64            -> $result_ty:ident<$ok_ty:tt $(, $err_ty:tt)?>;
65    } => {
66        $vis async fn $fn_name($nvim: $nvim_ty, $($arg: $type),*) -> $result_ty<$ok_ty $(, $err_ty)?> {
67            use nvim_rs::rpc::unpack::TryUnpack;
68            use nvim_rs::Value;
69
70            $(let $arg = Value::from($arg);)*
71            let res = $nvim.call_function(stringify!($fn_name), vec![$($arg),*]).await?;
72            Ok(res.try_unpack().expect("Failed to unpack"))
73        }
74    };
75
76    {
77        $(
78            $vis:vis async fn $fn_name:ident($($stuff:tt)*) -> $result_ty:ident<$ok_ty:tt $(, $err_ty:tt)?>;
79        )*
80    } => {
81        $(wrap_fn! { $vis async fn $fn_name($($stuff)*) -> $result_ty<$ok_ty $(, $err_ty)?>; })*
82    }
83}