@@ -31,13 +31,10 @@ where
31
31
T : Write ,
32
32
{
33
33
fn write_str ( & mut self , s : & str ) -> fmt:: Result {
34
- let mut first = true ;
35
- for line in s. split ( '\n' ) {
36
- if !first {
37
- write ! ( self . 0 , "\n {INDENT}" ) ?;
38
- }
39
- self . 0 . write_str ( line) ?;
40
- first = false ;
34
+ self . 0 . write_str ( s) ?;
35
+ // Our NewLine and SpaceOrNewline utils always print individual newlines as a single-character string.
36
+ if s == "\n " {
37
+ self . 0 . write_str ( INDENT ) ?;
41
38
}
42
39
Ok ( ( ) )
43
40
}
@@ -98,36 +95,24 @@ pub(crate) fn indented_list<T: fmt::Display>(f: &mut fmt::Formatter, slice: &[T]
98
95
mod tests {
99
96
use super :: * ;
100
97
101
- struct DisplayCharByChar < T : Display > ( T ) ;
98
+ #[ test]
99
+ fn test_indent ( ) {
100
+ struct TwoLines ;
102
101
103
- impl < T : Display > Display for DisplayCharByChar < T > {
104
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
105
- for c in self . 0 . to_string ( ) . chars ( ) {
106
- write ! ( f, "{}" , c) ?;
102
+ impl Display for TwoLines {
103
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
104
+ f. write_str ( "line 1" ) ?;
105
+ SpaceOrNewline . fmt ( f) ?;
106
+ f. write_str ( "line 2" )
107
107
}
108
- Ok ( ( ) )
109
108
}
110
- }
111
109
112
- #[ test]
113
- fn test_indent ( ) {
114
- let original = "line 1\n line 2" ;
115
- let indent = Indent ( original) ;
110
+ let indent = Indent ( TwoLines ) ;
116
111
assert_eq ! (
117
112
indent. to_string( ) ,
118
- original ,
113
+ TwoLines . to_string ( ) ,
119
114
"Only the alternate form should be indented"
120
115
) ;
121
- let expected = " line 1\n line 2" ;
122
- assert_eq ! ( format!( "{:#}" , indent) , expected) ;
123
- let display_char_by_char = DisplayCharByChar ( original) ;
124
- assert_eq ! ( format!( "{:#}" , Indent ( display_char_by_char) ) , expected) ;
125
- }
126
-
127
- #[ test]
128
- fn test_space_or_newline ( ) {
129
- let space_or_newline = SpaceOrNewline ;
130
- assert_eq ! ( format!( "{}" , space_or_newline) , " " ) ;
131
- assert_eq ! ( format!( "{:#}" , space_or_newline) , "\n " ) ;
116
+ assert_eq ! ( format!( "{:#}" , indent) , " line 1\n line 2" ) ;
132
117
}
133
118
}
0 commit comments