Skip to content

Commit b0fe2ad

Browse files
Improve code readability
1 parent 77e846f commit b0fe2ad

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

rinja_derive/src/generator.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ impl<'a, 'h> Generator<'a, 'h> {
176176
buf.write('}');
177177

178178
#[cfg(feature = "blocks")]
179-
for (block, span) in self.input.blocks {
180-
self.impl_block(buf, block, span)?;
179+
for block in self.input.blocks {
180+
self.impl_block(buf, block)?;
181181
}
182182

183183
Ok(size_hint)
@@ -187,8 +187,7 @@ impl<'a, 'h> Generator<'a, 'h> {
187187
fn impl_block(
188188
&self,
189189
buf: &mut Buffer,
190-
block: &str,
191-
span: &proc_macro2::Span,
190+
block: &crate::input::Block,
192191
) -> Result<(), CompileError> {
193192
// RATIONALE: `*self` must be the input type, implementation details should not leak:
194193
// - impl Self { fn as_block(self) } ->
@@ -199,7 +198,7 @@ impl<'a, 'h> Generator<'a, 'h> {
199198
use quote::quote_spanned;
200199
use syn::{GenericParam, Ident, Lifetime, LifetimeParam, Token};
201200

202-
let span = *span;
201+
let span = block.span;
203202
buf.write(
204203
"\
205204
#[allow(missing_docs, non_camel_case_types, non_snake_case, unreachable_pub)]\
@@ -208,11 +207,14 @@ impl<'a, 'h> Generator<'a, 'h> {
208207

209208
let ident = &self.input.ast.ident;
210209

211-
let doc = format!("A sub-template that renders only the block `{block}` of [`{ident}`].");
212-
let method_name = format!("as_{block}");
213-
let trait_name = format!("__Rinja__{ident}__as__{block}");
214-
let wrapper_name = format!("__Rinja__{ident}__as__{block}__Wrapper");
215-
let self_lt_name = format!("'__Rinja__{ident}__as__{block}__self");
210+
let doc = format!(
211+
"A sub-template that renders only the block `{}` of [`{ident}`].",
212+
block.name
213+
);
214+
let method_name = format!("as_{}", block.name);
215+
let trait_name = format!("__Rinja__{ident}__as__{}", block.name);
216+
let wrapper_name = format!("__Rinja__{ident}__as__{}__Wrapper", block.name);
217+
let self_lt_name = format!("'__Rinja__{ident}__as__{}__self", block.name);
216218

217219
let method_id = Ident::new(&method_name, span);
218220
let trait_id = Ident::new(&trait_name, span);
@@ -235,7 +237,7 @@ impl<'a, 'h> Generator<'a, 'h> {
235237
wrapper_generics.split_for_impl();
236238

237239
let input = TemplateInput {
238-
block: Some((block, span)),
240+
block: Some((&block.name, span)),
239241
#[cfg(feature = "blocks")]
240242
blocks: &[],
241243
..self.input.clone()

rinja_derive/src/input.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub(crate) struct TemplateInput<'a> {
2727
pub(crate) source_span: Option<Span>,
2828
pub(crate) block: Option<(&'a str, Span)>,
2929
#[cfg(feature = "blocks")]
30-
pub(crate) blocks: &'a [(String, Span)],
30+
pub(crate) blocks: &'a [Block],
3131
pub(crate) print: Print,
3232
pub(crate) escaper: &'a str,
3333
pub(crate) path: Arc<Path>,
@@ -351,11 +351,17 @@ impl AnyTemplateArgs {
351351
}
352352
}
353353

354+
#[cfg(feature = "blocks")]
355+
pub(crate) struct Block {
356+
pub(crate) name: String,
357+
pub(crate) span: Span,
358+
}
359+
354360
pub(crate) struct TemplateArgs {
355361
pub(crate) source: (Source, Option<Span>),
356362
block: Option<(String, Span)>,
357363
#[cfg(feature = "blocks")]
358-
blocks: Vec<(String, Span)>,
364+
blocks: Vec<Block>,
359365
print: Print,
360366
escaping: Option<String>,
361367
ext: Option<String>,
@@ -410,7 +416,10 @@ impl TemplateArgs {
410416
.blocks
411417
.unwrap_or_default()
412418
.into_iter()
413-
.map(|value| (value.value(), value.span()))
419+
.map(|value| Block {
420+
name: value.value(),
421+
span: value.span(),
422+
})
414423
.collect(),
415424
print: args.print.unwrap_or_default(),
416425
escaping: args.escape.map(|value| value.value()),

0 commit comments

Comments
 (0)