From ea488cadcae033d66f34f4e836baf140ab4713b7 Mon Sep 17 00:00:00 2001 From: jake Date: Sat, 26 Oct 2019 16:10:56 -0700 Subject: [PATCH] implement nodebug for specific vars --- psopacket/src/lib.rs | 74 +++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/psopacket/src/lib.rs b/psopacket/src/lib.rs index fc050d2..8dd1acc 100644 --- a/psopacket/src/lib.rs +++ b/psopacket/src/lib.rs @@ -11,8 +11,9 @@ use quote::quote; -#[derive(Debug)] +#[derive(Debug, PartialEq)] enum AttrMeta { + None, Utf8, Utf16, NoDebug, @@ -22,8 +23,8 @@ enum AttrMeta { #[derive(Debug)] enum AttrType { - Value(syn::PathSegment, syn::Ident, Option), - Array(syn::PathSegment, syn::Ident, usize, Option), + Value(syn::PathSegment, syn::Ident, AttrMeta), + Array(syn::PathSegment, syn::Ident, usize, AttrMeta) } fn generate_struct_def(name: syn::Ident, attrs: &Vec) -> proc_macro2::TokenStream { @@ -196,40 +197,41 @@ fn generate_debug_impl(name: syn::Ident, attrs: &Vec) -> proc_macro2:: let mut dbg_write = Vec::new(); for attr in attrs { let element = match attr { - AttrType::Value(ty, name, _meta) => { + AttrType::Value(ty, name, meta) => { let ident_str = name.to_string(); let type_str = ty.ident.to_string(); - quote! { - write!(f, " {} {}: {:?}\n", #ident_str, #type_str, self.#name)?; + match meta { + AttrMeta::NoDebug => quote! { + write!(f, " {} {}: [...]\n", #ident_str, #type_str)?; + }, + _ => quote! { + write!(f, " {} {}: {:?}\n", #ident_str, #type_str, self.#name)?; + } } }, AttrType::Array(ty, name, len, meta) => { let ident_str = name.to_string(); let type_str = ty.ident.to_string(); - if let Some(meta) = meta { - match meta { - AttrMeta::Utf8 => { - quote! { - match std::str::from_utf8(&self.#name) { - Ok(v) => write!(f, " {} [utf8; {}]: {:?}\n", #ident_str, #len, v)?, - Err(_) => write!(f, " {} [{}; {}]: {:?}\n", #ident_str, #type_str, #len, self.#name.to_vec())?, - }; - } - }, - AttrMeta::Utf16 => { - quote! { - match String::from_utf16(&self.#name) { - Ok(v) => write!(f, " {} [utf16; {}]: {:?}\n", #ident_str, #len, v)?, - Err(_) => write!(f, " {} [{}; {}]: {:?}\n", #ident_str, #type_str, #len, self.#name.to_vec())?, - }; - } - }, - _ => quote! { write!(f, " {} [{}; {}]: {:?}\n", #ident_str, #type_str, #len, self.#name.to_vec())?; } + match meta { + AttrMeta::Utf8 => quote! { + match std::str::from_utf8(&self.#name) { + Ok(v) => write!(f, " {} [utf8; {}]: {:?}\n", #ident_str, #len, v)?, + Err(_) => write!(f, " {} [{}; {}]: {:?}\n", #ident_str, #type_str, #len, self.#name.to_vec())?, + }; + }, + AttrMeta::Utf16 => quote! { + match String::from_utf16(&self.#name) { + Ok(v) => write!(f, " {} [utf16; {}]: {:?}\n", #ident_str, #len, v)?, + Err(_) => write!(f, " {} [{}; {}]: {:?}\n", #ident_str, #type_str, #len, self.#name.to_vec())?, + }; + }, + AttrMeta::NoDebug => quote! { + write!(f, " {} [{}; {}]: [...]\n", #ident_str, #type_str, #len)?; + }, + AttrMeta::None => quote! { + write!(f, " {} [{}; {}]: {:?}\n", #ident_str, #type_str, #len, self.#name.to_vec())?; } } - else { - quote! { write!(f, " {} [{}; {}]: {:?}\n", #ident_str, #type_str, #len, self.#name.to_vec())?; } - } } }; dbg_write.push(element); @@ -285,9 +287,6 @@ pub fn pso_packet2(attr: TokenStream, item: TokenStream) -> TokenStream { let args = parse_macro_input!(attr as syn::AttributeArgs); let mut cmd = 0; let mut flag = true; - let mut debug = true; - let mut from_bytes = true; - let mut as_bytes = true; for a in args { match &a { NestedMeta::Lit(lit) => { @@ -299,9 +298,6 @@ pub fn pso_packet2(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, - "no_debug" => debug = false, - "no_from" => from_bytes = false, - "no_as" => as_bytes = false, _ => { return syn::Error::new(segments[0].ident.span(), "unknown macro param").to_compile_error().into(); } @@ -319,13 +315,13 @@ pub fn pso_packet2(attr: TokenStream, item: TokenStream) -> TokenStream { if must_be_last { return syn::Error::new(field.ident.as_ref().unwrap().span(), "variables can not follow Vec or String").to_compile_error().into(); } - let mut attr_meta = None; + let mut attr_meta = AttrMeta::None; for attr in &field.attrs { attr_meta = match attr.path.segments[0].ident.to_string().as_str() { - "utf8" => Some(AttrMeta::Utf8), - "utf16" => Some(AttrMeta::Utf16), - "nodebug" => Some(AttrMeta::NoDebug), - _ => None + "utf8" => AttrMeta::Utf8, + "utf16" => AttrMeta::Utf16, + "nodebug" => AttrMeta::NoDebug, + _ => AttrMeta::None } } match &field.ty {