From d9b897d19337f961d46539f5b867238e7ba4dced Mon Sep 17 00:00:00 2001 From: jake Date: Sat, 9 Nov 2019 22:53:55 -0800 Subject: [PATCH] pso_packet manual_flag attribute --- psopacket/src/lib.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/psopacket/src/lib.rs b/psopacket/src/lib.rs index 60c7459..48f10c1 100644 --- a/psopacket/src/lib.rs +++ b/psopacket/src/lib.rs @@ -342,6 +342,7 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream { let args = parse_macro_input!(attr as syn::AttributeArgs); let mut cmd = 0; let mut flag = true; + let mut manual_flag = false; for a in args { match &a { NestedMeta::Lit(lit) => { @@ -353,6 +354,10 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream { if let syn::Meta::Path(syn::Path {segments, ..}) = k { match segments[0].ident.to_string().as_str() { "no_flag" => flag = false, + "manual_flag" => { + flag = false; + manual_flag = true; + }, _ => { return syn::Error::new(segments[0].ident.span(), "unknown macro param").to_compile_error().into(); } @@ -368,6 +373,29 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream { Err(err) => return err }; + if manual_flag { + let mut found_flag = false; + for attr in attrs.iter() { + match attr { + AttrType::Array(_, ident, _, _) => { + if ident.to_string() == "flag" { + found_flag = true; + break; + } + }, + AttrType::Value(_, ident, _) => { + if ident.to_string() == "flag" { + found_flag = true; + break; + } + } + } + } + if !found_flag { + return syn::Error::new(pkt_struct.ident.span(), "struct must have flag if manual_flag is set").to_compile_error().into(); + } + } + let struct_def = generate_struct_def(pkt_struct.ident.clone(), &attrs); let psopacket_impl = generate_psopacket_impl(cmd, pkt_struct.ident.clone(), &attrs, flag); let debug_impl = generate_debug_impl(pkt_struct.ident.clone(), &attrs);