Line | Count | Source (jump to first uncovered line) |
1 | | // The MIT License (MIT) |
2 | | |
3 | | // Copyright (c) 2015 Y. T. Chung <zonyitoo@gmail.com> |
4 | | |
5 | | // Permission is hereby granted, free of charge, to any person obtaining a copy of |
6 | | // this software and associated documentation files (the "Software"), to deal in |
7 | | // the Software without restriction, including without limitation the rights to |
8 | | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
9 | | // the Software, and to permit persons to whom the Software is furnished to do so, |
10 | | // subject to the following conditions: |
11 | | |
12 | | // The above copyright notice and this permission notice shall be included in all |
13 | | // copies or substantial portions of the Software. |
14 | | |
15 | | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 | | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
17 | | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
18 | | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
19 | | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
20 | | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
21 | | |
22 | | //! Deserializer |
23 | | |
24 | | mod raw; |
25 | | mod serde; |
26 | | |
27 | | pub use self::serde::Deserializer; |
28 | | |
29 | | use std::io::Read; |
30 | | |
31 | | use crate::{ |
32 | | bson::{Bson, Document}, |
33 | | error::{Error, Result}, |
34 | | raw::reader_to_vec, |
35 | | spec::BinarySubtype, |
36 | | }; |
37 | | |
38 | | #[rustfmt::skip] |
39 | | use ::serde::{de::DeserializeOwned, Deserialize}; |
40 | | |
41 | | pub(crate) use self::serde::{convert_unsigned_to_signed_raw, BsonVisitor}; |
42 | | |
43 | | #[cfg(test)] |
44 | | pub(crate) use self::raw::Deserializer as RawDeserializer; |
45 | | |
46 | | /// Hint provided to the deserializer via `deserialize_newtype_struct` as to the type of thing |
47 | | /// being deserialized. |
48 | | #[derive(Debug, Clone, Copy)] |
49 | | enum DeserializerHint { |
50 | | /// No hint provided, deserialize normally. |
51 | | None, |
52 | | |
53 | | /// The type being deserialized expects the BSON to contain a binary value with the provided |
54 | | /// subtype. This is currently used to deserialize [`bson::Uuid`] values. |
55 | | BinarySubtype(BinarySubtype), |
56 | | |
57 | | /// The type being deserialized is raw BSON, meaning no allocations should occur as part of |
58 | | /// deserializing and everything should be visited via borrowing or [`Copy`] if possible. |
59 | | RawBson, |
60 | | } |
61 | | |
62 | | /// Deserialize a `T` from the provided [`Bson`] value. |
63 | | /// |
64 | | /// The [`Deserializer`] used by this function presents itself as human readable, whereas the |
65 | | /// one used in [`deserialize_from_slice`] does not. This means that this function may deserialize |
66 | | /// differently than [`deserialize_from_slice`] for types that change their deserialization logic |
67 | | /// depending on whether the format is human readable or not. |
68 | 0 | pub fn deserialize_from_bson<T>(bson: Bson) -> Result<T> |
69 | 0 | where |
70 | 0 | T: DeserializeOwned, |
71 | 0 | { |
72 | 0 | let de = Deserializer::new(bson); |
73 | 0 | #[cfg(feature = "serde_path_to_error")] |
74 | 0 | { |
75 | 0 | serde_path_to_error::deserialize(de).map_err(Error::with_path) |
76 | 0 | } |
77 | 0 | #[cfg(not(feature = "serde_path_to_error"))] |
78 | 0 | { |
79 | 0 | Deserialize::deserialize(de) |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | /// Deserialize a `T` from the provided [`Document`]. |
84 | | /// |
85 | | /// The [`Deserializer`] used by this function presents itself as human readable, whereas the |
86 | | /// one used in [`deserialize_from_slice`] does not. This means that this function may deserialize |
87 | | /// differently than [`deserialize_from_slice`] for types that change their deserialization logic |
88 | | /// depending on whether the format is human readable or not. |
89 | 0 | pub fn deserialize_from_document<T>(doc: Document) -> Result<T> |
90 | 0 | where |
91 | 0 | T: DeserializeOwned, |
92 | 0 | { |
93 | 0 | deserialize_from_bson(Bson::Document(doc)) |
94 | 0 | } |
95 | | |
96 | | /// Deserialize an instance of type `T` from an I/O stream of BSON. |
97 | 0 | pub fn deserialize_from_reader<R, T>(reader: R) -> Result<T> |
98 | 0 | where |
99 | 0 | T: DeserializeOwned, |
100 | 0 | R: Read, |
101 | 0 | { |
102 | 0 | let bytes = reader_to_vec(reader)?; |
103 | 0 | deserialize_from_slice(bytes.as_slice()) |
104 | 0 | } |
105 | | |
106 | | /// Deserialize an instance of type `T` from a slice of BSON bytes. |
107 | 17.1k | pub fn deserialize_from_slice<'de, T>(bytes: &'de [u8]) -> Result<T> |
108 | 17.1k | where |
109 | 17.1k | T: Deserialize<'de>, |
110 | 17.1k | { |
111 | 17.1k | deserialize_from_raw(raw::Deserializer::new(bytes)?) |
112 | 17.1k | } Unexecuted instantiation: bson::de::deserialize_from_slice::<_> bson::de::deserialize_from_slice::<bson::document::Document> Line | Count | Source | 107 | 8.46k | pub fn deserialize_from_slice<'de, T>(bytes: &'de [u8]) -> Result<T> | 108 | 8.46k | where | 109 | 8.46k | T: Deserialize<'de>, | 110 | 8.46k | { | 111 | 8.46k | deserialize_from_raw(raw::Deserializer::new(bytes)?) | 112 | 8.46k | } |
bson::de::deserialize_from_slice::<bson::serde_helpers::Utf8LossyDeserialization<bson::document::Document>> Line | Count | Source | 107 | 8.70k | pub fn deserialize_from_slice<'de, T>(bytes: &'de [u8]) -> Result<T> | 108 | 8.70k | where | 109 | 8.70k | T: Deserialize<'de>, | 110 | 8.70k | { | 111 | 8.70k | deserialize_from_raw(raw::Deserializer::new(bytes)?) | 112 | 8.70k | } |
|
113 | | |
114 | 16.9k | pub(crate) fn deserialize_from_raw<'de, T: Deserialize<'de>>( |
115 | 16.9k | deserializer: raw::Deserializer<'de>, |
116 | 16.9k | ) -> Result<T> { |
117 | 16.9k | #[cfg(feature = "serde_path_to_error")] |
118 | 16.9k | { |
119 | 16.9k | serde_path_to_error::deserialize(deserializer).map_err(Error::with_path) |
120 | 16.9k | } |
121 | 16.9k | #[cfg(not(feature = "serde_path_to_error"))] |
122 | 16.9k | { |
123 | 16.9k | T::deserialize(deserializer) |
124 | 16.9k | } |
125 | 16.9k | } Unexecuted instantiation: bson::de::deserialize_from_raw::<_> bson::de::deserialize_from_raw::<bson::document::Document> Line | Count | Source | 114 | 8.36k | pub(crate) fn deserialize_from_raw<'de, T: Deserialize<'de>>( | 115 | 8.36k | deserializer: raw::Deserializer<'de>, | 116 | 8.36k | ) -> Result<T> { | 117 | 8.36k | #[cfg(feature = "serde_path_to_error")] | 118 | 8.36k | { | 119 | 8.36k | serde_path_to_error::deserialize(deserializer).map_err(Error::with_path) | 120 | 8.36k | } | 121 | 8.36k | #[cfg(not(feature = "serde_path_to_error"))] | 122 | 8.36k | { | 123 | 8.36k | T::deserialize(deserializer) | 124 | 8.36k | } | 125 | 8.36k | } |
bson::de::deserialize_from_raw::<bson::serde_helpers::Utf8LossyDeserialization<bson::document::Document>> Line | Count | Source | 114 | 8.62k | pub(crate) fn deserialize_from_raw<'de, T: Deserialize<'de>>( | 115 | 8.62k | deserializer: raw::Deserializer<'de>, | 116 | 8.62k | ) -> Result<T> { | 117 | 8.62k | #[cfg(feature = "serde_path_to_error")] | 118 | 8.62k | { | 119 | 8.62k | serde_path_to_error::deserialize(deserializer).map_err(Error::with_path) | 120 | 8.62k | } | 121 | 8.62k | #[cfg(not(feature = "serde_path_to_error"))] | 122 | 8.62k | { | 123 | 8.62k | T::deserialize(deserializer) | 124 | 8.62k | } | 125 | 8.62k | } |
|