@@ -25,9 +25,12 @@ pub(crate) fn template_to_string(
25
25
input : & TemplateInput < ' _ > ,
26
26
contexts : & HashMap < & Arc < Path > , Context < ' _ > , FxBuildHasher > ,
27
27
heritage : Option < & Heritage < ' _ , ' _ > > ,
28
- target : Option < & str > ,
28
+ tmpl_kind : TmplKind ,
29
29
) -> Result < usize , CompileError > {
30
- let ctx = & contexts[ & input. path ] ;
30
+ if tmpl_kind == TmplKind :: Struct {
31
+ buf. write ( "const _: () = { extern crate rinja as rinja;" ) ;
32
+ }
33
+
31
34
let generator = Generator :: new (
32
35
input,
33
36
contexts,
@@ -36,13 +39,27 @@ pub(crate) fn template_to_string(
36
39
input. block . is_some ( ) ,
37
40
0 ,
38
41
) ;
39
- let mut result = generator. build ( ctx, buf, target) ;
40
- if let Err ( err) = & mut result {
41
- if err. span . is_none ( ) {
42
+ let size_hint = match generator. impl_template ( buf, tmpl_kind) {
43
+ Err ( mut err) if err. span . is_none ( ) => {
42
44
err. span = input. source_span ;
45
+ Err ( err)
43
46
}
47
+ result => result,
48
+ } ?;
49
+
50
+ if tmpl_kind == TmplKind :: Struct {
51
+ impl_everything ( input. ast , buf) ;
52
+ buf. write ( "};" ) ;
44
53
}
45
- result
54
+ Ok ( size_hint)
55
+ }
56
+
57
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
58
+ pub ( crate ) enum TmplKind {
59
+ /// [`rinja::Template`]
60
+ Struct ,
61
+ /// [`rinja::helpers::EnumVariantTemplate`]
62
+ Variant ,
46
63
}
47
64
48
65
struct Generator < ' a , ' h > {
@@ -97,31 +114,18 @@ impl<'a, 'h> Generator<'a, 'h> {
97
114
}
98
115
}
99
116
100
- // Takes a Context and generates the relevant implementations.
101
- fn build (
102
- mut self ,
103
- ctx : & Context < ' a > ,
104
- buf : & mut Buffer ,
105
- target : Option < & str > ,
106
- ) -> Result < usize , CompileError > {
107
- if target. is_none ( ) {
108
- buf. write ( "const _: () = { extern crate rinja as rinja;" ) ;
109
- }
110
- let size_hint = self . impl_template ( ctx, buf, target. unwrap_or ( "rinja::Template" ) ) ?;
111
- if target. is_none ( ) {
112
- impl_everything ( self . input . ast , buf) ;
113
- buf. write ( "};" ) ;
114
- }
115
- Ok ( size_hint)
116
- }
117
-
118
117
// Implement `Template` for the given context struct.
119
118
fn impl_template (
120
- & mut self ,
121
- ctx : & Context < ' a > ,
119
+ mut self ,
122
120
buf : & mut Buffer ,
123
- target : & str ,
121
+ tmpl_kind : TmplKind ,
124
122
) -> Result < usize , CompileError > {
123
+ let ctx = & self . contexts [ & self . input . path ] ;
124
+
125
+ let target = match tmpl_kind {
126
+ TmplKind :: Struct => "rinja::Template" ,
127
+ TmplKind :: Variant => "rinja::helpers::EnumVariantTemplate" ,
128
+ } ;
125
129
write_header ( self . input . ast , buf, target) ;
126
130
buf. write (
127
131
"fn render_into_with_values<RinjaW>(\
@@ -161,12 +165,12 @@ impl<'a, 'h> Generator<'a, 'h> {
161
165
162
166
let size_hint = self . impl_template_inner ( ctx, buf) ?;
163
167
164
- buf. write ( format_args ! (
165
- " \
166
- rinja::Result::Ok(()) \
167
- }} \
168
- const SIZE_HINT: rinja::helpers::core::primitive::usize = {size_hint}usize;" ,
169
- ) ) ;
168
+ buf. write ( "rinja::Result::Ok(()) }" ) ;
169
+ if tmpl_kind == TmplKind :: Struct {
170
+ buf . write ( format_args ! (
171
+ "const SIZE_HINT: rinja::helpers::core::primitive::usize = {size_hint}usize;" ,
172
+ ) ) ;
173
+ }
170
174
171
175
buf. write ( '}' ) ;
172
176
Ok ( size_hint)
0 commit comments