@@ -57,26 +57,26 @@ pub struct StyleConfiguration {
57
57
pub ( crate ) indent : Option < usize > ,
58
58
pub ( crate ) item_separator : String ,
59
59
pub ( crate ) key_separator : String ,
60
- pub ( crate ) current_indent : usize ,
60
+ pub ( crate ) current_indent : String ,
61
61
pub ( crate ) trailing_comma : TrailingComma
62
62
}
63
63
64
64
#[ allow( dead_code) ]
65
65
impl StyleConfiguration {
66
66
pub fn new ( indent : Option < usize > , item_separator : & str , key_separator : & str , trailing_comma : TrailingComma ) -> Self {
67
- StyleConfiguration { indent : indent, item_separator : item_separator. to_string ( ) , key_separator : key_separator. to_string ( ) , current_indent : 0 , trailing_comma : trailing_comma}
67
+ StyleConfiguration { indent : indent, item_separator : item_separator. to_string ( ) , key_separator : key_separator. to_string ( ) , current_indent : String :: with_capacity ( 64 ) , trailing_comma : trailing_comma}
68
68
}
69
69
70
70
pub fn with_indent ( indent : usize , trailing_comma : TrailingComma ) -> Self {
71
- StyleConfiguration { indent : Some ( indent) , item_separator : "," . to_string ( ) , key_separator : ": " . to_string ( ) , trailing_comma, current_indent : 0 }
71
+ StyleConfiguration { indent : Some ( indent) , item_separator : "," . to_string ( ) , key_separator : ": " . to_string ( ) , trailing_comma, current_indent : String :: with_capacity ( 64 ) }
72
72
}
73
73
74
74
pub fn with_separators ( item_separator : & str , key_separator : & str , trailing_comma : TrailingComma ) -> Self {
75
- StyleConfiguration { indent : Some ( 0 ) , key_separator : key_separator. to_string ( ) , trailing_comma, item_separator : item_separator. to_string ( ) , current_indent : 0 }
75
+ StyleConfiguration { indent : Some ( 0 ) , key_separator : key_separator. to_string ( ) , trailing_comma, item_separator : item_separator. to_string ( ) , current_indent : String :: with_capacity ( 64 ) }
76
76
}
77
77
78
78
pub fn default ( ) -> Self {
79
- StyleConfiguration { indent : None , item_separator : ", " . to_string ( ) , key_separator : ": " . to_string ( ) , current_indent : 0 , trailing_comma : TrailingComma :: NONE }
79
+ StyleConfiguration { indent : None , item_separator : ", " . to_string ( ) , key_separator : ": " . to_string ( ) , current_indent : String :: with_capacity ( 64 ) , trailing_comma : TrailingComma :: NONE }
80
80
81
81
}
82
82
}
@@ -123,7 +123,10 @@ impl<'input> JSONValue<'input> {
123
123
ret = String :: from ( "{" ) ;
124
124
}
125
125
Some ( ident) => {
126
- style. current_indent += ident;
126
+ style. current_indent . reserve ( ident) ;
127
+ for _ in 0 .. ident {
128
+ style. current_indent . push ( ' ' ) ;
129
+ }
127
130
ret = format ! ( "{{\n {}" , style. current_indent) ;
128
131
}
129
132
}
@@ -151,7 +154,7 @@ impl<'input> JSONValue<'input> {
151
154
ret. push_str ( "}" ) ;
152
155
}
153
156
Some ( ident) => {
154
- style. current_indent -= ident;
157
+ style. current_indent . truncate ( style . current_indent . len ( ) - ident) ;
155
158
ret. push_str ( format ! ( "\n {}}}" , style. current_indent) . as_str ( ) ) ;
156
159
}
157
160
}
@@ -165,7 +168,10 @@ impl<'input> JSONValue<'input> {
165
168
ret = String :: from ( "[" ) ;
166
169
}
167
170
Some ( ident) => {
168
- style. current_indent += ident;
171
+ style. current_indent . reserve ( ident) ;
172
+ for _ in 0 .. ident {
173
+ style. current_indent . push ( ' ' ) ;
174
+ }
169
175
ret = format ! ( "{{\n {}" , style. current_indent) ;
170
176
}
171
177
}
@@ -193,7 +199,7 @@ impl<'input> JSONValue<'input> {
193
199
ret. push_str ( "]" ) ;
194
200
}
195
201
Some ( ident) => {
196
- style. current_indent -= ident;
202
+ style. current_indent . truncate ( style . current_indent . len ( ) - ident) ;
197
203
ret. push_str ( format ! ( "\n {}}}" , style. current_indent) . as_str ( ) ) ;
198
204
}
199
205
}
0 commit comments