Skip to content

Commit cebaffe

Browse files
committed
initial slash
1 parent d1aaa8e commit cebaffe

File tree

11 files changed

+115
-78
lines changed

11 files changed

+115
-78
lines changed

src/datetime.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,6 @@ impl crate::DateTime {
386386
}
387387
}
388388

389-
#[deprecated(since = "2.3.0", note = "Use try_to_rfc3339_string instead.")]
390-
/// Convert this [`DateTime`] to an RFC 3339 formatted string. Panics if it could not be
391-
/// represented in that format.
392-
pub fn to_rfc3339_string(self) -> String {
393-
self.try_to_rfc3339_string().unwrap()
394-
}
395-
396389
/// Convert this [`DateTime`] to an RFC 3339 formatted string.
397390
pub fn try_to_rfc3339_string(self) -> Result<String> {
398391
self.to_time_0_3()

src/de.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -137,40 +137,12 @@ where
137137
from_slice(bytes.as_slice())
138138
}
139139

140-
/// Deserialize an instance of type `T` from an I/O stream of BSON, replacing any invalid UTF-8
141-
/// sequences with the Unicode replacement character.
142-
///
143-
/// This is mainly useful when reading raw BSON returned from a MongoDB server, which
144-
/// in rare cases can contain invalidly truncated strings (<https://jira.mongodb.org/browse/SERVER-24007>).
145-
/// For most use cases, [`crate::from_reader`] can be used instead.
146-
pub fn from_reader_utf8_lossy<R, T>(reader: R) -> Result<T>
147-
where
148-
T: DeserializeOwned,
149-
R: Read,
150-
{
151-
let bytes = reader_to_vec(reader)?;
152-
from_slice_utf8_lossy(bytes.as_slice())
153-
}
154-
155140
/// Deserialize an instance of type `T` from a slice of BSON bytes.
156141
pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T>
157142
where
158143
T: Deserialize<'de>,
159144
{
160-
from_raw(raw::Deserializer::new(bytes, false)?)
161-
}
162-
163-
/// Deserialize an instance of type `T` from a slice of BSON bytes, replacing any invalid UTF-8
164-
/// sequences with the Unicode replacement character.
165-
///
166-
/// This is mainly useful when reading raw BSON returned from a MongoDB server, which
167-
/// in rare cases can contain invalidly truncated strings (<https://jira.mongodb.org/browse/SERVER-24007>).
168-
/// For most use cases, [`crate::from_slice`] can be used instead.
169-
pub fn from_slice_utf8_lossy<'de, T>(bytes: &'de [u8]) -> Result<T>
170-
where
171-
T: Deserialize<'de>,
172-
{
173-
from_raw(raw::Deserializer::new(bytes, true)?)
145+
from_raw(raw::Deserializer::new(bytes)?)
174146
}
175147

