File tree Expand file tree Collapse file tree 2 files changed +33
-2
lines changed
Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ use crate::{
99 de:: MIN_BSON_DOCUMENT_SIZE ,
1010 raw:: { error:: ErrorKind , serde:: OwnedOrBorrowedRawDocument , RAW_DOCUMENT_NEWTYPE } ,
1111 DateTime ,
12+ RawBson ,
1213 Timestamp ,
1314} ;
1415
@@ -534,13 +535,24 @@ impl RawDocument {
534535
535536 /// Copy this into a [`Document`], returning an error if invalid BSON is encountered.
536537 pub fn to_document ( & self ) -> Result < Document > {
537- self . as_ref ( ) . try_into ( )
538+ self . try_into ( )
538539 }
539540
540541 /// Copy this into a [`Document`], returning an error if invalid BSON is encountered. Any
541542 /// invalid UTF-8 sequences will be replaced with the Unicode replacement character.
542543 pub fn to_document_utf8_lossy ( & self ) -> Result < Document > {
543- todo ! ( )
544+ self . iter_elements ( )
545+ . map ( |res| {
546+ res. and_then ( |e| {
547+ let key = e. key ( ) . to_owned ( ) ;
548+ let raw_value: RawBson = match e. value_utf8_lossy ( ) ? {
549+ Some ( l) => l. into ( ) ,
550+ None => e. value ( ) ?. to_raw_bson ( ) ,
551+ } ;
552+ Ok ( ( key, raw_value. try_into ( ) ?) )
553+ } )
554+ } )
555+ . collect ( )
544556 }
545557}
546558
Original file line number Diff line number Diff line change @@ -455,3 +455,22 @@ pub(crate) struct Utf8LossyJavaScriptCodeWithScope<'a> {
455455 pub ( crate ) code : String ,
456456 pub ( crate ) scope : & ' a RawDocument ,
457457}
458+
459+ impl < ' a > From < Utf8LossyBson < ' a > > for RawBson {
460+ fn from ( value : Utf8LossyBson < ' a > ) -> Self {
461+ match value {
462+ Utf8LossyBson :: String ( s) => RawBson :: String ( s) ,
463+ Utf8LossyBson :: JavaScriptCode ( s) => RawBson :: JavaScriptCode ( s) ,
464+ Utf8LossyBson :: JavaScriptCodeWithScope ( Utf8LossyJavaScriptCodeWithScope {
465+ code,
466+ scope,
467+ } ) => RawBson :: JavaScriptCodeWithScope ( super :: RawJavaScriptCodeWithScope {
468+ code,
469+ scope : scope. to_raw_document_buf ( ) ,
470+ } ) ,
471+ Utf8LossyBson :: Symbol ( s) => RawBson :: Symbol ( s) ,
472+ Utf8LossyBson :: DbPointer ( p) => RawBson :: DbPointer ( p) ,
473+ Utf8LossyBson :: RegularExpression ( r) => RawBson :: RegularExpression ( r) ,
474+ }
475+ }
476+ }
You can’t perform that action at this time.
0 commit comments