use Formatter::debug_struct for packet debug impl #23
@ -205,55 +205,59 @@ fn generate_psopacket_impl(pkt_cmd: u16, name: syn::Ident, attrs: &Vec<AttrType>
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn generate_debug_impl(name: syn::Ident, attrs: &Vec<AttrType>) -> proc_macro2::TokenStream {
 | 
					fn generate_debug_impl(name: syn::Ident, attrs: &Vec<AttrType>) -> proc_macro2::TokenStream {
 | 
				
			||||||
    let mut dbg_write = Vec::new();
 | 
					    let dbg_write = attrs
 | 
				
			||||||
    for attr in attrs {
 | 
					        .iter()
 | 
				
			||||||
        let element = match attr {
 | 
					        .map(|attr| {
 | 
				
			||||||
            AttrType::Value(ty, name, meta) => {
 | 
					            match attr {
 | 
				
			||||||
                let ident_str = name.to_string();
 | 
					                AttrType::Value(ty, name, meta) => {
 | 
				
			||||||
                let type_str = ty.path.segments[0].ident.to_string();
 | 
					                    let ident_str = name.to_string();
 | 
				
			||||||
                match meta {
 | 
					                    let type_str = ty.path.segments[0].ident.to_string();
 | 
				
			||||||
                    AttrMeta::NoDebug => quote! {
 | 
					                    match meta {
 | 
				
			||||||
                        write!(f, "    {} {}: [...]\n", #ident_str, #type_str)?;
 | 
					                        AttrMeta::NoDebug => quote! {
 | 
				
			||||||
                    },
 | 
					                            .field(&format!("{} [{}]", #ident_str, #type_str), &format_args!("[...]"))
 | 
				
			||||||
                    _ => quote! {
 | 
					                        },
 | 
				
			||||||
                        write!(f, "    {} {}: {:?}\n", #ident_str, #type_str, self.#name)?;
 | 
					                        _ => quote! {
 | 
				
			||||||
 | 
					                            .field(&format!("{} [{}]", #ident_str, #type_str), &self.#name)
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                AttrType::Array(ty, name, len, meta) => {
 | 
				
			||||||
 | 
					                    let ident_str = name.to_string();
 | 
				
			||||||
 | 
					                    let type_str = ty.path.segments[0].ident.to_string();
 | 
				
			||||||
 | 
					                    match meta {
 | 
				
			||||||
 | 
					                        AttrMeta::Utf8 => quote! {
 | 
				
			||||||
 | 
					                            .field(&format!("{} [utf8; {}]", #ident_str, #len),
 | 
				
			||||||
 | 
					                                   match std::str::from_utf8(&self.#name) {
 | 
				
			||||||
 | 
					                                       Ok(ref s) => s,
 | 
				
			||||||
 | 
					                                       Err(_) => &self.#name
 | 
				
			||||||
 | 
					                                   })
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        AttrMeta::Utf16 => quote! {
 | 
				
			||||||
 | 
					                            .field(&format!("{} [utf16; {}]", #ident_str, #len),
 | 
				
			||||||
 | 
					                                   match std::str::from_utf16(&self.#name) {
 | 
				
			||||||
 | 
					                                       Ok(ref s) => s,
 | 
				
			||||||
 | 
					                                       Err(_) => &self.#name
 | 
				
			||||||
 | 
					                                   })
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        AttrMeta::NoDebug => quote! {
 | 
				
			||||||
 | 
					                            .field(&format!("{} [{}; {}]", #ident_str, #type_str, #len), &format_args!("[...]"))
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        _ => quote! {
 | 
				
			||||||
 | 
					                            .field(&format!("{} [{}; {}]", #ident_str, #type_str, #len), &format_args!("{:?}", &self.#name))
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            AttrType::Array(ty, name, len, meta) => {
 | 
					 | 
				
			||||||
                let ident_str = name.to_string();
 | 
					 | 
				
			||||||
                let type_str = ty.path.segments[0].ident.to_string();
 | 
					 | 
				
			||||||
                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)?;
 | 
					 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                    _ => quote! {
 | 
					 | 
				
			||||||
                        write!(f, "    {} [{}; {}]: {:?}\n", #ident_str, #type_str, #len, self.#name.to_vec())?;
 | 
					 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        })
 | 
				
			||||||
        dbg_write.push(element);
 | 
					        .collect::<Vec<_>>();
 | 
				
			||||||
    }
 | 
					
 | 
				
			||||||
    let name_str = name.to_string();
 | 
					    let name_str = name.to_string();
 | 
				
			||||||
    quote! {
 | 
					    quote! {
 | 
				
			||||||
        impl std::fmt::Debug for #name {
 | 
					        impl std::fmt::Debug for #name {
 | 
				
			||||||
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
 | 
					            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
 | 
				
			||||||
                write!(f, "{} {{\n", #name_str)?;
 | 
					                f.debug_struct(#name_str)
 | 
				
			||||||
                #(#dbg_write)*
 | 
					                    #(#dbg_write)*
 | 
				
			||||||
                write!(f, "}}")
 | 
					                .finish()
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user