Fixed macro lib
This commit is contained in:
parent
b51826c0ff
commit
fb2c45d163
|
|
@ -2,46 +2,38 @@
|
|||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, DeriveInput, Data, Fields, Meta, Lit};
|
||||
use syn::{parse_macro_input, DeriveInput, Data, Fields, Lit};
|
||||
|
||||
#[proc_macro_derive(Packet, attributes(packet))]
|
||||
pub fn derive_packet(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let name = &input.ident;
|
||||
|
||||
let packet_id = input.attrs.iter().find_map(|attr| {
|
||||
if attr.path.is_ident("packet") {
|
||||
if let Ok(Meta::List(list)) = attr.parse_meta() {
|
||||
for nested in list.nested.iter() {
|
||||
if let syn::NestedMeta::Meta(Meta::NameValue(nv)) = nested {
|
||||
if nv.path.is_ident("id") {
|
||||
if let Lit::Int(litint) = &nv.lit {
|
||||
return Some(litint.base10_parse::<i32>().unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}).expect("Missing #[packet(id = …)] attribute");
|
||||
let mut packet_id: Option<i32> = None;
|
||||
let mut enum_variant: Option<String> = None;
|
||||
|
||||
let enum_variant = input.attrs.iter().find_map(|attr| {
|
||||
if attr.path.is_ident("packet") {
|
||||
if let Ok(Meta::List(list)) = attr.parse_meta() {
|
||||
for nested in list.nested.iter() {
|
||||
if let syn::NestedMeta::Meta(Meta::NameValue(nv)) = nested {
|
||||
if nv.path.is_ident("name") {
|
||||
if let Lit::Str(litstr) = &nv.lit {
|
||||
return Some(litstr.value());
|
||||
}
|
||||
}
|
||||
for attr in &input.attrs {
|
||||
if attr.path().is_ident("packet") {
|
||||
let _ = attr.parse_nested_meta(|meta| {
|
||||
if meta.path.is_ident("id") {
|
||||
let lit: Lit = meta.value()?.parse()?;
|
||||
if let Lit::Int(litint) = lit {
|
||||
packet_id = Some(litint.base10_parse::<i32>()?);
|
||||
}
|
||||
}
|
||||
}
|
||||
if meta.path.is_ident("name") {
|
||||
let lit: Lit = meta.value()?.parse()?;
|
||||
if let Lit::Str(litstr) = lit {
|
||||
enum_variant = Some(litstr.value());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
None
|
||||
}).expect("Missing #[packet(name = …)] attribute");
|
||||
}
|
||||
|
||||
let packet_id = packet_id.expect("Missing #[packet(id = …)] attribute");
|
||||
let enum_variant = enum_variant.expect("Missing #[packet(name = …)] attribute");
|
||||
|
||||
let Data::Struct(data_struct) = input.data else {
|
||||
return syn::Error::new_spanned(name, "Packet can only be derived for structs")
|
||||
|
|
@ -55,7 +47,6 @@ pub fn derive_packet(input: TokenStream) -> TokenStream {
|
|||
.into();
|
||||
};
|
||||
|
||||
// build field accessors
|
||||
let read_fields = fields.named.iter().map(|f| {
|
||||
let fname = &f.ident;
|
||||
let fty = &f.ty;
|
||||
|
|
@ -125,7 +116,6 @@ pub fn derive_mctype_struct(input: TokenStream) -> TokenStream {
|
|||
.into();
|
||||
};
|
||||
|
||||
// build field accessors
|
||||
let read_fields = fields.named.iter().map(|f| {
|
||||
let fname = &f.ident;
|
||||
let fty = &f.ty;
|
||||
|
|
|
|||
Loading…
Reference in New Issue