@@ -33,19 +33,15 @@ const BLOCK_HELPER_MISSING: &str = "blockHelperMissing";
33
33
/// This context stores information of a render and a writer where generated
34
34
/// content is written to.
35
35
///
36
- #[ derive( Clone , Debug ) ]
37
- pub struct RenderContext < ' reg , ' rc > {
38
- inner : Rc < RenderContextInner < ' reg , ' rc > > ,
39
-
36
+ #[ derive( Clone ) ]
37
+ pub struct RenderContext < ' reg : ' rc , ' rc > {
40
38
dev_mode_templates : Option < & ' rc BTreeMap < String , Cow < ' rc , Template > > > ,
41
39
42
40
blocks : VecDeque < BlockContext < ' rc > > ,
41
+
43
42
// copy-on-write context
44
43
modified_context : Option < Rc < Context > > ,
45
- }
46
44
47
- #[ derive( Clone ) ]
48
- pub struct RenderContextInner < ' reg : ' rc , ' rc > {
49
45
partials : BTreeMap < String , & ' rc Template > ,
50
46
partial_block_stack : VecDeque < & ' rc Template > ,
51
47
partial_block_depth : isize ,
@@ -72,7 +68,11 @@ pub struct RenderContextInner<'reg: 'rc, 'rc> {
72
68
impl < ' reg : ' rc , ' rc > RenderContext < ' reg , ' rc > {
73
69
/// Create a render context
74
70
pub fn new ( root_template : Option < & ' reg String > ) -> RenderContext < ' reg , ' rc > {
75
- let inner = Rc :: new ( RenderContextInner {
71
+ let mut blocks = VecDeque :: with_capacity ( 5 ) ;
72
+ blocks. push_front ( BlockContext :: new ( ) ) ;
73
+
74
+ let modified_context = None ;
75
+ RenderContext {
76
76
partials : BTreeMap :: new ( ) ,
77
77
partial_block_stack : VecDeque :: new ( ) ,
78
78
partial_block_depth : 0 ,
@@ -84,36 +84,12 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
84
84
content_produced : false ,
85
85
indent_before_write : false ,
86
86
indent_string : None ,
87
- } ) ;
88
-
89
- let mut blocks = VecDeque :: with_capacity ( 5 ) ;
90
- blocks. push_front ( BlockContext :: new ( ) ) ;
91
-
92
- let modified_context = None ;
93
- RenderContext {
94
- inner,
95
87
blocks,
96
88
modified_context,
97
89
dev_mode_templates : None ,
98
90
}
99
91
}
100
92
101
- pub ( crate ) fn new_for_block ( & self ) -> RenderContext < ' reg , ' rc > {
102
- let inner = self . inner . clone ( ) ;
103
-
104
- let mut blocks = VecDeque :: with_capacity ( 2 ) ;
105
- blocks. push_front ( BlockContext :: new ( ) ) ;
106
-
107
- let modified_context = self . modified_context . clone ( ) ;
108
-
109
- RenderContext {
110
- inner,
111
- blocks,
112
- modified_context,
113
- dev_mode_templates : self . dev_mode_templates ,
114
- }
115
- }
116
-
117
93
/// Push a block context into render context stack. This is typically
118
94
/// called when you entering a block scope.
119
95
pub fn push_block ( & mut self , block : BlockContext < ' rc > ) {
@@ -141,14 +117,6 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
141
117
self . blocks . front_mut ( )
142
118
}
143
119
144
- fn inner ( & self ) -> & RenderContextInner < ' reg , ' rc > {
145
- self . inner . borrow ( )
146
- }
147
-
148
- fn inner_mut ( & mut self ) -> & mut RenderContextInner < ' reg , ' rc > {
149
- Rc :: make_mut ( & mut self . inner )
150
- }
151
-
152
120
/// Get the modified context data if any
153
121
pub fn context ( & self ) -> Option < Rc < Context > > {
154
122
self . modified_context . clone ( )
@@ -192,45 +160,44 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
192
160
pub fn get_partial ( & self , name : & str ) -> Option < & ' rc Template > {
193
161
if name == partial:: PARTIAL_BLOCK {
194
162
return self
195
- . inner ( )
196
163
. partial_block_stack
197
- . get ( self . inner ( ) . partial_block_depth as usize )
164
+ . get ( self . partial_block_depth as usize )
198
165
. copied ( ) ;
199
166
}
200
- self . inner ( ) . partials . get ( name) . copied ( )
167
+ self . partials . get ( name) . copied ( )
201
168
}
202
169
203
170
/// Register a partial for this context
204
171
pub fn set_partial ( & mut self , name : String , partial : & ' rc Template ) {
205
- self . inner_mut ( ) . partials . insert ( name, partial) ;
172
+ self . partials . insert ( name, partial) ;
206
173
}
207
174
208
175
pub ( crate ) fn push_partial_block ( & mut self , partial : & ' rc Template ) {
209
- self . inner_mut ( ) . partial_block_stack . push_front ( partial) ;
176
+ self . partial_block_stack . push_front ( partial) ;
210
177
}
211
178
212
179
pub ( crate ) fn pop_partial_block ( & mut self ) {
213
- self . inner_mut ( ) . partial_block_stack . pop_front ( ) ;
180
+ self . partial_block_stack . pop_front ( ) ;
214
181
}
215
182
216
183
pub ( crate ) fn inc_partial_block_depth ( & mut self ) {
217
- self . inner_mut ( ) . partial_block_depth += 1 ;
184
+ self . partial_block_depth += 1 ;
218
185
}
219
186
220
187
pub ( crate ) fn dec_partial_block_depth ( & mut self ) {
221
- let depth = & mut self . inner_mut ( ) . partial_block_depth ;
188
+ let depth = & mut self . partial_block_depth ;
222
189
if * depth > 0 {
223
190
* depth -= 1 ;
224
191
}
225
192
}
226
193
227
194
pub ( crate ) fn set_indent_string ( & mut self , indent : Option < Cow < ' rc , str > > ) {
228
- self . inner_mut ( ) . indent_string = indent;
195
+ self . indent_string = indent;
229
196
}
230
197
231
198
#[ inline]
232
199
pub ( crate ) fn get_indent_string ( & self ) -> Option < & Cow < ' rc , str > > {
233
- self . inner . indent_string . as_ref ( )
200
+ self . indent_string . as_ref ( )
234
201
}
235
202
236
203
pub ( crate ) fn get_dev_mode_template ( & self , name : & str ) -> Option < & ' rc Template > {
@@ -247,7 +214,7 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
247
214
248
215
/// Remove a registered partial
249
216
pub fn remove_partial ( & mut self , name : & str ) {
250
- self . inner_mut ( ) . partials . remove ( name) ;
217
+ self . partials . remove ( name) ;
251
218
}
252
219
253
220
fn get_local_var ( & self , level : usize , name : & str ) -> Option < & Json > {
@@ -258,7 +225,7 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
258
225
259
226
/// Test if given template name is current template.
260
227
pub fn is_current_template ( & self , p : & str ) -> bool {
261
- self . inner ( ) . current_template . is_some_and ( |s| s == p)
228
+ self . current_template . is_some_and ( |s| s == p)
262
229
}
263
230
264
231
/// Register a helper in this render context.
@@ -269,89 +236,90 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
269
236
name : & str ,
270
237
def : Box < dyn HelperDef + Send + Sync + ' rc > ,
271
238
) {
272
- self . inner_mut ( )
273
- . local_helpers
274
- . insert ( name. to_string ( ) , def. into ( ) ) ;
239
+ self . local_helpers . insert ( name. to_string ( ) , def. into ( ) ) ;
275
240
}
276
241
277
242
/// Remove a helper from render context
278
243
pub fn unregister_local_helper ( & mut self , name : & str ) {
279
- self . inner_mut ( ) . local_helpers . remove ( name) ;
244
+ self . local_helpers . remove ( name) ;
280
245
}
281
246
282
247
/// Attempt to get a helper from current render context.
283
248
pub fn get_local_helper ( & self , name : & str ) -> Option < Rc < dyn HelperDef + Send + Sync + ' rc > > {
284
- self . inner ( ) . local_helpers . get ( name) . cloned ( )
249
+ self . local_helpers . get ( name) . cloned ( )
285
250
}
286
251
287
252
#[ inline]
288
253
fn has_local_helper ( & self , name : & str ) -> bool {
289
- self . inner . local_helpers . contains_key ( name)
254
+ self . local_helpers . contains_key ( name)
290
255
}
291
256
292
257
/// Returns the current template name.
293
258
/// Note that the name can be vary from root template when you are rendering
294
259
/// from partials.
295
260
pub fn get_current_template_name ( & self ) -> Option < & ' rc String > {
296
- self . inner ( ) . current_template
261
+ self . current_template
297
262
}
298
263
299
264
/// Set the current template name.
300
265
pub fn set_current_template_name ( & mut self , name : Option < & ' rc String > ) {
301
- self . inner_mut ( ) . current_template = name;
266
+ self . current_template = name;
302
267
}
303
268
304
269
/// Get root template name if any.
305
270
/// This is the template name that you call `render` from `Handlebars`.
306
271
pub fn get_root_template_name ( & self ) -> Option < & ' reg String > {
307
- self . inner ( ) . root_template
272
+ self . root_template
308
273
}
309
274
310
275
/// Get the escape toggle
311
276
pub fn is_disable_escape ( & self ) -> bool {
312
- self . inner ( ) . disable_escape
277
+ self . disable_escape
313
278
}
314
279
315
280
/// Set the escape toggle.
316
281
/// When toggle is on, `escape_fn` will be called when rendering.
317
282
pub fn set_disable_escape ( & mut self , disable : bool ) {
318
- self . inner_mut ( ) . disable_escape = disable;
283
+ self . disable_escape = disable;
319
284
}
320
285
321
286
#[ inline]
322
287
pub fn set_trailing_newline ( & mut self , trailing_newline : bool ) {
323
- self . inner_mut ( ) . trailing_newline = trailing_newline;
288
+ self . trailing_newline = trailing_newline;
324
289
}
325
290
326
291
#[ inline]
327
292
pub fn get_trailine_newline ( & self ) -> bool {
328
- self . inner ( ) . trailing_newline
293
+ self . trailing_newline
329
294
}
330
295
331
296
#[ inline]
332
297
pub fn set_content_produced ( & mut self , content_produced : bool ) {
333
- self . inner_mut ( ) . content_produced = content_produced;
298
+ self . content_produced = content_produced;
334
299
}
335
300
336
301
#[ inline]
337
302
pub fn get_content_produced ( & self ) -> bool {
338
- self . inner ( ) . content_produced
303
+ self . content_produced
339
304
}
340
305
341
306
#[ inline]
342
307
pub fn set_indent_before_write ( & mut self , indent_before_write : bool ) {
343
- self . inner_mut ( ) . indent_before_write = indent_before_write;
308
+ self . indent_before_write = indent_before_write;
344
309
}
345
310
346
311
#[ inline]
347
312
pub fn get_indent_before_write ( & self ) -> bool {
348
- self . inner ( ) . indent_before_write
313
+ self . indent_before_write
349
314
}
350
315
}
351
316
352
- impl < ' reg , ' rc > fmt:: Debug for RenderContextInner < ' reg , ' rc > {
317
+ impl < ' reg , ' rc > fmt:: Debug for RenderContext < ' reg , ' rc > {
353
318
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> Result < ( ) , fmt:: Error > {
354
319
f. debug_struct ( "RenderContextInner" )
320
+ . field ( "dev_mode_templates" , & self . dev_mode_templates )
321
+ . field ( "blocks" , & self . blocks )
322
+ . field ( "modified_context" , & self . modified_context )
355
323
. field ( "partials" , & self . partials )
356
324
. field ( "partial_block_stack" , & self . partial_block_stack )
357
325
. field ( "partial_block_depth" , & self . partial_block_depth )
0 commit comments