From 2996b176a7f834325ca2db824bd2a31f09ad4cfd Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 2 Aug 2022 21:31:29 -0600 Subject: [PATCH 1/2] use Formatter::debug_struct for packet debug impl --- psopacket/src/lib.rs | 88 +++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/psopacket/src/lib.rs b/psopacket/src/lib.rs index fc26bcb..bbe96ca 100644 --- a/psopacket/src/lib.rs +++ b/psopacket/src/lib.rs @@ -205,55 +205,59 @@ fn generate_psopacket_impl(pkt_cmd: u16, name: syn::Ident, attrs: &Vec } fn generate_debug_impl(name: syn::Ident, attrs: &Vec) -> proc_macro2::TokenStream { - let mut dbg_write = Vec::new(); - for attr in attrs { - let element = match attr { - AttrType::Value(ty, name, meta) => { - let ident_str = name.to_string(); - let type_str = ty.path.segments[0].ident.to_string(); - match meta { - AttrMeta::NoDebug => quote! { - write!(f, " {} {}: [...]\n", #ident_str, #type_str)?; - }, - _ => quote! { - write!(f, " {} {}: {:?}\n", #ident_str, #type_str, self.#name)?; + let dbg_write = attrs + .iter() + .map(|attr| { + match attr { + AttrType::Value(ty, name, meta) => { + let ident_str = name.to_string(); + let type_str = ty.path.segments[0].ident.to_string(); + match meta { + AttrMeta::NoDebug => quote! { + .field(&format!("{} [{}]", #ident_str, #type_str), &format_args!("[...]")) + }, + _ => 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::>(); + let name_str = name.to_string(); quote! { impl std::fmt::Debug for #name { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{} {{\n", #name_str)?; - #(#dbg_write)* - write!(f, "}}") + f.debug_struct(#name_str) + #(#dbg_write)* + .finish() } } } -- 2.36.0 From a1424332e79bf429d74c0c6931ed600c5d979c9d Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 2 Aug 2022 21:46:04 -0600 Subject: [PATCH 2/2] why wasn't this derived originally --- src/character/character.rs | 16 +--------------- src/packet/login.rs | 12 ------------ 2 files changed, 1 insertion(+), 27 deletions(-) diff --git a/src/character/character.rs b/src/character/character.rs index 1b0f88b..60b6c61 100644 --- a/src/character/character.rs +++ b/src/character/character.rs @@ -138,7 +138,7 @@ impl std::default::Default for Character { } } -#[derive(Copy, Clone, Debug, PartialEq, Default)] +#[derive(Copy, Clone, PSOPacketData, Default)] #[repr(C)] pub struct SelectScreenCharacter { pub exp: u32, @@ -169,20 +169,6 @@ pub struct SelectScreenCharacter { } impl SelectScreenCharacter { - pub const SIZE: usize = 0x7C; - - pub fn from_le_bytes(bytes: [u8; 0x7C]) -> Result { - unsafe { - Ok(std::mem::transmute(bytes)) - } - } - - pub fn to_le_bytes(&self) -> [u8; 0x7C] { - unsafe { - std::mem::transmute(*self) - } - } - pub fn as_character(&self) -> Character { Character { exp: self.exp, diff --git a/src/packet/login.rs b/src/packet/login.rs index f8404ad..b43a47c 100644 --- a/src/packet/login.rs +++ b/src/packet/login.rs @@ -292,18 +292,6 @@ pub struct CharAck { pub code: u32, // TODO: enum? } -impl PSOPacketData for SelectScreenCharacter { - fn from_bytes(cursor: &mut R) -> Result { - let mut buf = [0u8; SelectScreenCharacter::SIZE]; - cursor.read(&mut buf).map_err(|_| PacketParseError::ReadError)?; - SelectScreenCharacter::from_le_bytes(buf) - } - - fn as_bytes(&self) -> Vec { - self.to_le_bytes().to_vec() - } -} - #[pso_packet(0xE5)] pub struct CharacterPreview { pub slot: u32, -- 2.36.0