Browse Source

handle String objects in pso_packet

pull/3/head
Jake Probst 6 years ago
parent
commit
47a07a63ae
  1. 35
      psopacket/src/lib.rs

35
psopacket/src/lib.rs

@ -100,10 +100,26 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
dbg_write_vars.push(quote! { dbg_write_vars.push(quote! {
write!(f, " {}: {:?}\n", #ident_str, self.#ident).unwrap(); write!(f, " {}: {:?}\n", #ident_str, self.#ident).unwrap();
}); });
as_bytes.push(quote! {
buf.extend_from_slice(&self.#ident.to_le_bytes());
});
let ty = path.path.segments[0].ident.to_string(); let ty = path.path.segments[0].ident.to_string();
// as_bytes
match ty.as_str() {
"String" => {
as_bytes.push(quote! {
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());
});
}
_ => {
as_bytes.push(quote! {
buf.extend_from_slice(&self.#ident.to_le_bytes());
});
}
}
// from_bytes
match ty.as_str() { match ty.as_str() {
"u8" | "u8_str" => { "u8" | "u8_str" => {
from_bytes.push(quote! { from_bytes.push(quote! {
@ -153,6 +169,19 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
}, },
}); });
}, },
"String" => {
from_bytes.push(quote! {
#ident: {
let mut s = String::new();
if let Ok(len) = cur.read_to_string(&mut s) {
}
else {
return Err(PacketParseError::NotEnoughBytes);
};
s
},
});
},
_ => { _ => {
return syn::Error::new(path.path.segments[0].ident.span(), "type not supported") return syn::Error::new(path.path.segments[0].ident.span(), "type not supported")
.to_compile_error().into(); .to_compile_error().into();

Loading…
Cancel
Save