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 @@ -10,6 +10,7 @@ use crate::{
1010 error:: { Error , Result } ,
1111 raw:: { error:: ErrorKind , serde:: OwnedOrBorrowedRawDocument , RAW_DOCUMENT_NEWTYPE } ,
1212 DateTime ,
13+ RawBson ,
1314 Timestamp ,
1415} ;
1516
@@ -526,13 +527,24 @@ impl RawDocument {
526527
527528 /// Copy this into a [`Document`], returning an error if invalid BSON is encountered.
528529 pub fn to_document ( & self ) -> Result < Document > {
529- self . as_ref ( ) . try_into ( )
530+ self . try_into ( )
530531 }
531532
532533 /// Copy this into a [`Document`], returning an error if invalid BSON is encountered. Any
533534 /// invalid UTF-8 sequences will be replaced with the Unicode replacement character.
534535 pub fn to_document_utf8_lossy ( & self ) -> Result < Document > {
535- todo ! ( )
536+ self . iter_elements ( )
537+ . map ( |res| {
538+ res. and_then ( |e| {
539+ let key = e. key ( ) . to_owned ( ) ;
540+ let raw_value: RawBson = match e. value_utf8_lossy ( ) ? {
541+ Some ( l) => l. into ( ) ,
542+ None => e. value ( ) ?. to_raw_bson ( ) ,
543+ } ;
544+ Ok ( ( key, raw_value. try_into ( ) ?) )
545+ } )
546+ } )
547+ . collect ( )
536548 }
537549}
538550
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