From 2be98febd935d942a94013ecd0375d0c0db95ab0 Mon Sep 17 00:00:00 2001 From: Asger Hautop Drewsen Date: Tue, 20 Aug 2024 15:32:20 +0200 Subject: [PATCH] Add deserialize-utf8-lossy feature to always deserialize using lossy UTF-8 conversion --- Cargo.toml | 2 ++ src/change_stream.rs | 2 +- src/change_stream/session.rs | 2 +- src/cursor.rs | 2 +- src/cursor/common.rs | 2 +- src/cursor/session.rs | 2 +- src/lib.rs | 10 ++++++++++ 7 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bc1a979b1..8c2471ab2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,6 +61,8 @@ snappy-compression = ["dep:snap"] # The In Use Encryption API is unstable and may have backwards-incompatible changes in minor version updates. in-use-encryption-unstable = ["dep:mongocrypt", "dep:rayon", "dep:num_cpus"] +deserialize-utf8-lossy = [] + # Enables support for emitting tracing events. # The tracing API is unstable and may have backwards-incompatible changes in minor version updates. # TODO: pending https://github.com/tokio-rs/tracing/issues/2036 stop depending directly on log. diff --git a/src/change_stream.rs b/src/change_stream.rs index 86c2bb0b7..1e8fab534 100644 --- a/src/change_stream.rs +++ b/src/change_stream.rs @@ -159,7 +159,7 @@ where /// ``` pub async fn next_if_any(&mut self) -> Result> { Ok(match NextInBatchFuture::new(self).await? { - BatchValue::Some { doc, .. } => Some(bson::from_slice(doc.as_bytes())?), + BatchValue::Some { doc, .. } => Some(crate::de::from_slice(doc.as_bytes())?), BatchValue::Empty | BatchValue::Exhausted => None, }) } diff --git a/src/change_stream/session.rs b/src/change_stream/session.rs index 0af9e2fa4..fda3c2282 100644 --- a/src/change_stream/session.rs +++ b/src/change_stream/session.rs @@ -149,7 +149,7 @@ where match bv { BatchValue::Some { doc, .. } => { self.data.document_returned = true; - return Ok(Some(bson::from_slice(doc.as_bytes())?)); + return Ok(Some(crate::de::from_slice(doc.as_bytes())?)); } BatchValue::Empty | BatchValue::Exhausted => return Ok(None), } diff --git a/src/cursor.rs b/src/cursor.rs index d22474441..8df5b305a 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -270,7 +270,7 @@ impl Cursor { where T: Deserialize<'a>, { - bson::from_slice(self.current().as_bytes()).map_err(Error::from) + crate::de::from_slice(self.current().as_bytes()).map_err(Error::from) } /// Update the type streamed values will be parsed as. diff --git a/src/cursor/common.rs b/src/cursor/common.rs index 4c8bc3b5a..30e03ef62 100644 --- a/src/cursor/common.rs +++ b/src/cursor/common.rs @@ -278,7 +278,7 @@ where Poll::Pending => return Poll::Pending, Poll::Ready(bv) => match bv? { BatchValue::Some { doc, .. } => { - return Poll::Ready(Some(Ok(bson::from_slice(doc.as_bytes())?))) + return Poll::Ready(Some(Ok(crate::de::from_slice(doc.as_bytes())?))) } BatchValue::Empty => continue, BatchValue::Exhausted => return Poll::Ready(None), diff --git a/src/cursor/session.rs b/src/cursor/session.rs index 6f460ac63..404603758 100644 --- a/src/cursor/session.rs +++ b/src/cursor/session.rs @@ -306,7 +306,7 @@ impl SessionCursor { where T: Deserialize<'a>, { - bson::from_slice(self.current().as_bytes()).map_err(Error::from) + crate::de::from_slice(self.current().as_bytes()).map_err(Error::from) } /// Update the type streamed values will be parsed as. diff --git a/src/lib.rs b/src/lib.rs index 3ae64b154..e4f33ba06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,3 +81,13 @@ compile_error!( "The feature 'compat-3-0-0' must be enabled to ensure forward compatibility with future \ versions of this crate." ); + +#[cfg(not(feature = "deserialize-utf8-lossy"))] +pub(crate) mod de { + pub(crate) use ::bson::from_slice; +} + +#[cfg(feature = "deserialize-utf8-lossy")] +pub(crate) mod de { + pub(crate) use ::bson::from_slice_utf8_lossy as from_slice; +}