@@ -15,6 +15,7 @@ use proc_macro2::TokenStream;
15
15
use quote:: { format_ident, quote} ;
16
16
17
17
/// Generates code of `#[derive(WorldInit)]` macro expansion.
18
+ #[ allow( clippy:: similar_names) ]
18
19
pub ( crate ) fn world_init (
19
20
input : TokenStream ,
20
21
steps : & [ & str ] ,
@@ -26,10 +27,16 @@ pub(crate) fn world_init(
26
27
let step_types = step_types ( steps, world) ;
27
28
let step_structs = generate_step_structs ( steps, & input) ;
28
29
30
+ let given_ty = & step_types[ 0 ] ;
31
+ let when_ty = & step_types[ 1 ] ;
32
+ let then_ty = & step_types[ 2 ] ;
33
+
29
34
Ok ( quote ! {
30
- impl :: cucumber:: codegen:: WorldInventory <
31
- #( #step_types, ) *
32
- > for #world { }
35
+ impl :: cucumber:: codegen:: WorldInventory for #world {
36
+ type Given = #given_ty;
37
+ type When = #when_ty;
38
+ type Then = #then_ty;
39
+ }
33
40
34
41
#( #step_structs ) *
35
42
} )
@@ -41,10 +48,7 @@ pub(crate) fn world_init(
41
48
fn step_types ( steps : & [ & str ] , world : & syn:: Ident ) -> Vec < syn:: Ident > {
42
49
steps
43
50
. iter ( )
44
- . map ( |step| {
45
- let step = to_pascal_case ( step) ;
46
- format_ident ! ( "Cucumber{}{}" , step, world)
47
- } )
51
+ . map ( |step| format_ident ! ( "Cucumber{}{}" , to_pascal_case( step) , world) )
48
52
. collect ( )
49
53
}
50
54
@@ -53,7 +57,8 @@ fn generate_step_structs(
53
57
steps : & [ & str ] ,
54
58
world : & syn:: DeriveInput ,
55
59
) -> Vec < TokenStream > {
56
- let ( world, world_vis) = ( & world. ident , & world. vis ) ;
60
+ let world_vis = & world. vis ;
61
+ let world = & world. ident ;
57
62
58
63
step_types ( steps, world)
59
64
. iter ( )
@@ -63,35 +68,23 @@ fn generate_step_structs(
63
68
#[ doc( hidden) ]
64
69
#world_vis struct #ty {
65
70
#[ doc( hidden) ]
66
- pub loc: :: cucumber:: step:: Location ,
71
+ #world_vis loc: :: cucumber:: step:: Location ,
67
72
68
73
#[ doc( hidden) ]
69
- pub regex: :: cucumber:: codegen:: Regex ,
74
+ #world_vis regex: :: cucumber:: codegen:: LazyRegex ,
70
75
71
76
#[ doc( hidden) ]
72
- pub func: :: cucumber:: Step <#world>,
77
+ #world_vis func: :: cucumber:: Step <#world>,
73
78
}
74
79
75
80
#[ automatically_derived]
76
81
impl :: cucumber:: codegen:: StepConstructor <#world> for #ty {
77
- fn new (
78
- loc: :: cucumber:: step:: Location ,
79
- regex: :: cucumber:: codegen:: Regex ,
80
- func: :: cucumber:: Step <#world>,
81
- ) -> Self {
82
- Self { loc, regex, func }
83
- }
84
-
85
82
fn inner( & self ) -> (
86
83
:: cucumber:: step:: Location ,
87
- :: cucumber:: codegen:: Regex ,
84
+ :: cucumber:: codegen:: LazyRegex ,
88
85
:: cucumber:: Step <#world>,
89
86
) {
90
- (
91
- self . loc. clone( ) ,
92
- self . regex. clone( ) ,
93
- self . func. clone( ) ,
94
- )
87
+ ( self . loc, self . regex, self . func)
95
88
}
96
89
}
97
90
@@ -114,45 +107,35 @@ mod spec {
114
107
} ;
115
108
116
109
let output = quote ! {
117
- impl :: cucumber:: codegen:: WorldInventory <
118
- CucumberGivenWorld , CucumberWhenWorld , CucumberThenWorld ,
119
- > for World { }
110
+ impl :: cucumber:: codegen:: WorldInventory for World {
111
+ type Given = CucumberGivenWorld ;
112
+ type When = CucumberWhenWorld ;
113
+ type Then = CucumberThenWorld ;
114
+ }
120
115
121
116
#[ automatically_derived]
122
117
#[ doc( hidden) ]
123
118
pub struct CucumberGivenWorld {
124
- #[ doc( hidden) ]
125
- pub loc: :: cucumber:: step:: Location ,
119
+ #[ doc( hidden) ]
120
+ pub loc: :: cucumber:: step:: Location ,
126
121
127
- #[ doc( hidden) ]
128
- pub regex: :: cucumber:: codegen:: Regex ,
122
+ #[ doc( hidden) ]
123
+ pub regex: :: cucumber:: codegen:: LazyRegex ,
129
124
130
- #[ doc( hidden) ]
131
- pub func: :: cucumber:: Step <World >,
125
+ #[ doc( hidden) ]
126
+ pub func: :: cucumber:: Step <World >,
132
127
}
133
128
134
129
#[ automatically_derived]
135
130
impl :: cucumber:: codegen:: StepConstructor <World > for
136
131
CucumberGivenWorld
137
132
{
138
- fn new (
139
- loc: :: cucumber:: step:: Location ,
140
- regex: :: cucumber:: codegen:: Regex ,
141
- func: :: cucumber:: Step <World >,
142
- ) -> Self {
143
- Self { loc, regex, func }
144
- }
145
-
146
133
fn inner( & self ) -> (
147
134
:: cucumber:: step:: Location ,
148
- :: cucumber:: codegen:: Regex ,
135
+ :: cucumber:: codegen:: LazyRegex ,
149
136
:: cucumber:: Step <World >,
150
137
) {
151
- (
152
- self . loc. clone( ) ,
153
- self . regex. clone( ) ,
154
- self . func. clone( ) ,
155
- )
138
+ ( self . loc, self . regex, self . func)
156
139
}
157
140
}
158
141
@@ -162,38 +145,26 @@ mod spec {
162
145
#[ automatically_derived]
163
146
#[ doc( hidden) ]
164
147
pub struct CucumberWhenWorld {
165
- #[ doc( hidden) ]
166
- pub loc: :: cucumber:: step:: Location ,
148
+ #[ doc( hidden) ]
149
+ pub loc: :: cucumber:: step:: Location ,
167
150
168
- #[ doc( hidden) ]
169
- pub regex: :: cucumber:: codegen:: Regex ,
151
+ #[ doc( hidden) ]
152
+ pub regex: :: cucumber:: codegen:: LazyRegex ,
170
153
171
- #[ doc( hidden) ]
172
- pub func: :: cucumber:: Step <World >,
154
+ #[ doc( hidden) ]
155
+ pub func: :: cucumber:: Step <World >,
173
156
}
174
157
175
158
#[ automatically_derived]
176
159
impl :: cucumber:: codegen:: StepConstructor <World > for
177
160
CucumberWhenWorld
178
161
{
179
- fn new (
180
- loc: :: cucumber:: step:: Location ,
181
- regex: :: cucumber:: codegen:: Regex ,
182
- func: :: cucumber:: Step <World >,
183
- ) -> Self {
184
- Self { loc, regex, func }
185
- }
186
-
187
162
fn inner( & self ) -> (
188
163
:: cucumber:: step:: Location ,
189
- :: cucumber:: codegen:: Regex ,
164
+ :: cucumber:: codegen:: LazyRegex ,
190
165
:: cucumber:: Step <World >,
191
166
) {
192
- (
193
- self . loc. clone( ) ,
194
- self . regex. clone( ) ,
195
- self . func. clone( ) ,
196
- )
167
+ ( self . loc, self . regex, self . func)
197
168
}
198
169
}
199
170
@@ -203,38 +174,26 @@ mod spec {
203
174
#[ automatically_derived]
204
175
#[ doc( hidden) ]
205
176
pub struct CucumberThenWorld {
206
- #[ doc( hidden) ]
207
- pub loc: :: cucumber:: step:: Location ,
177
+ #[ doc( hidden) ]
178
+ pub loc: :: cucumber:: step:: Location ,
208
179
209
- #[ doc( hidden) ]
210
- pub regex: :: cucumber:: codegen:: Regex ,
180
+ #[ doc( hidden) ]
181
+ pub regex: :: cucumber:: codegen:: LazyRegex ,
211
182
212
- #[ doc( hidden) ]
213
- pub func: :: cucumber:: Step <World >,
183
+ #[ doc( hidden) ]
184
+ pub func: :: cucumber:: Step <World >,
214
185
}
215
186
216
187
#[ automatically_derived]
217
188
impl :: cucumber:: codegen:: StepConstructor <World > for
218
189
CucumberThenWorld
219
190
{
220
- fn new (
221
- loc: :: cucumber:: step:: Location ,
222
- regex: :: cucumber:: codegen:: Regex ,
223
- func: :: cucumber:: Step <World >,
224
- ) -> Self {
225
- Self { loc, regex, func }
226
- }
227
-
228
191
fn inner( & self ) -> (
229
192
:: cucumber:: step:: Location ,
230
- :: cucumber:: codegen:: Regex ,
193
+ :: cucumber:: codegen:: LazyRegex ,
231
194
:: cucumber:: Step <World >,
232
195
) {
233
- (
234
- self . loc. clone( ) ,
235
- self . regex. clone( ) ,
236
- self . func. clone( ) ,
237
- )
196
+ ( self . loc, self . regex, self . func)
238
197
}
239
198
}
240
199
0 commit comments