Browse Source

use full type path

pull/3/head
jake 5 years ago
parent
commit
955e284c19
  1. 18
      psopacket/src/lib.rs

18
psopacket/src/lib.rs

@ -20,8 +20,8 @@ enum AttrMeta {
#[derive(Debug)] #[derive(Debug)]
enum AttrType { enum AttrType {
Value(syn::PathSegment, syn::Ident, AttrMeta),
Array(syn::PathSegment, syn::Ident, usize, AttrMeta)
Value(syn::TypePath, syn::Ident, AttrMeta),
Array(syn::TypePath, 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 {
@ -51,9 +51,9 @@ fn generate_from_bytes(attrs: &Vec<AttrType>) -> Vec<proc_macro2::TokenStream> {
for attr in attrs { for attr in attrs {
let element = match attr { let element = match attr {
AttrType::Value(ty, name, _) => { AttrType::Value(ty, name, _) => {
let type_str = ty.ident.to_string();
let type_str = ty.path.segments[0].ident.to_string();
if type_str == "Vec" { if type_str == "Vec" {
let vec_type = match &ty.arguments {
let vec_type = match &ty.path.segments[0].arguments {
syn::PathArguments::AngleBracketed(arg) => { syn::PathArguments::AngleBracketed(arg) => {
match &arg.args[0] { match &arg.args[0] {
syn::GenericArgument::Type(typ) => { syn::GenericArgument::Type(typ) => {
@ -109,7 +109,7 @@ fn generate_as_bytes(attrs: &Vec<AttrType>) -> Vec<proc_macro2::TokenStream> {
for attr in attrs { for attr in attrs {
let element = match attr { let element = match attr {
AttrType::Value(ty, name, _) => { AttrType::Value(ty, name, _) => {
let type_str = ty.ident.to_string();
let type_str = ty.path.segments[0].ident.to_string();
if type_str == "Vec" { if type_str == "Vec" {
quote! { quote! {
flag = self.#name.len() as u32; flag = self.#name.len() as u32;
@ -208,7 +208,7 @@ fn generate_debug_impl(name: syn::Ident, attrs: &Vec<AttrType>) -> proc_macro2::
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.path.segments[0].ident.to_string();
match meta { match meta {
AttrMeta::NoDebug => quote! { AttrMeta::NoDebug => quote! {
write!(f, " {} {}: [...]\n", #ident_str, #type_str)?; write!(f, " {} {}: [...]\n", #ident_str, #type_str)?;
@ -220,7 +220,7 @@ fn generate_debug_impl(name: syn::Ident, attrs: &Vec<AttrType>) -> proc_macro2::
}, },
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.path.segments[0].ident.to_string();
match meta { match meta {
AttrMeta::Utf8 => quote! { AttrMeta::Utf8 => quote! {
match std::str::from_utf8(&self.#name) { match std::str::from_utf8(&self.#name) {
@ -313,7 +313,7 @@ fn get_struct_fields(fields: Iter<Field>) -> Result<Vec<AttrType>, TokenStream>
syn::Type::Array(ty) => { syn::Type::Array(ty) => {
if let (syn::Type::Path(ref ty), syn::Expr::Lit(ref lit)) = (&*ty.elem, &ty.len) { if let (syn::Type::Path(ref ty), syn::Expr::Lit(ref lit)) = (&*ty.elem, &ty.len) {
if let syn::Lit::Int(ref int) = lit.lit { if let syn::Lit::Int(ref int) = lit.lit {
attrs.push(AttrType::Array(ty.path.segments[0].clone(),
attrs.push(AttrType::Array(ty.clone(),
field.ident.as_ref().unwrap().clone(), field.ident.as_ref().unwrap().clone(),
int.base10_parse().unwrap(), int.base10_parse().unwrap(),
attr_meta attr_meta
@ -326,7 +326,7 @@ fn get_struct_fields(fields: Iter<Field>) -> Result<Vec<AttrType>, TokenStream>
if type_str == "String" || type_str == "Vec"{ if type_str == "String" || type_str == "Vec"{
must_be_last = true; must_be_last = true;
} }
attrs.push(AttrType::Value(ty.path.segments[0].clone(),
attrs.push(AttrType::Value(ty.clone(),
field.ident.as_ref().unwrap().clone(), field.ident.as_ref().unwrap().clone(),
attr_meta)) attr_meta))
}, },

Loading…
Cancel
Save