Skip to content

Commit 6872c0d

Browse files
committed
initial slash
1 parent 9844287 commit 6872c0d

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
@@ -598,9 +598,9 @@ impl Document {
598598
Ok(())
599599
}
600600

601-
fn decode<R: Read + ?Sized>(reader: &mut R, utf_lossy: bool) -> crate::de::Result<Document> {
601+
fn decode<R: Read + ?Sized>(reader: &mut R) -> crate::de::Result<Document> {
602602
let buf = crate::de::reader_to_vec(reader)?;
603-
crate::de::from_raw(crate::de::RawDeserializer::new(&buf, utf_lossy)?)
603+
crate::de::from_raw(crate::de::RawDeserializer::new(&buf)?)
604604
}
605605

606606
/// Attempts to deserialize a [`Document`] from a byte stream.
@@ -632,18 +632,7 @@ impl Document {
632632
/// # }
633633
/// ```
634634
pub fn from_reader<R: Read>(mut reader: R) -> crate::de::Result<Document> {
635-
Self::decode(&mut reader, false)
636-
}
637-
638-
/// Attempt to deserialize a [`Document`] that may contain invalid UTF-8 strings from a byte
639-
/// stream.
640-
///
641-
/// This is mainly useful when reading raw BSON returned from a MongoDB server, which
642-
/// in rare cases can contain invalidly truncated strings (<https://jira.mongodb.org/browse/SERVER-24007>).
643-
/// For most use cases, `Document::from_reader` can be used instead.
644-
#[deprecated = "use bson::serde_helpers::Utf8LossyDeserialization"]
645-
pub fn from_reader_utf8_lossy<R: Read>(mut reader: R) -> crate::de::Result<Document> {
646-
Self::decode(&mut reader, true)
635+
Self::decode(&mut reader)
647636
}
648637
}
649638

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
@@ -531,6 +531,17 @@ impl RawDocument {
531531
let bytes = self.cstring_bytes_at(start_at)?;
532532
try_to_str(bytes)
533533
}
534+
535+
/// Copy this into a [`Document`], returning an error if invalid BSON is encountered.
536+
pub fn to_document(&self) -> Result<Document> {
537+
self.as_ref().try_into()
538+
}
539+
540+
/// Copy this into a [`Document`], returning an error if invalid BSON is encountered. Any
541+
/// invalid UTF-8 sequences will be replaced with the Unicode replacement character.
542+
pub fn to_document_utf8_lossy(&self) -> Result<Document> {
543+
todo!()
544+
}
534545
}
535546

536547
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
@@ -34,7 +34,7 @@ impl Error {
3434
}
3535

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

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

4866
impl std::fmt::Display for Error {
@@ -59,10 +77,17 @@ impl std::fmt::Display for Error {
5977
write!(f, "{}malformed value: {:?}", prefix, message)
6078
}
6179
ErrorKind::Utf8EncodingError => write!(f, "{}utf-8 encoding error", prefix),
80+
ErrorKind::DeError(e) => write!(f, "{}deserialization error: {}", prefix, e),
6281
}
6382
}
6483
}
6584

85+
impl From<crate::de::Error> for Error {
86+
fn from(value: crate::de::Error) -> Self {
87+
Self::new(ErrorKind::DeError(value))
88+
}
89+
}
90+
6691
impl std::error::Error for Error {}
6792

6893
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)