Browse Source

clean up pso_packet

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

25
psopacket/src/lib.rs

@ -9,7 +9,7 @@ use quote::quote;
#[proc_macro_attribute]
pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
let arg = parse_macro_input!(attr as syn::LitInt);
let pkt_cmd = arg.value() as u16;
let pkt_cmd = arg.base10_parse::<u16>().unwrap();
let parsed = parse_macro_input!(item as ItemStruct);
@ -17,7 +17,7 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
let mut as_bytes = Vec::new();
let mut dbg_write_vars = Vec::new();
let mut partialeq = Vec::new();
for f in parsed.fields.iter() {
if let Some(ident) = &f.ident {
let ident_str = ident.to_string();
@ -26,7 +26,7 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
syn::Type::Array(arr) => {
let array_length = if let syn::Expr::Lit(lit) = &arr.len {
if let syn::Lit::Int(int) = &lit.lit {
int.value() as usize
int.base10_parse::<usize>().unwrap()
}
else {
return syn::Error::new(arr.bracket_token.span, "unknown array size").to_compile_error().into();
@ -50,14 +50,6 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
write!(f, " {}: {:?}\n", #ident_str, self.#ident.to_vec()).unwrap();
});
}
//dbg_write_vars.push(quote! {
//write!(f, " {}: {:?}\n", #ident_str, self.#ident.iter()).unwrap();
/*match std::str::from_utf8(&self.#ident) {
Ok(v) => write!(f, " {}: {:?}\n", #ident_str, v).unwrap(),
Err(_) => write!(f, " {}: {:?}\n", #ident_str, self.#ident.iter()).unwrap()
}*/
//write!(f, " {}: {:?}\n", #ident_str, var_as_str).unwrap();
//});
as_bytes.push(quote! {
for f in self.#ident.iter() {
buf.extend_from_slice(&f.to_le_bytes())
@ -143,7 +135,6 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
for c in self.#ident.as_str().encode_utf16() {
buf.extend_from_slice(&c.to_le_bytes());
}
//buf.extend_from_slice(&self.#ident.as_str().encode_utf16());
});
}
_ => {
@ -235,8 +226,6 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
#path::from_le_bytes(b)?
},
});
//return syn::Error::new(path.path.segments[0].ident.span(), "type not supported")
// .to_compile_error().into();
}
}
partialeq.push(quote! {
@ -271,7 +260,7 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
if len as usize != data.len() {
return Err(PacketParseError::WrongPacketSize(len, data.len()));
}
let result = Ok(#this_struct {
#(#from_bytes)*
});
@ -301,8 +290,8 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
}
}
};
let psopacket_debug = quote! {
impl std::fmt::Debug for #this_struct {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
@ -332,7 +321,7 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
};
//println!("[[[{}]]]", q.to_string());
q.into()
}

Loading…
Cancel
Save