mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-30 14:31:36 +00:00
Extract writing of impl headers in generator
This commit is contained in:
parent
8d97f81880
commit
2d5c2d2f10
@ -319,11 +319,20 @@ impl<'a> Generator<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn impl_template(&mut self, ast: &syn::DeriveInput, nodes: &'a [Node]) {
|
||||
fn write_header(&mut self, ast: &syn::DeriveInput,
|
||||
trait_suffix: Option<&str>) {
|
||||
let anno = annotations(&ast.generics);
|
||||
self.writeln(&format!("impl{} askama::Template for {}{} {{",
|
||||
anno, ast.ident.as_ref(), anno));
|
||||
let name = if trait_suffix.is_some() {
|
||||
format!("TraitFrom{}", trait_suffix.unwrap())
|
||||
} else {
|
||||
"askama::Template".to_string()
|
||||
};
|
||||
self.writeln(&format!("impl{} {} for {}{} {{",
|
||||
anno, &name, ast.ident.as_ref(), anno));
|
||||
}
|
||||
|
||||
fn impl_template(&mut self, ast: &syn::DeriveInput, nodes: &'a [Node]) {
|
||||
self.write_header(ast, None);
|
||||
self.writeln("fn render_to(&self, writer: &mut std::fmt::Write) {");
|
||||
self.handle(nodes);
|
||||
self.flush_ws(&WS(false, false));
|
||||
@ -333,10 +342,7 @@ impl<'a> Generator<'a> {
|
||||
|
||||
fn impl_trait(&mut self, ast: &syn::DeriveInput, base: &str,
|
||||
blocks: &'a [Node], nodes: Option<&'a [Node]>) {
|
||||
let anno = annotations(&ast.generics);
|
||||
self.writeln(&format!("impl{} TraitFrom{} for {}{} {{",
|
||||
anno, path_as_identifier(base),
|
||||
ast.ident.as_ref(), anno));
|
||||
self.write_header(ast, Some(&path_as_identifier(base)));
|
||||
self.handle(blocks);
|
||||
|
||||
self.writeln("#[allow(unused_variables)]");
|
||||
@ -358,9 +364,7 @@ impl<'a> Generator<'a> {
|
||||
}
|
||||
|
||||
fn impl_template_for_trait(&mut self, ast: &syn::DeriveInput, derived: bool) {
|
||||
let anno = annotations(&ast.generics);
|
||||
self.writeln(&format!("impl{} askama::Template for {}{} {{",
|
||||
anno, ast.ident.as_ref(), anno));
|
||||
self.write_header(ast, None);
|
||||
self.writeln("fn render_to(&self, writer: &mut std::fmt::Write) {");
|
||||
if derived {
|
||||
self.writeln("self._parent.render_trait_to(self, writer);");
|
||||
|
Loading…
x
Reference in New Issue
Block a user