@@ -18,7 +18,6 @@ struct DecodedFile<'a> {
18
18
bom : Option < & ' a [ u8 ] > ,
19
19
contents : Cow < ' a , str > ,
20
20
encoding : & ' static Encoding ,
21
- replacements : bool ,
22
21
}
23
22
24
23
pub struct FileFormatter {
@@ -75,8 +74,9 @@ impl FileFormatter {
75
74
fn decode_file < ' a > (
76
75
& self ,
77
76
file : & mut impl Read ,
77
+ name : impl Display ,
78
78
buf : & ' a mut Vec < u8 > ,
79
- ) -> std :: io :: Result < DecodedFile < ' a > > {
79
+ ) -> anyhow :: Result < DecodedFile < ' a > > {
80
80
file. read_to_end ( buf) ?;
81
81
82
82
let ( encoding, ( bom, contents) ) = match Encoding :: for_bom ( buf) {
@@ -87,11 +87,19 @@ impl FileFormatter {
87
87
} ;
88
88
let ( contents, replacements) = encoding. decode_without_bom_handling ( contents) ;
89
89
90
+ if replacements {
91
+ bail ! (
92
+ "File '{}' has malformed sequences (in encoding '{}'{})" ,
93
+ name,
94
+ encoding. name( ) ,
95
+ bom. map( |_| " - inferred from BOM" ) . unwrap_or( "" ) ,
96
+ ) ;
97
+ }
98
+
90
99
Ok ( DecodedFile {
91
100
bom,
92
101
contents,
93
102
encoding,
94
- replacements,
95
103
} )
96
104
}
97
105
@@ -123,17 +131,9 @@ impl FileFormatter {
123
131
. with_context ( || format ! ( "Failed to open '{}'" , file_path. display( ) ) ) ?;
124
132
125
133
let decoded_file = self
126
- . decode_file ( & mut file, input_buf)
134
+ . decode_file ( & mut file, file_path . display ( ) , input_buf)
127
135
. with_context ( || format ! ( "Failed to read '{}'" , file_path. display( ) ) ) ?;
128
136
129
- if decoded_file. replacements {
130
- bail ! (
131
- "File '{}' had malformed sequences (in encoding '{}')" ,
132
- file_path. display( ) ,
133
- decoded_file. encoding. name( )
134
- ) ;
135
- }
136
-
137
137
self . formatter
138
138
. format_into_buf ( & decoded_file. contents , output_buf) ;
139
139
result_operation ( & mut file, & file_path, & decoded_file, output_buf)
@@ -239,7 +239,7 @@ impl FileFormatter {
239
239
}
240
240
let mut stdin = std:: io:: stdin ( ) . lock ( ) ;
241
241
242
- self . decode_file ( & mut stdin, buf)
242
+ self . decode_file ( & mut stdin, "<stdin>" , buf)
243
243
. context ( "Failed to read from stdin" )
244
244
}
245
245
0 commit comments