diff --git a/macros/src/data_type_from/attr/field.rs b/macros/src/data_type_from/attr/field.rs index 3191151f..337fc42a 100644 --- a/macros/src/data_type_from/attr/field.rs +++ b/macros/src/data_type_from/attr/field.rs @@ -1,3 +1,5 @@ +use proc_macro2::TokenStream; +use quote::ToTokens; use syn::Result; use crate::utils::Attribute; @@ -5,11 +7,18 @@ use crate::utils::Attribute; #[derive(Default)] pub struct FieldAttr { pub skip: bool, + pub rename: Option, } impl_parse! { FieldAttr(attr, out) { "skip" => out.skip = true, + "rename" => { + let attr = attr.parse_string()?; + out.rename = out.rename.take().or_else(|| Some( + attr.to_token_stream() + )) + }, } } diff --git a/macros/src/data_type_from/mod.rs b/macros/src/data_type_from/mod.rs index 301bef77..3aff146d 100644 --- a/macros/src/data_type_from/mod.rs +++ b/macros/src/data_type_from/mod.rs @@ -1,6 +1,6 @@ mod attr; -use quote::quote; +use quote::{quote, ToTokens}; use syn::{parse_macro_input, Data, DeriveInput, Fields}; use attr::*; @@ -47,7 +47,11 @@ pub fn derive(input: proc_macro::TokenStream) -> syn::Result rename.to_token_stream(), + None => ident.to_string().to_token_stream(), + }; let ty = then_option(attrs.skip, quote!(t.#ident.into())); diff --git a/tests/datatype.rs b/tests/datatype.rs index 8e740d40..f4c3fd94 100644 --- a/tests/datatype.rs +++ b/tests/datatype.rs @@ -38,6 +38,12 @@ struct Procedures8 {} #[derive(DataTypeFrom)] struct Procedures9(DataType, DataType); +#[derive(DataTypeFrom)] +struct Procedures10 { + #[specta(rename = "b")] + pub a: DataType, +} + #[test] fn test_datatype() { let val: DataType = Procedures1(vec![ @@ -159,4 +165,14 @@ fn test_datatype() { ts::datatype(&Default::default(), &val, &Default::default()), Ok("[any, string]".into()) ); + + let val: StructType = Procedures10 { a: DataType::Any }.into(); + assert_eq!( + ts::datatype( + &Default::default(), + &val.clone().to_anonymous(), + &Default::default() + ), + Ok("{ b: any }".into()) + ); }