Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/raw/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,18 +498,6 @@ impl RawDocument {
let s = try_to_str(bytes)?;
s.try_into()
}

/// Copy this into a [`Document`], returning an error if invalid BSON is encountered. Any
/// invalid UTF-8 sequences will be replaced with the Unicode replacement character.
pub fn to_document_utf8_lossy(&self) -> RawResult<Document> {
let mut out = Document::new();
for elem in self.iter_elements() {
let elem = elem?;
let value = deep_utf8_lossy(elem.value_utf8_lossy()?)?;
out.insert(elem.key().as_str(), value);
}
Ok(out)
}
}

#[cfg(feature = "serde")]
Expand Down
7 changes: 4 additions & 3 deletions src/tests/modules/serializer_deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
RawDocumentBuf,
Regex,
Timestamp,
Utf8Lossy,
};
use serde_json::json;

Expand Down Expand Up @@ -74,11 +75,11 @@ fn test_encode_decode_utf8_string_invalid() {
doc.to_writer(&mut buf).unwrap();

let expected = doc! { "key": "��", "subdoc": { "subkey": "��" } };
let decoded = RawDocumentBuf::from_reader(&mut Cursor::new(buf))
let decoded: Utf8Lossy<Document> = RawDocumentBuf::from_reader(&mut Cursor::new(buf))
.unwrap()
.to_document_utf8_lossy()
.try_into()
.unwrap();
assert_eq!(decoded, expected);
assert_eq!(decoded.0, expected);
}

#[test]
Expand Down
18 changes: 9 additions & 9 deletions src/tests/spec/corpus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,15 +562,15 @@ fn run_test(test: TestFile) {
.expect_err(description.as_str());

if decode_error.description.contains("invalid UTF-8") {
RawDocumentBuf::from_reader(bson.as_slice())
.expect(&description)
.to_document_utf8_lossy()
.unwrap_or_else(|err| {
panic!(
"{}: utf8_lossy should not fail (failed with {:?})",
description, err
)
});
Utf8Lossy::<Document>::try_from(
RawDocumentBuf::from_reader(bson.as_slice()).expect(&description),
)
.unwrap_or_else(|err| {
panic!(
"{}: utf8_lossy should not fail (failed with {:?})",
description, err
)
});
crate::deserialize_from_slice::<Utf8Lossy<Document>>(bson.as_slice())
.expect(&description);
}
Expand Down