Skip to content

Commit 23b7053

Browse files
committed
shift recursion to doc conversion
1 parent 328ac6d commit 23b7053

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

src/raw/document.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ use serde::{ser::SerializeMap, Deserialize, Serialize};
88
use crate::{
99
de::MIN_BSON_DOCUMENT_SIZE,
1010
raw::{error::ErrorKind, serde::OwnedOrBorrowedRawDocument, RAW_DOCUMENT_NEWTYPE},
11+
Bson,
1112
DateTime,
13+
JavaScriptCodeWithScope,
14+
RawBson,
15+
RawJavaScriptCodeWithScope,
1216
Timestamp,
1317
};
1418

@@ -540,11 +544,45 @@ impl RawDocument {
540544
/// Copy this into a [`Document`], returning an error if invalid BSON is encountered. Any
541545
/// invalid UTF-8 sequences will be replaced with the Unicode replacement character.
542546
pub fn to_document_utf8_lossy(&self) -> Result<Document> {
543-
self.iter_elements()
544-
.map(|res| {
545-
res.and_then(|e| Ok((e.key().to_owned(), e.value_utf8_lossy()?.try_into()?)))
546-
})
547-
.collect()
547+
let mut out = Document::new();
548+
for elem in self.iter_elements() {
549+
let elem = elem?;
550+
let value = deep_utf8_lossy(elem.value_utf8_lossy()?)?;
551+
out.insert(elem.key(), value);
552+
}
553+
Ok(out)
554+
}
555+
}
556+
557+
fn deep_utf8_lossy(src: RawBson) -> Result<Bson> {
558+
match src {
559+
RawBson::Array(arr) => {
560+
let mut tmp = vec![];
561+
for elem in arr.iter_elements() {
562+
tmp.push(deep_utf8_lossy(elem?.value_utf8_lossy()?)?);
563+
}
564+
Ok(Bson::Array(tmp))
565+
}
566+
RawBson::Document(doc) => {
567+
let mut tmp = doc! {};
568+
for elem in doc.iter_elements() {
569+
let elem = elem?;
570+
tmp.insert(elem.key(), deep_utf8_lossy(elem.value_utf8_lossy()?)?);
571+
}
572+
Ok(Bson::Document(tmp))
573+
}
574+
RawBson::JavaScriptCodeWithScope(RawJavaScriptCodeWithScope { code, scope }) => {
575+
let mut tmp = doc! {};
576+
for elem in scope.iter_elements() {
577+
let elem = elem?;
578+
tmp.insert(elem.key(), deep_utf8_lossy(elem.value_utf8_lossy()?)?);
579+
}
580+
Ok(Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope {
581+
code,
582+
scope: tmp,
583+
}))
584+
}
585+
v => v.try_into(),
548586
}
549587
}
550588

src/raw/iter.rs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ use crate::{
99
DateTime,
1010
Decimal128,
1111
RawArray,
12-
RawArrayBuf,
1312
RawBinaryRef,
1413
RawBson,
1514
RawDbPointerRef,
16-
RawDocumentBuf,
1715
RawJavaScriptCodeWithScopeRef,
1816
RawRegexRef,
1917
Timestamp,
@@ -270,41 +268,8 @@ impl<'a> RawElement<'a> {
270268

271269
pub fn value_utf8_lossy(&self) -> Result<RawBson> {
272270
match self.value_utf8_lossy_inner()? {
273-
Some(v) => Ok(match v {
274-
Utf8LossyBson::JavaScriptCodeWithScope(Utf8LossyJavaScriptCodeWithScope {
275-
code,
276-
scope,
277-
}) => {
278-
let mut tmp = RawDocumentBuf::new();
279-
for elem in scope.iter_elements() {
280-
let elem = elem?;
281-
tmp.append(elem.key(), elem.value_utf8_lossy()?);
282-
}
283-
RawBson::JavaScriptCodeWithScope(super::RawJavaScriptCodeWithScope {
284-
code,
285-
scope: tmp,
286-
})
287-
}
288-
v => v.into(),
289-
}),
290-
None => Ok(match self.value()? {
291-
RawBsonRef::Array(arr) => {
292-
let mut tmp = RawArrayBuf::new();
293-
for elem in arr.iter_elements() {
294-
tmp.push(elem?.value_utf8_lossy()?);
295-
}
296-
RawBson::Array(tmp)
297-
}
298-
RawBsonRef::Document(doc) => {
299-
let mut tmp = RawDocumentBuf::new();
300-
for elem in doc.iter_elements() {
301-
let elem = elem?;
302-
tmp.append(elem.key(), elem.value_utf8_lossy()?);
303-
}
304-
RawBson::Document(tmp)
305-
}
306-
v => v.to_raw_bson(),
307-
}),
271+
Some(v) => Ok(v.into()),
272+
None => Ok(self.value()?.to_raw_bson()),
308273
}
309274
}
310275

0 commit comments

Comments
 (0)