|
|
@ -20,8 +20,8 @@ enum AttrMeta { |
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
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 {
|
|
|
@ -51,9 +51,9 @@ fn generate_from_bytes(attrs: &Vec<AttrType>) -> Vec<proc_macro2::TokenStream> { |
|
|
|
for attr in attrs {
|
|
|
|
let element = match attr {
|
|
|
|
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" {
|
|
|
|
let vec_type = match &ty.arguments {
|
|
|
|
let vec_type = match &ty.path.segments[0].arguments {
|
|
|
|
syn::PathArguments::AngleBracketed(arg) => {
|
|
|
|
match &arg.args[0] {
|
|
|
|
syn::GenericArgument::Type(typ) => {
|
|
|
@ -109,7 +109,7 @@ fn generate_as_bytes(attrs: &Vec<AttrType>) -> Vec<proc_macro2::TokenStream> { |
|
|
|
for attr in attrs {
|
|
|
|
let element = match attr {
|
|
|
|
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" {
|
|
|
|
quote! {
|
|
|
|
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 {
|
|
|
|
AttrType::Value(ty, name, meta) => {
|
|
|
|
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 {
|
|
|
|
AttrMeta::NoDebug => quote! {
|
|
|
|
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) => {
|
|
|
|
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 {
|
|
|
|
AttrMeta::Utf8 => quote! {
|
|
|
|
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) => {
|
|
|
|
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 {
|
|
|
|
attrs.push(AttrType::Array(ty.path.segments[0].clone(),
|
|
|
|
attrs.push(AttrType::Array(ty.clone(),
|
|
|
|
field.ident.as_ref().unwrap().clone(),
|
|
|
|
int.base10_parse().unwrap(),
|
|
|
|
attr_meta
|
|
|
@ -326,7 +326,7 @@ fn get_struct_fields(fields: Iter<Field>) -> Result<Vec<AttrType>, TokenStream> |
|
|
|
if type_str == "String" || type_str == "Vec"{
|
|
|
|
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(),
|
|
|
|
attr_meta))
|
|
|
|
},
|
|
|
|