176148
pub(crate) fn from_raw<'de, T: Deserialize<'de>>(

src/de/raw.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ struct DeserializerOptions {
5050
}
5151

5252
impl<'de> Deserializer<'de> {
53-
pub(crate) fn new(buf: &'de [u8], utf8_lossy: bool) -> Result<Self> {
53+
pub(crate) fn new(buf: &'de [u8]) -> Result<Self> {
5454
Ok(Self {
5555
element: RawElement::toplevel(buf)?,
5656
options: DeserializerOptions {
57-
utf8_lossy,
57+
utf8_lossy: false,
5858
human_readable: false,
5959
},
6060
})

src/document.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,9 @@ impl Document {
695695
Ok(())
696696
}
697697

698-
fn decode<R: Read + ?Sized>(reader: &mut R, utf_lossy: bool) -> crate::de::Result<Document> {
698+
fn decode<R: Read + ?Sized>(reader: &mut R) -> crate::de::Result<Document> {
699699
let buf = crate::de::reader_to_vec(reader)?;
700-
crate::de::from_raw(crate::de::RawDeserializer::new(&buf, utf_lossy)?)
700+
crate::de::from_raw(crate::de::RawDeserializer::new(&buf)?)
701701
}
702702

703703
/// Attempts to deserialize a [`Document`] from a byte stream.
@@ -729,18 +729,7 @@ impl Document {
729729
/// # }
730730
/// ```
731731
pub fn from_reader<R: Read>(mut reader: R) -> crate::de::Result<Document> {
732-
Self::decode(&mut reader, false)
733-
}
734-
735-
/// Attempt to deserialize a [`Document`] that may contain invalid UTF-8 strings from a byte
736-
/// stream.
737-
///
738-
/// This is mainly useful when reading raw BSON returned from a MongoDB server, which
739-
/// in rare cases can contain invalidly truncated strings (<https://jira.mongodb.org/browse/SERVER-24007>).
740-
/// For most use cases, `Document::from_reader` can be used instead.
741-
#[deprecated = "use bson::serde_helpers::Utf8LossyDeserialization"]
742-
pub fn from_reader_utf8_lossy<R: Read>(mut reader: R) -> crate::de::Result<Document> {
743-
Self::decode(&mut reader, true)
732+
Self::decode(&mut reader)
744733
}
745734
}
746735

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,6 @@ pub use self::{
308308
uuid::{Uuid, UuidRepresentation},
309309
};
310310

311-
#[allow(deprecated)]
312-
pub use self::de::{from_reader_utf8_lossy, from_slice_utf8_lossy};
313-
314311
#[macro_use]
315312
mod macros;
316313
mod base64;

src/raw/document.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,17 @@ impl RawDocument {
523523
let bytes = self.cstring_bytes_at(start_at)?;
524524
try_to_str(bytes)
525525
}
526+
527+
/// Copy this into a [`Document`], returning an error if invalid BSON is encountered.
528+
pub fn to_document(&self) -> Result<Document> {
529+
self.as_ref().try_into()
530+
}
531+
532+
/// Copy this into a [`Document`], returning an error if invalid BSON is encountered. Any
533+
/// invalid UTF-8 sequences will be replaced with the Unicode replacement character.
534+
pub fn to_document_utf8_lossy(&self) -> Result<Document> {
535+
todo!()
536+
}
526537
}
527538

528539
impl<'de: 'a, 'a> Deserialize<'de> for &'a RawDocument {

src/raw/document_buf.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
borrow::{Borrow, Cow},
3-
convert::{TryFrom, TryInto},
3+
convert::TryFrom,
44
iter::FromIterator,
55
ops::Deref,
66
};
@@ -66,7 +66,7 @@ pub struct RawDocumentBuf {
6666

6767
impl RawDocumentBuf {
6868
/// Creates a new, empty [`RawDocumentBuf`].
69-
pub fn new() -> RawDocumentBuf {
69+
pub fn new() -> Self {
7070
let mut data = Vec::new();
7171
data.extend(MIN_BSON_DOCUMENT_SIZE.to_le_bytes());
7272
data.push(0);
@@ -90,11 +90,16 @@ impl RawDocumentBuf {
9090
/// let doc = RawDocumentBuf::from_bytes(b"\x05\0\0\0\0".to_vec())?;
9191
/// # Ok::<(), Error>(())
9292
/// ```
93-
pub fn from_bytes(data: Vec<u8>) -> Result<RawDocumentBuf> {
93+
pub fn from_bytes(data: Vec<u8>) -> Result<Self> {
9494
let _ = RawDocument::from_bytes(data.as_slice())?;
9595
Ok(Self { data })
9696
}
9797

98+
pub fn from_reader<R: std::io::Read>(reader: R) -> Result<Self> {
99+
let buf = crate::de::reader_to_vec(reader)?;
100+
Self::from_bytes(buf)
101+
}
102+
98103
/// Create a [`RawDocumentBuf`] from a [`Document`].
99104
///
100105
/// ```
@@ -222,12 +227,6 @@ impl RawDocumentBuf {
222227
.expect("key should not contain interior null byte")
223228
})
224229
}
225-
226-
/// Convert this [`RawDocumentBuf`] to a [`Document`], returning an error
227-
/// if invalid BSON is encountered.
228-
pub fn to_document(&self) -> Result<Document> {
229-
self.as_ref().try_into()
230-
}
231230
}
232231

233232
impl Default for RawDocumentBuf {

src/raw/error.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Error {
3232
}
3333

3434
/// The different categories of errors that can be returned when reading from raw BSON.
35-
#[derive(Clone, Debug, PartialEq)]
35+
#[derive(Clone, Debug)]
3636
#[non_exhaustive]
3737
pub enum ErrorKind {
3838
/// A BSON value did not fit the proper format.
@@ -41,6 +41,24 @@ pub enum ErrorKind {
4141

4242
/// Improper UTF-8 bytes were found when proper UTF-8 was expected.
4343
Utf8EncodingError,
44+
45+
/// A wrapped deserialization error.
46+
/// TODO RUST-1406: collapse this
47+
DeError(crate::de::Error),
48+
}
49+
50+
impl PartialEq for ErrorKind {
51+
fn eq(&self, other: &Self) -> bool {
52+
match (self, other) {
53+
(
54+
Self::MalformedValue { message: l_message },
55+
Self::MalformedValue { message: r_message },
56+
) => l_message == r_message,
57+
(Self::Utf8EncodingError, Self::Utf8EncodingError) => true,
58+
(Self::DeError(_), Self::DeError(_)) => true,
59+
_ => false,
60+
}
61+
}
4462
}
4563

4664
impl std::fmt::Display for Error {
@@ -57,10 +75,17 @@ impl std::fmt::Display for Error {
5775
write!(f, "{}malformed value: {:?}", prefix, message)
5876
}
5977
ErrorKind::Utf8EncodingError => write!(f, "{}utf-8 encoding error", prefix),
78+
ErrorKind::DeError(e) => write!(f, "{}deserialization error: {}", prefix, e),
6079
}
6180
}
6281
}
6382

83+
impl From<crate::de::Error> for Error {
84+
fn from(value: crate::de::Error) -> Self {
85+
Self::new(ErrorKind::DeError(value))
86+
}
87+
}
88+
6489
impl std::error::Error for Error {}
6590

6691
pub type Result<T> = std::result::Result<T, Error>;

src/serde_helpers.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,8 @@ where
817817
///
818818
/// This wrapper type has no impact on serialization. Serializing a `Utf8LossyDeserialization<T>`
819819
/// will call the `serialize` method for the wrapped `T`.
820-
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
820+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Default)]
821+
#[repr(transparent)]
821822
pub struct Utf8LossyDeserialization<T>(pub T);
822823

823824
pub(crate) const UTF8_LOSSY_NEWTYPE: &str = "$__bson_private_utf8_lossy";
@@ -852,3 +853,48 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Utf8LossyDeserialization<T>
852853
deserializer.deserialize_newtype_struct(UTF8_LOSSY_NEWTYPE, V(PhantomData))
853854
}
854855
}
856+
857+
impl<T: std::fmt::Display> std::fmt::Display for Utf8LossyDeserialization<T> {
858+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
859+
self.0.fmt(f)
860+
}
861+
}
862+
863+
impl<T> From<T> for Utf8LossyDeserialization<T> {
864+
fn from(value: T) -> Self {
865+
Self(value)
866+
}
867+
}
868+
869+
impl<T> Deref for Utf8LossyDeserialization<T> {
870+
type Target = T;
871+
872+
fn deref(&self) -> &Self::Target {
873+
&self.0
874+
}
875+
}
876+
877+
impl<T> DerefMut for Utf8LossyDeserialization<T> {
878+
fn deref_mut(&mut self) -> &mut Self::Target {
879+
&mut self.0
880+
}
881+
}
882+
883+
impl<T, R> AsRef<R> for Utf8LossyDeserialization<T>
884+
where
885+
R: ?Sized,
886+
<Utf8LossyDeserialization<T> as Deref>::Target: AsRef<R>,
887+
{
888+
fn as_ref(&self) -> &R {
889+
self.deref().as_ref()
890+
}
891+
}
892+
893+
impl<T, R: ?Sized> AsMut<R> for Utf8LossyDeserialization<T>
894+
where
895+
<Utf8LossyDeserialization<T> as Deref>::Target: AsMut<R>,
896+
{
897+
fn as_mut(&mut self) -> &mut R {
898+
self.deref_mut().as_mut()
899+
}
900+
}

src/tests/modules/serializer_deserializer.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
Decimal128,
1919
Document,
2020
JavaScriptCodeWithScope,
21+
RawDocumentBuf,
2122
Regex,
2223
Timestamp,
2324
};
@@ -73,8 +74,10 @@ fn test_encode_decode_utf8_string_invalid() {
7374
doc.to_writer(&mut buf).unwrap();
7475

7576
let expected = doc! { "key": "��" };
76-
#[allow(deprecated)]
77-
let decoded = Document::from_reader_utf8_lossy(&mut Cursor::new(buf)).unwrap();
77+
let decoded = RawDocumentBuf::from_reader(&mut Cursor::new(buf))
78+
.unwrap()
79+
.to_document_utf8_lossy()
80+
.unwrap();
7881
assert_eq!(decoded, expected);
7982
}
8083

0 commit comments

Comments
 (0)