Skip to content

Commit bafab41

Browse files
zaneduffieldjgardn3r
authored andcommitted
Return error when replacement characters are required to encode output
1 parent 5e49f0d commit bafab41

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Missing `winapi` keyword.
2323
- Erroneous removal of BOM.
2424
- Erroneous transcoding of UTF-16 input to UTF-8.
25+
- Unwanted use of the replacement character when encoding. This now results in an error.
2526

2627
## [0.2.0] - 2024-05-07
2728

orchestrator/src/file_formatter.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,18 @@ impl FileFormatter {
191191
fn encode<'a>(encoding: &'static Encoding, data: &'a str) -> io::Result<Cow<'a, [u8]>> {
192192
// encoding_rs doesn't support encoding to UTF16, so we do it ourselves.
193193
if encoding.output_encoding() == encoding {
194-
Ok(encoding.encode(data).0)
194+
let (encoded_data, _encoding_used, replacements) = encoding.encode(data);
195+
if replacements {
196+
Err(io::Error::new(
197+
io::ErrorKind::InvalidData,
198+
format!(
199+
"Formatting result contains data that cannot be encoded as {}",
200+
encoding.name()
201+
),
202+
))
203+
} else {
204+
Ok(encoded_data)
205+
}
195206
} else if encoding == encoding_rs::UTF_16BE {
196207
Ok(Cow::Owned(Self::encode_utf16be(data)))
197208
} else if encoding == encoding_rs::UTF_16LE {

0 commit comments

Comments
 (0)