1
- use crate :: { attr :: * , IdentExt , camel_to_snake, r#struct:: * , AsSnake } ;
1
+ use crate :: { camel_to_snake, r#struct:: * , AsSnake } ;
2
2
3
3
use proc_macro2:: Span ;
4
- use syn:: {
5
- parse:: { Parse , ParseStream , Result } ,
6
- punctuated:: Punctuated ,
7
- token, Attribute , Ident , LitStr ,
8
- Meta :: * ,
9
- MetaNameValue ,
10
- Index ,
11
- } ;
4
+ use syn:: { parse:: Result , LitStr } ;
12
5
13
6
pub struct Input {
14
7
pub parsed_struct : Struct ,
@@ -18,7 +11,7 @@ pub trait Builder<'i> {
18
11
fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > ;
19
12
}
20
13
21
- pub struct InferredTableMacro ;
14
+ struct InferredTableMacro ;
22
15
23
16
impl < ' i > Builder < ' i > for InferredTableMacro {
24
17
fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > {
@@ -28,172 +21,23 @@ impl<'i> Builder<'i> for InferredTableMacro {
28
21
}
29
22
}
30
23
31
- pub struct CustomTableMacro < B > ( pub B ) ;
24
+ pub struct TableMacro ;
32
25
33
- impl < ' i , B : Builder < ' i > > Builder < ' i > for CustomTableMacro < B > {
26
+ impl < ' i > Builder < ' i > for TableMacro {
34
27
fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > {
35
- self . 0 . build ( input)
28
+ if let Some ( ref table) = input. parsed_struct . attrs . table {
29
+ Ok ( quote ! ( #[ table_name = #table] ) )
30
+ } else {
31
+ Ok ( InferredTableMacro . build ( input) ?)
32
+ }
36
33
}
37
34
}
38
35
39
- pub struct ModelWithId ;
36
+ pub struct Schema ;
40
37
41
- impl < ' i > Builder < ' i > for ModelWithId {
42
- fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > {
43
- let model_name_with_id = input. parsed_struct . ident . append ( "WithId" ) ;
44
- let fields = ModelWithIdFields . build ( input) ?;
45
-
46
- Ok ( quote ! {
47
- pub struct #model_name_with_id {
48
- #fields
49
- }
50
- } )
51
- }
52
- }
53
-
54
- pub struct ModelWithIdFields ;
55
-
56
- impl < ' i > Builder < ' i > for ModelWithIdFields {
57
- fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > {
58
- let mut fields = Vec :: new ( ) ;
59
- fields. push ( quote ! ( pub id: i32 ) ) ;
60
-
61
- let model_name = & input. parsed_struct . ident ;
62
- fields. push ( quote ! ( pub inner: #model_name) ) ;
63
-
64
- input. parsed_struct . fields . iter ( ) . for_each ( |field| {
65
- if field. fk ( ) {
66
- let ty = field. ty ( ) ;
67
- let name = & field. name ;
68
- fields. push ( quote ! ( pub #name: #ty) ) ;
69
- }
70
- } ) ;
71
-
72
- Ok ( quote ! ( #( #fields, ) * ) )
73
- }
74
- }
75
-
76
- pub struct Model ;
77
-
78
- impl < ' i > Builder < ' i > for Model {
79
- fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > {
80
- let model_name = & input. parsed_struct . ident ;
81
- let fields = ModelFields . build ( input) ?;
82
-
83
- Ok ( quote ! {
84
- pub struct #model_name {
85
- #fields
86
- }
87
- } )
88
- }
89
- }
90
-
91
- pub struct ModelFields ;
92
-
93
- impl < ' i > Builder < ' i > for ModelFields {
94
- fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > {
95
- let mut fields = Vec :: new ( ) ;
96
- input. parsed_struct . fields . iter ( ) . for_each ( |field| {
97
- if !field. fk ( ) {
98
- let ty = field. ty ( ) ;
99
- let name = & field. name ;
100
- fields. push ( quote ! ( pub #name: #ty) ) ;
101
- }
102
- } ) ;
103
-
104
- Ok ( quote ! ( #( #fields, ) * ) )
105
- }
106
- }
107
-
108
- pub struct Table ;
109
-
110
- impl < ' i > Builder < ' i > for Table {
38
+ impl < ' i > Builder < ' i > for Schema {
111
39
fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > {
112
40
let model = input. parsed_struct . as_snake_plural ( ) ;
113
41
Ok ( quote ! ( crate :: schema:: #model) )
114
42
}
115
43
}
116
-
117
- pub struct QueryableRow ;
118
-
119
- impl < ' i > Builder < ' i > for QueryableRow {
120
- fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > {
121
- let mut fields = Vec :: new ( ) ;
122
- fields. push ( quote ! ( i32 ) ) ;
123
-
124
- input. parsed_struct . fields . iter ( ) . for_each ( |field| {
125
- let ty = field. ty ( ) ;
126
- fields. push ( quote ! ( #ty) ) ;
127
- } ) ;
128
-
129
- Ok ( quote ! ( type Row = ( #( #fields, ) * ) ; ) )
130
- }
131
- }
132
-
133
- pub struct QueryableFields ;
134
-
135
- impl < ' i > Builder < ' i > for QueryableFields {
136
- fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > {
137
- let mut fields = Vec :: new ( ) ;
138
- let mut inner_fields = Vec :: new ( ) ;
139
-
140
- let model_name = input. parsed_struct . inner_model_name ( ) ;
141
-
142
- let mut index = 0 ;
143
-
144
- // Push id
145
- let idx = Index :: from ( index) ;
146
- fields. push ( quote ! ( id: row. #idx) ) ;
147
-
148
- input. parsed_struct . fields . iter ( ) . enumerate ( ) . for_each ( |( i, field) | {
149
- let field_name = & field. name ;
150
- if !field. fk ( ) {
151
- index = i + 1 ;
152
- let idx = Index :: from ( index) ;
153
- inner_fields. push ( quote ! ( #field_name: row. #idx) ) ;
154
- }
155
- } ) ;
156
-
157
- let generated_inner_fields = quote ! ( { #( #inner_fields, ) * } ) ;
158
-
159
- // Push inner fields
160
- fields. push ( quote ! ( inner: #model_name #generated_inner_fields) ) ;
161
-
162
- // Push remaining fields
163
- input. parsed_struct . fields . iter ( ) . for_each ( |field| {
164
- if field. fk ( ) {
165
- index = index + 1 ;
166
- let name = & field. name ;
167
- let idx = Index :: from ( index) ;
168
- fields. push ( quote ! ( #name: row. #idx) ) ;
169
- }
170
- } ) ;
171
-
172
- Ok ( quote ! ( #( #fields, ) * ) )
173
- }
174
- }
175
-
176
- pub struct Queryable ;
177
-
178
- impl < ' i > Builder < ' i > for Queryable {
179
- fn build ( self , input : & ' i Input ) -> Result < proc_macro2:: TokenStream > {
180
- let table = input. parsed_struct . as_snake_plural ( ) ;
181
- let fields = QueryableFields . build ( input) ?;
182
- let row = QueryableRow . build ( input) ?;
183
-
184
- let model_with_id = input. parsed_struct . model_name ( ) ;
185
-
186
- Ok ( quote ! {
187
- impl diesel:: Queryable <#table:: SqlType , diesel:: pg:: Pg > for #model_with_id {
188
- #row
189
- fn build( row: Self :: Row ) -> Self {
190
- #model_with_id {
191
- #fields
192
- }
193
- }
194
- }
195
- } )
196
- }
197
- }
198
-
199
-
0 commit comments