Get JSON LSP running, still work to be done
This commit is contained in:
parent
38d7321511
commit
fbaff615a3
7 changed files with 269 additions and 88 deletions
|
@ -2,7 +2,7 @@ use core::panic;
|
|||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{parse_macro_input, ItemFn, Visibility};
|
||||
use syn::{parse_macro_input, FnArg, ItemFn, Type, Visibility};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
|
@ -21,7 +21,17 @@ pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
|
|||
|
||||
let variadic = inner_fn.sig.inputs.len();
|
||||
let i = (0..variadic).map(syn::Index::from);
|
||||
let t = (0..variadic).map(|_| quote! { _ });
|
||||
let t: Vec<Type> = inner_fn
|
||||
.sig
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|x| match x {
|
||||
FnArg::Receiver(_) => {
|
||||
panic!("all arguments must have specified types, no `self` allowed")
|
||||
}
|
||||
FnArg::Typed(item) => *item.ty.clone(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
// this is cursed...
|
||||
let (args, ty) = if variadic != 1 {
|
||||
|
@ -34,7 +44,8 @@ pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
|
|||
},
|
||||
)
|
||||
} else {
|
||||
(quote! { data }, quote! { _ })
|
||||
let ty = &t[0];
|
||||
(quote! { data }, quote! { #ty })
|
||||
};
|
||||
|
||||
TokenStream::from(quote! {
|
||||
|
@ -48,7 +59,14 @@ pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
|
|||
let data = unsafe { buffer.to_vec() };
|
||||
|
||||
// operation
|
||||
let data: #ty = ::plugin::bincode::deserialize(&data).unwrap();
|
||||
let data: #ty = match ::plugin::bincode::deserialize(&data) {
|
||||
Ok(d) => d,
|
||||
Err(e) => {
|
||||
println!("data: {:?}", data);
|
||||
println!("error: {}", e);
|
||||
panic!("Data passed to function not deserializable.")
|
||||
},
|
||||
};
|
||||
let result = #inner_fn_name(#args);
|
||||
let new_data: Result<Vec<u8>, _> = ::plugin::bincode::serialize(&result);
|
||||
let new_data = new_data.unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue