@@ -3,17 +3,17 @@ use quote::ToTokens;
33use syn:: spanned:: Spanned ;
44use syn:: { Field as SynField , Ident , Index , Type } ;
55
6- use attrs:: { parse_attributes, FieldAttr , SqlIdentifier } ;
6+ use attrs:: { parse_attributes, AttributeSpanWrapper , FieldAttr , SqlIdentifier } ;
77
88pub struct Field {
99 pub ty : Type ,
1010 pub span : Span ,
1111 pub name : FieldName ,
12- column_name : Option < SqlIdentifier > ,
13- pub sql_type : Option < Type > ,
14- pub serialize_as : Option < Type > ,
15- pub deserialize_as : Option < Type > ,
16- pub embed : bool ,
12+ column_name : Option < AttributeSpanWrapper < SqlIdentifier > > ,
13+ pub sql_type : Option < AttributeSpanWrapper < Type > > ,
14+ pub serialize_as : Option < AttributeSpanWrapper < Type > > ,
15+ pub deserialize_as : Option < AttributeSpanWrapper < Type > > ,
16+ pub embed : Option < AttributeSpanWrapper < bool > > ,
1717}
1818
1919impl Field {
@@ -26,15 +26,47 @@ impl Field {
2626 let mut sql_type = None ;
2727 let mut serialize_as = None ;
2828 let mut deserialize_as = None ;
29- let mut embed = false ;
29+ let mut embed = None ;
3030
3131 for attr in parse_attributes ( attrs) {
32- match attr {
33- FieldAttr :: ColumnName ( _, value) => column_name = Some ( value) ,
34- FieldAttr :: SqlType ( _, value) => sql_type = Some ( Type :: Path ( value) ) ,
35- FieldAttr :: SerializeAs ( _, value) => serialize_as = Some ( Type :: Path ( value) ) ,
36- FieldAttr :: DeserializeAs ( _, value) => deserialize_as = Some ( Type :: Path ( value) ) ,
37- FieldAttr :: Embed ( _) => embed = true ,
32+ let attribute_span = attr. attribute_span ;
33+ let ident_span = attr. ident_span ;
34+ match attr. item {
35+ FieldAttr :: ColumnName ( _, value) => {
36+ column_name = Some ( AttributeSpanWrapper {
37+ item : value,
38+ attribute_span,
39+ ident_span,
40+ } )
41+ }
42+ FieldAttr :: SqlType ( _, value) => {
43+ sql_type = Some ( AttributeSpanWrapper {
44+ item : Type :: Path ( value) ,
45+ attribute_span,
46+ ident_span,
47+ } )
48+ }
49+ FieldAttr :: SerializeAs ( _, value) => {
50+ serialize_as = Some ( AttributeSpanWrapper {
51+ item : Type :: Path ( value) ,
52+ attribute_span,
53+ ident_span,
54+ } )
55+ }
56+ FieldAttr :: DeserializeAs ( _, value) => {
57+ deserialize_as = Some ( AttributeSpanWrapper {
58+ item : Type :: Path ( value) ,
59+ attribute_span,
60+ ident_span,
61+ } )
62+ }
63+ FieldAttr :: Embed ( _) => {
64+ embed = Some ( AttributeSpanWrapper {
65+ item : true ,
66+ attribute_span,
67+ ident_span,
68+ } )
69+ }
3870 }
3971 }
4072
@@ -61,24 +93,31 @@ impl Field {
6193 }
6294
6395 pub fn column_name ( & self ) -> SqlIdentifier {
64- self . column_name . clone ( ) . unwrap_or_else ( || match self . name {
65- FieldName :: Named ( ref x) => x. into ( ) ,
66- FieldName :: Unnamed ( ref x) => {
67- abort ! (
96+ self . column_name
97+ . as_ref ( )
98+ . map ( |a| a. item . clone ( ) )
99+ . unwrap_or_else ( || match self . name {
100+ FieldName :: Named ( ref x) => x. into ( ) ,
101+ FieldName :: Unnamed ( ref x) => {
102+ abort ! (
68103 x,
69104 "All fields of tuple structs must be annotated with `#[diesel(column_name)]`"
70105 ) ;
71- }
72- } )
106+ }
107+ } )
73108 }
74109
75110 pub fn ty_for_deserialize ( & self ) -> & Type {
76- if let Some ( value) = & self . deserialize_as {
111+ if let Some ( AttributeSpanWrapper { item : value, .. } ) = & self . deserialize_as {
77112 value
78113 } else {
79114 & self . ty
80115 }
81116 }
117+
118+ pub ( crate ) fn embed ( & self ) -> bool {
119+ self . embed . as_ref ( ) . map ( |a| a. item ) . unwrap_or ( false )
120+ }
82121}
83122
84123pub enum FieldName {
0 commit comments