@@ -361,10 +361,10 @@ impl<'a, W: Write> ElementWriter<'a, W> {
361
361
let mut serializer = Serializer :: new ( ToFmtWrite ( self . writer . inner ( ) ) ) ;
362
362
363
363
if let Some ( indent) = indent {
364
- serializer. indent_with_len (
364
+ serializer. indent (
365
365
indent. indent_char as char ,
366
366
indent. indent_size ,
367
- indent. indents_len ,
367
+ indent. current_indent_len ,
368
368
) ;
369
369
}
370
370
@@ -391,11 +391,16 @@ where
391
391
392
392
#[ derive( Clone ) ]
393
393
pub ( crate ) struct Indentation {
394
+ /// todo: does this even belong here? It has no impact on indentation logic.
394
395
should_line_break : bool ,
396
+ /// The character code to be used for indentations (e.g. ` ` or `\t`)
395
397
indent_char : u8 ,
398
+ /// How many instances of the indent character ought to be used for each level of indentation
396
399
indent_size : usize ,
400
+ /// Used as a cache for the bytes used for indentation
397
401
indents : Vec < u8 > ,
398
- indents_len : usize ,
402
+ /// The current amount of indentation - must be less than indents.len()
403
+ current_indent_len : usize ,
399
404
}
400
405
401
406
impl Indentation {
@@ -405,26 +410,28 @@ impl Indentation {
405
410
indent_char,
406
411
indent_size,
407
412
indents : vec ! [ indent_char; 128 ] ,
408
- indents_len,
413
+ current_indent_len : indents_len,
409
414
}
410
415
}
411
416
412
417
/// Increase indentation by one level
413
418
pub fn grow ( & mut self ) {
414
- self . indents_len += self . indent_size ;
415
- if self . indents_len > self . indents . len ( ) {
416
- self . indents . resize ( self . indents_len , self . indent_char ) ;
419
+ self . current_indent_len += self . indent_size ;
420
+ if self . current_indent_len > self . indents . len ( ) {
421
+ self . indents
422
+ . resize ( self . current_indent_len , self . indent_char ) ;
417
423
}
418
424
}
419
425
420
426
/// Decrease indentation by one level. Do nothing, if level already zero
421
427
pub fn shrink ( & mut self ) {
422
- self . indents_len = self . indents_len . saturating_sub ( self . indent_size ) ;
428
+ self . current_indent_len = self . current_indent_len . saturating_sub ( self . indent_size ) ;
423
429
}
424
430
425
431
/// Returns indent string for current level
426
432
pub fn current ( & self ) -> & [ u8 ] {
427
- & self . indents [ ..self . indents_len ]
433
+ // todo: might be buggy if initialized > 128 without a call to grow() first
434
+ & self . indents [ ..self . current_indent_len ]
428
435
}
429
436
}
430
437
0 commit comments