@@ -196,112 +196,70 @@ impl<'a> Generator<'a> {
196
196
// Implement `From<Template> for hyper::Response<Body>` and `From<Template> for hyper::Body.
197
197
#[ cfg( feature = "with-hyper" ) ]
198
198
fn impl_hyper_into_response ( & mut self , buf : & mut Buffer ) -> Result < ( ) , CompileError > {
199
- let ( impl_generics, orig_ty_generics, where_clause) =
200
- self . input . ast . generics . split_for_impl ( ) ;
201
- let ident = & self . input . ast . ident ;
202
- // From<Template> for hyper::Response<Body>
203
- buf. writeln ( & format ! (
204
- "{} {{" ,
205
- quote!(
206
- impl #impl_generics :: core:: convert:: From <& #ident #orig_ty_generics>
207
- for :: askama_hyper:: hyper:: Response <:: askama_hyper:: hyper:: Body >
208
- #where_clause
209
- )
210
- ) ) ?;
211
- buf. writeln ( "#[inline]" ) ?;
212
- buf. writeln ( & format ! (
213
- "{} {{" ,
214
- quote!( fn from( value: & #ident #orig_ty_generics) -> Self )
215
- ) ) ?;
216
- buf. writeln ( "::askama_hyper::respond(value)" ) ?;
217
- buf. writeln ( "}" ) ?;
218
- buf. writeln ( "}" ) ?;
199
+ let generics_with_bounds = self . input . ast . generics . display_with_bounds ( ) ;
200
+ let orig_ty_generics = & self . input . ast . generics ;
201
+ let type_name = & self . input . ast . ident ;
202
+ let where_clause = & self . input . ast . where_clause ;
219
203
220
- // TryFrom<Template> for hyper::Body
221
- buf. writeln ( & format ! (
222
- "{} {{" ,
223
- quote!(
224
- impl #impl_generics :: core:: convert:: TryFrom <& #ident #orig_ty_generics>
225
- for :: askama_hyper:: hyper:: Body
226
- #where_clause
227
- )
228
- ) ) ?;
229
- buf. writeln ( "type Error = ::askama::Error;" ) ?;
230
- buf. writeln ( "#[inline]" ) ?;
204
+ // From<Template> for hyper::Response<Body>
231
205
buf. writeln ( & format ! (
232
- "{} {{" ,
233
- quote!( fn try_from( value: & #ident #orig_ty_generics) -> Result <Self , Self :: Error >)
234
- ) ) ?;
235
- buf. writeln ( "::askama::Template::render(value).map(Into::into)" ) ?;
236
- buf. writeln ( "}" ) ?;
237
- buf. writeln ( "}" )
206
+ "\
207
+ impl {generics_with_bounds} ::core::convert::From<&{type_name} {orig_ty_generics}>
208
+ for ::askama_hyper::hyper::Response<::askama_hyper::hyper::Body>
209
+ {where_clause}
210
+ {{
211
+ #[inline]
212
+ fn from(value: &{type_name} {orig_ty_generics}) -> Self {{
213
+ ::askama_hyper::respond(value)
214
+ }}
215
+ }}
216
+
217
+ impl {generics_with_bounds} ::core::convert::TryFrom<&{type_name} {orig_ty_generics}>
218
+ for ::askama_hyper::hyper::Body
219
+ {where_clause}
220
+ {{
221
+ type Error = ::askama::Error;
222
+
223
+ #[inline]
224
+ fn try_from(value: &{type_name} {orig_ty_generics}) -> Result<Self, Self::Error> {{
225
+ ::askama::Template::render(value).map(Into::into)
226
+ }}
227
+ }}"
228
+ ) )
238
229
}
239
230
240
231
// Implement mendes' `Responder`.
241
232
#[ cfg( feature = "with-mendes" ) ]
242
233
fn impl_mendes_responder ( & mut self , buf : & mut Buffer ) -> Result < ( ) , CompileError > {
243
- let param = syn:: parse_str ( "A: ::mendes::Application" ) . unwrap ( ) ;
244
-
245
234
let mut generics = self . input . ast . generics . clone ( ) ;
246
- generics. params . push ( param) ;
247
- let ( _, orig_ty_generics, _) = self . input . ast . generics . split_for_impl ( ) ;
248
- let ( impl_generics, _, where_clause) = generics. split_for_impl ( ) ;
249
-
250
- let mut where_clause = match where_clause {
251
- Some ( clause) => clause. clone ( ) ,
252
- None => syn:: WhereClause {
253
- where_token : syn:: Token ![ where ] ( proc_macro2:: Span :: call_site ( ) ) ,
254
- predicates : syn:: punctuated:: Punctuated :: new ( ) ,
255
- } ,
256
- } ;
235
+ generics. add_generic ( "A" , "::mendes::Application" ) ;
257
236
258
- where_clause
259
- . predicates
260
- . push ( syn:: parse_str ( "A::ResponseBody: From<String>" ) . unwrap ( ) ) ;
261
- where_clause
262
- . predicates
263
- . push ( syn:: parse_str ( "A::Error: From<::askama_mendes::Error>" ) . unwrap ( ) ) ;
237
+ let generics_with_bounds = generics. display_with_bounds ( ) ;
238
+ let orig_ty_generics = & self . input . ast . generics ;
239
+ let type_name = & self . input . ast . ident ;
240
+ let mut where_clause = self . input . ast . where_clause . clone ( ) ;
264
241
265
- buf. writeln (
266
- format ! (
267
- "{} {} for {} {} {{" ,
268
- quote!( impl #impl_generics) ,
269
- "::mendes::application::IntoResponse<A>" ,
270
- self . input. ast. ident,
271
- quote!( #orig_ty_generics #where_clause) ,
272
- )
273
- . as_ref ( ) ,
274
- ) ?;
242
+ where_clause. add_predicate ( "A::ResponseBody: From<String>" ) ;
243
+ where_clause. add_predicate ( "A::Error: From<::askama_mendes::Error>" ) ;
275
244
276
- buf. writeln (
277
- "fn into_response(self, app: &A, req: &::mendes::http::request::Parts) \
278
- -> ::mendes::http::Response<A::ResponseBody> {" ,
279
- ) ? ;
280
-
281
- buf . writeln ( " ::askama_mendes::into_response(app, req, &self)" ) ? ;
282
- buf . writeln ( "}" ) ? ;
283
- buf . writeln ( "}" ) ? ;
284
- Ok ( ( ) )
245
+ buf. writeln ( & format ! (
246
+ "\
247
+ impl {generics_with_bounds} ::mendes::application::IntoResponse<A> for {type_name} {orig_ty_generics} {{
248
+ fn into_response(self, app: &A, req: &::mendes::http::request::Parts) \
249
+ -> ::mendes::http::Response<A::ResponseBody> {{
250
+ ::askama_mendes::into_response(app, req, &self)
251
+ }}
252
+ }}" ,
253
+ ) )
285
254
}
286
255
287
256
// Implement Rocket's `Responder`.
288
257
#[ cfg( feature = "with-rocket" ) ]
289
258
fn impl_rocket_responder ( & mut self , buf : & mut Buffer ) -> Result < ( ) , CompileError > {
290
- let lifetime1 = syn:: Lifetime :: new ( "'askama1" , proc_macro2:: Span :: call_site ( ) ) ;
291
- let lifetime2 = syn:: Lifetime :: new ( "'askama2" , proc_macro2:: Span :: call_site ( ) ) ;
292
-
293
- let mut param2 = syn:: LifetimeParam :: new ( lifetime2) ;
294
- param2. colon_token = Some ( syn:: Token ![ : ] ( proc_macro2:: Span :: call_site ( ) ) ) ;
295
- param2. bounds = syn:: punctuated:: Punctuated :: new ( ) ;
296
- param2. bounds . push_value ( lifetime1. clone ( ) ) ;
297
-
298
- let param1 = syn:: GenericParam :: Lifetime ( syn:: LifetimeParam :: new ( lifetime1) ) ;
299
- let param2 = syn:: GenericParam :: Lifetime ( param2) ;
300
-
301
259
self . write_header (
302
260
buf,
303
261
"::askama_rocket::Responder<'askama1, 'askama2>" ,
304
- Some ( vec ! [ param1 , param2 ] ) ,
262
+ Some ( & [ ( "'askama1" , "" ) , ( "'askama2" , "'askama1" ) ] ) ,
305
263
) ?;
306
264
307
265
buf. writeln ( "#[inline]" ) ?;
@@ -324,30 +282,39 @@ impl<'a> Generator<'a> {
324
282
None ,
325
283
) ?;
326
284
buf. writeln (
327
- "type Error = ::askama_tide::askama::Error;\n \
328
- #[inline]\n \
329
- fn try_into(self) -> ::askama_tide::askama::Result<::askama_tide::tide::Body> {",
285
+ "\
286
+ type Error = ::askama_tide::askama::Error;
287
+
288
+ #[inline]
289
+ fn try_into(self) -> ::askama_tide::askama::Result<::askama_tide::tide::Body> {
290
+ ::askama_tide::try_into_body(&self)
291
+ }
292
+ }" ,
330
293
) ?;
331
- buf. writeln ( "::askama_tide::try_into_body(&self)" ) ?;
332
- buf. writeln ( "}" ) ?;
333
- buf. writeln ( "}" ) ?;
334
294
335
295
buf. writeln ( "#[allow(clippy::from_over_into)]" ) ?;
336
296
self . write_header ( buf, "Into<::askama_tide::tide::Response>" , None ) ?;
337
- buf. writeln ( "#[inline]" ) ?;
338
- buf. writeln ( "fn into(self) -> ::askama_tide::tide::Response {" ) ?;
339
- buf. writeln ( "::askama_tide::into_response(&self)" ) ?;
340
- buf. writeln ( "}\n }" )
297
+ buf. writeln (
298
+ "\
299
+ #[inline]
300
+ fn into(self) -> ::askama_tide::tide::Response {
301
+ ::askama_tide::into_response(&self)
302
+ }
303
+ }" ,
304
+ )
341
305
}
342
306
343
307
#[ cfg( feature = "with-warp" ) ]
344
308
fn impl_warp_reply ( & mut self , buf : & mut Buffer ) -> Result < ( ) , CompileError > {
345
309
self . write_header ( buf, "::askama_warp::warp::reply::Reply" , None ) ?;
346
- buf. writeln ( "#[inline]" ) ?;
347
- buf. writeln ( "fn into_response(self) -> ::askama_warp::warp::reply::Response {" ) ?;
348
- buf. writeln ( "::askama_warp::reply(&self)" ) ?;
349
- buf. writeln ( "}" ) ?;
350
- buf. writeln ( "}" )
310
+ buf. writeln (
311
+ "\
312
+ #[inline]
313
+ fn into_response(self) -> ::askama_warp::warp::reply::Response {
314
+ ::askama_warp::reply(&self)
315
+ }
316
+ }" ,
317
+ )
351
318
}
352
319
353
320
// Writes header for the `impl` for `TraitFromPathName` or `Template`
@@ -356,25 +323,25 @@ impl<'a> Generator<'a> {
356
323
& mut self ,
357
324
buf : & mut Buffer ,
358
325
target : & str ,
359
- params : Option < Vec < syn :: GenericParam > > ,
326
+ lifetimes : Option < & [ ( & str , & str ) ] > ,
360
327
) -> Result < ( ) , CompileError > {
361
- let mut generics = self . input . ast . generics . clone ( ) ;
362
- if let Some ( params) = params {
363
- for param in params {
364
- generics. params . push ( param) ;
328
+ let mut generics;
329
+ let generics = if let Some ( lifetimes) = lifetimes {
330
+ generics = self . input . ast . generics . clone ( ) ;
331
+ for ( lifetime, bound) in lifetimes {
332
+ generics. add_lifetime ( lifetime, bound) ;
365
333
}
366
- }
367
- let ( _, orig_ty_generics, _) = self . input . ast . generics . split_for_impl ( ) ;
368
- let ( impl_generics, _, where_clause) = generics. split_for_impl ( ) ;
334
+ & generics
335
+ } else {
336
+ & self . input . ast . generics
337
+ } ;
338
+ let generics_with_bounds = generics. display_with_bounds ( ) ;
339
+ let orig_ty_generics = & self . input . ast . generics ;
340
+ let type_name = & self . input . ast . ident ;
341
+ let where_clause = & self . input . ast . where_clause ;
369
342
buf. writeln (
370
- format ! (
371
- "{} {} for {}{} {{" ,
372
- quote!( impl #impl_generics) ,
373
- target,
374
- self . input. ast. ident,
375
- quote!( #orig_ty_generics #where_clause) ,
376
- )
377
- . as_ref ( ) ,
343
+ format ! ( "impl{generics_with_bounds} {target} for {type_name}{orig_ty_generics} {where_clause} {{" )
344
+ . as_ref ( ) ,
378
345
)
379
346
}
380
347
0 commit comments