Browse Source

implement nodebug for specific vars

pull/3/head
jake 5 years ago
parent
commit
ea488cadca
  1. 74
      psopacket/src/lib.rs

74
psopacket/src/lib.rs

@ -11,8 +11,9 @@ use quote::quote;
#[derive(Debug)]
#[derive(Debug, PartialEq)]
enum AttrMeta { enum AttrMeta {
None,
Utf8, Utf8,
Utf16, Utf16,
NoDebug, NoDebug,
@ -22,8 +23,8 @@ enum AttrMeta {
#[derive(Debug)] #[derive(Debug)]
enum AttrType { enum AttrType {
Value(syn::PathSegment, syn::Ident, Option<AttrMeta>),
Array(syn::PathSegment, syn::Ident, usize, Option<AttrMeta>),
Value(syn::PathSegment, syn::Ident, AttrMeta),
Array(syn::PathSegment, syn::Ident, usize, AttrMeta)
} }
fn generate_struct_def(name: syn::Ident, attrs: &Vec<AttrType>) -> proc_macro2::TokenStream { fn generate_struct_def(name: syn::Ident, attrs: &Vec<AttrType>) -> proc_macro2::TokenStream {
@ -196,40 +197,41 @@ fn generate_debug_impl(name: syn::Ident, attrs: &Vec<AttrType>) -> proc_macro2::
let mut dbg_write = Vec::new(); let mut dbg_write = Vec::new();
for attr in attrs { for attr in attrs {
let element = match attr { let element = match attr {
AttrType::Value(ty, name, _meta) => {
AttrType::Value(ty, name, meta) => {
let ident_str = name.to_string(); let ident_str = name.to_string();
let type_str = ty.ident.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) => { AttrType::Array(ty, name, len, meta) => {
let ident_str = name.to_string(); let ident_str = name.to_string();
let type_str = ty.ident.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); 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 args = parse_macro_input!(attr as syn::AttributeArgs);
let mut cmd = 0; let mut cmd = 0;
let mut flag = true; let mut flag = true;
let mut debug = true;
let mut from_bytes = true;
let mut as_bytes = true;
for a in args { for a in args {
match &a { match &a {
NestedMeta::Lit(lit) => { 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 { if let syn::Meta::Path(syn::Path {segments, ..}) = k {
match segments[0].ident.to_string().as_str() { match segments[0].ident.to_string().as_str() {
"no_flag" => flag = false, "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(); 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 { 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(); 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 { for attr in &field.attrs {
attr_meta = match attr.path.segments[0].ident.to_string().as_str() { 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 { match &field.ty {

Loading…
Cancel
Save