| 1 |
use std::{ |
| 2 |
borrow::Cow, |
| 3 |
convert::TryInto, |
| 4 |
io::{ErrorKind, Read}, |
| 5 |
sync::Arc, |
| 6 |
}; |
| 7 |
|
| 8 |
use serde::{ |
| 9 |
de::{EnumAccess, Error as SerdeError, IntoDeserializer, MapAccess, VariantAccess}, |
| 10 |
forward_to_deserialize_any, |
| 11 |
Deserializer as SerdeDeserializer, |
| 12 |
}; |
| 13 |
|
| 14 |
use crate::{ |
| 15 |
oid::ObjectId, |
| 16 |
raw::{RawBinaryRef, RAW_ARRAY_NEWTYPE, RAW_BSON_NEWTYPE, RAW_DOCUMENT_NEWTYPE}, |
| 17 |
spec::{BinarySubtype, ElementType}, |
| 18 |
uuid::UUID_NEWTYPE_NAME, |
| 19 |
Bson, |
| 20 |
DateTime, |
| 21 |
Decimal128, |
| 22 |
DeserializerOptions, |
| 23 |
RawDocument, |
| 24 |
Timestamp, |
| 25 |
}; |
| 26 |
|
| 27 |
use super::{ |
| 28 |
read_bool, |
| 29 |
read_f128, |
| 30 |
read_f64, |
| 31 |
read_i32, |
| 32 |
read_i64, |
| 33 |
read_string, |
| 34 |
read_u8, |
| 35 |
DeserializerHint, |
| 36 |
Error, |
| 37 |
Result, |
| 38 |
MAX_BSON_SIZE, |
| 39 |
MIN_CODE_WITH_SCOPE_SIZE, |
| 40 |
}; |
| 41 |
use crate::de::serde::MapDeserializer; |
| 42 |
|
| 43 |
/// Deserializer used to parse and deserialize raw BSON bytes. |
| 44 |
pub(crate) struct Deserializer<'de> { |
| 45 |
bytes: BsonBuf<'de>, |
| 46 |
|
| 47 |
/// The type of the element currently being deserialized. |
| 48 |
/// |
| 49 |
/// When the Deserializer is initialized, this will be `ElementType::EmbeddedDocument`, as the |
| 50 |
/// only top level type is a document. The "embedded" portion is incorrect in this context, |
| 51 |
/// but given that there's no difference between deserializing an embedded document and a |
| 52 |
/// top level one, the distinction isn't necessary. |
| 53 |
current_type: ElementType, |
| 54 |
} |
| 55 |
|
| 56 |
/// Enum used to determine what the type of document being deserialized is in |
| 57 |
/// `Deserializer::deserialize_document`. |
| 58 |
enum DocumentType { |
| 59 |
Array, |
| 60 |
EmbeddedDocument, |
| 61 |
} |
| 62 |
|
| 63 |
impl<'de> Deserializer<'de> { |
| 64 |
pub(crate) fn new(buf: &'de [u8], utf8_lossy: bool) -> Self { |
| 65 |
Self { |
| 66 |
bytes: BsonBuf::new(buf, utf8_lossy), |
| 67 |
current_type: ElementType::EmbeddedDocument, |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/// Ensure the entire document was visited, returning an error if not. |
| 72 |
/// Will read the trailing null byte if necessary (i.e. the visitor stopped after visiting |
| 73 |
/// exactly the number of elements in the document). |
| 74 |
fn end_document(&mut self, length_remaining: i32) -> Result<()> { |
| 75 |
match length_remaining.cmp(&1) { |
| 76 |
std::cmp::Ordering::Equal => { |
| 77 |
let nullbyte = read_u8(&mut self.bytes)?; |
| 78 |
if nullbyte != 0 { |
| 79 |
return Err(Error::custom(format!( |
| 80 |
"expected null byte at end of document, got {:#x} instead", |
| 81 |
nullbyte |
| 82 |
))); |
| 83 |
} |
| 84 |
} |
| 85 |
std::cmp::Ordering::Greater => { |
| 86 |
return Err(Error::custom(format!( |
| 87 |
"document has bytes remaining that were not visited: {}", |
| 88 |
length_remaining |
| 89 |
))); |
| 90 |
} |
| 91 |
std::cmp::Ordering::Less => { |
| 92 |
if length_remaining < 0 { |
| 93 |
return Err(Error::custom("length of document was too short")); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
Ok(()) |
| 98 |
} |
| 99 |
|
| 100 |
/// Read a string from the BSON. |
| 101 |
/// |
| 102 |
/// If utf8_lossy, this will be an owned string if invalid UTF-8 is encountered in the string, |
| 103 |
/// otherwise it will be borrowed. |
| 104 |
fn deserialize_str(&mut self) -> Result<Cow<'de, str>> { |
| 105 |
self.bytes.read_str() |
| 106 |
} |
| 107 |
|
| 108 |
/// Read a null-terminated C style string from the underling BSON. |
| 109 |
/// |
| 110 |
/// If utf8_lossy, this will be an owned string if invalid UTF-8 is encountered in the string, |
| 111 |
/// otherwise it will be borrowed. |
| 112 |
fn deserialize_cstr(&mut self) -> Result<Cow<'de, str>> { |
| 113 |
self.bytes.read_cstr() |
| 114 |
} |
| 115 |
|
| 116 |
/// Read an ObjectId from the underling BSON. |
| 117 |
/// |
| 118 |
/// If hinted to use raw BSON, the bytes of the ObjectId will be visited. |
| 119 |
/// Otherwise, a map in the shape of the extended JSON format of an ObjectId will be. |
| 120 |
fn deserialize_objectid<V>(&mut self, visitor: V, hint: DeserializerHint) -> Result<V::Value> |
| 121 |
where |
| 122 |
V: serde::de::Visitor<'de>, |
| 123 |
{ |
| 124 |
let oid = ObjectId::from_reader(&mut self.bytes)?; |
| 125 |
visitor.visit_map(ObjectIdAccess::new(oid, hint)) |
| 126 |
} |
| 127 |
|
| 128 |
/// Read a document from the underling BSON, whether it's an array or an actual document. |
| 129 |
/// |
| 130 |
/// If hinted to use raw BSON, the bytes themselves will be visited using a special newtype |
| 131 |
/// name. Otherwise, the key-value pairs will be accessed in order, either as part of a |
| 132 |
/// [`MapAccess`] for documents or a [`SeqAccess`] for arrays. |
| 133 |
fn deserialize_document<V>( |
| 134 |
&mut self, |
| 135 |
visitor: V, |
| 136 |
hint: DeserializerHint, |
| 137 |
document_type: DocumentType, |
| 138 |
) -> Result<V::Value> |
| 139 |
where |
| 140 |
V: serde::de::Visitor<'de>, |
| 141 |
{ |
| 142 |
let is_array = match document_type { |
| 143 |
DocumentType::Array => true, |
| 144 |
DocumentType::EmbeddedDocument => false, |
| 145 |
}; |
| 146 |
|
| 147 |
match hint { |
| 148 |
DeserializerHint::RawBson => { |
| 149 |
let mut len = self.bytes.slice(4)?; |
| 150 |
let len = read_i32(&mut len)?; |
| 151 |
|
| 152 |
let doc = RawDocument::from_bytes(self.bytes.read_slice(len as usize)?) |
| 153 |
.map_err(Error::custom)?; |
| 154 |
|
| 155 |
let access = if is_array { |
| 156 |
RawDocumentAccess::for_array(doc) |
| 157 |
} else { |
| 158 |
RawDocumentAccess::new(doc) |
| 159 |
}; |
| 160 |
|
| 161 |
visitor.visit_map(access) |
| 162 |
} |
| 163 |
_ if is_array => self.access_document(|access| visitor.visit_seq(access)), |
| 164 |
_ => self.access_document(|access| visitor.visit_map(access)), |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/// Construct a [`DocumentAccess`] and pass it into the provided closure, returning the |
| 169 |
/// result of the closure if no other errors are encountered. |
| 170 |
fn access_document<F, O>(&mut self, f: F) -> Result<O> |
| 171 |
where |
| 172 |
F: FnOnce(DocumentAccess<'_, 'de>) -> Result<O>, |
| 173 |
{ |
| 174 |
let mut length_remaining = read_i32(&mut self.bytes)?; |
| 175 |
if length_remaining < 4 { |
| 176 |
return Err(Error::custom("invalid length, less than min document size")); |
| 177 |
} |
| 178 |
length_remaining -= 4; |
| 179 |
let out = f(DocumentAccess { |
| 180 |
root_deserializer: self, |
| 181 |
length_remaining: &mut length_remaining, |
| 182 |
}); |
| 183 |
|
| 184 |
if out.is_ok() { |
| 185 |
self.end_document(length_remaining)?; |
| 186 |
} |
| 187 |
out |
| 188 |
} |
| 189 |
|
| 190 |
/// Deserialize the next element type and update `current_type` accordingly. |
| 191 |
/// Returns [`None`] if a null byte is read. |
| 192 |
fn deserialize_next_type(&mut self) -> Result<Option<ElementType>> { |
| 193 |
let tag = read_u8(&mut self.bytes)?; |
| 194 |
if tag == 0 { |
| 195 |
return Ok(None); |
| 196 |
} |
| 197 |
|
| 198 |
let element_type = ElementType::from(tag) |
| 199 |
.ok_or_else(|| Error::custom(format!("invalid element type: {}", tag)))?; |
| 200 |
|
| 201 |
self.current_type = element_type; |
| 202 |
Ok(Some(element_type)) |
| 203 |
} |
| 204 |
|
| 205 |
/// Deserialize the next element in the BSON, using the type of the element along with the |
| 206 |
/// provided hint to determine how to visit the data. |
| 207 |
fn deserialize_next<V>(&mut self, visitor: V, hint: DeserializerHint) -> Result<V::Value> |
| 208 |
where |
| 209 |
V: serde::de::Visitor<'de>, |
| 210 |
{ |
| 211 |
match self.current_type { |
| 212 |
ElementType::Int32 => visitor.visit_i32(read_i32(&mut self.bytes)?), |
| 213 |
ElementType::Int64 => visitor.visit_i64(read_i64(&mut self.bytes)?), |
| 214 |
ElementType::Double => visitor.visit_f64(read_f64(&mut self.bytes)?), |
| 215 |
ElementType::String => match self.deserialize_str()? { |
| 216 |
Cow::Borrowed(s) => visitor.visit_borrowed_str(s), |
| 217 |
Cow::Owned(string) => visitor.visit_string(string), |
| 218 |
}, |
| 219 |
ElementType::Boolean => visitor.visit_bool(read_bool(&mut self.bytes)?), |
| 220 |
ElementType::Null => visitor.visit_unit(), |
| 221 |
ElementType::ObjectId => self.deserialize_objectid(visitor, hint), |
| 222 |
ElementType::EmbeddedDocument => { |
| 223 |
self.deserialize_document(visitor, hint, DocumentType::EmbeddedDocument) |
| 224 |
} |
| 225 |
ElementType::Array => self.deserialize_document(visitor, hint, DocumentType::Array), |
| 226 |
ElementType::Binary => { |
| 227 |
let len = read_i32(&mut self.bytes)?; |
| 228 |
if !(0..=MAX_BSON_SIZE).contains(&len) { |
| 229 |
return Err(Error::invalid_length( |
| 230 |
len as usize, |
| 231 |
&format!("binary length must be between 0 and {}", MAX_BSON_SIZE).as_str(), |
| 232 |
)); |
| 233 |
} |
| 234 |
let subtype = BinarySubtype::from(read_u8(&mut self.bytes)?); |
| 235 |
|
| 236 |
if let DeserializerHint::BinarySubtype(expected_subtype) = hint { |
| 237 |
if subtype != expected_subtype { |
| 238 |
return Err(Error::custom(format!( |
| 239 |
"expected binary subtype {:?} instead got {:?}", |
| 240 |
expected_subtype, subtype |
| 241 |
))); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
match subtype { |
| 246 |
BinarySubtype::Generic => { |
| 247 |
visitor.visit_borrowed_bytes(self.bytes.read_slice(len as usize)?) |
| 248 |
} |
| 249 |
_ => { |
| 250 |
let binary = RawBinaryRef::from_slice_with_len_and_payload( |
| 251 |
self.bytes.read_slice(len as usize)?, |
| 252 |
len, |
| 253 |
subtype, |
| 254 |
)?; |
| 255 |
let mut d = BinaryDeserializer::new(binary, hint); |
| 256 |
visitor.visit_map(BinaryAccess { |
| 257 |
deserializer: &mut d, |
| 258 |
}) |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
ElementType::Undefined => { |
| 263 |
visitor.visit_map(RawBsonAccess::new("$undefined", BsonContent::Boolean(true))) |
| 264 |
} |
| 265 |
ElementType::DateTime => { |
| 266 |
let dti = read_i64(&mut self.bytes)?; |
| 267 |
let dt = DateTime::from_millis(dti); |
| 268 |
let mut d = DateTimeDeserializer::new(dt, hint); |
| 269 |
visitor.visit_map(DateTimeAccess { |
| 270 |
deserializer: &mut d, |
| 271 |
}) |
| 272 |
} |
| 273 |
ElementType::RegularExpression => { |
| 274 |
let mut de = RegexDeserializer::new(&mut *self); |
| 275 |
visitor.visit_map(RegexAccess::new(&mut de)) |
| 276 |
} |
| 277 |
ElementType::DbPointer => { |
| 278 |
let mut de = DbPointerDeserializer::new(&mut *self, hint); |
| 279 |
visitor.visit_map(DbPointerAccess::new(&mut de)) |
| 280 |
} |
| 281 |
ElementType::JavaScriptCode => { |
| 282 |
let utf8_lossy = self.bytes.utf8_lossy; |
| 283 |
|
| 284 |
match hint { |
| 285 |
DeserializerHint::RawBson => visitor.visit_map(RawBsonAccess::new( |
| 286 |
"$code", |
| 287 |
BsonContent::Str(self.bytes.read_borrowed_str()?), |
| 288 |
)), |
| 289 |
_ => { |
| 290 |
let code = read_string(&mut self.bytes, utf8_lossy)?; |
| 291 |
let doc = Bson::JavaScriptCode(code).into_extended_document(false); |
| 292 |
visitor.visit_map(MapDeserializer::new( |
| 293 |
doc, |
| 294 |
DeserializerOptions::builder().human_readable(false).build(), |
| 295 |
)) |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
ElementType::JavaScriptCodeWithScope => { |
| 300 |
let len = read_i32(&mut self.bytes)?; |
| 301 |
|
| 302 |
if len < MIN_CODE_WITH_SCOPE_SIZE { |
| 303 |
return Err(SerdeError::invalid_length( |
| 304 |
len.try_into().unwrap_or(0), |
| 305 |
&format!( |
| 306 |
"CodeWithScope to be at least {} bytes", |
| 307 |
MIN_CODE_WITH_SCOPE_SIZE |
| 308 |
) |
| 309 |
.as_str(), |
| 310 |
)); |
| 311 |
} else if (self.bytes.bytes_remaining() as i32) < len - 4 { |
| 312 |
return Err(SerdeError::invalid_length( |
| 313 |
len.try_into().unwrap_or(0), |
| 314 |
&format!( |
| 315 |
"CodeWithScope to be at most {} bytes", |
| 316 |
self.bytes.bytes_remaining() |
| 317 |
) |
| 318 |
.as_str(), |
| 319 |
)); |
| 320 |
} |
| 321 |
|
| 322 |
let mut de = CodeWithScopeDeserializer::new(&mut *self, hint, len - 4); |
| 323 |
let out = visitor.visit_map(CodeWithScopeAccess::new(&mut de)); |
| 324 |
|
| 325 |
if de.length_remaining != 0 { |
| 326 |
return Err(SerdeError::invalid_length( |
| 327 |
len.try_into().unwrap_or(0), |
| 328 |
&format!( |
| 329 |
"CodeWithScope length {} bytes greater than actual length", |
| 330 |
de.length_remaining |
| 331 |
) |
| 332 |
.as_str(), |
| 333 |
)); |
| 334 |
} |
| 335 |
|
| 336 |
out |
| 337 |
} |
| 338 |
ElementType::Symbol => { |
| 339 |
let utf8_lossy = self.bytes.utf8_lossy; |
| 340 |
|
| 341 |
match hint { |
| 342 |
DeserializerHint::RawBson => visitor.visit_map(RawBsonAccess::new( |
| 343 |
"$symbol", |
| 344 |
BsonContent::Str(self.bytes.read_borrowed_str()?), |
| 345 |
)), |
| 346 |
_ => { |
| 347 |
let symbol = read_string(&mut self.bytes, utf8_lossy)?; |
| 348 |
let doc = Bson::Symbol(symbol).into_extended_document(false); |
| 349 |
visitor.visit_map(MapDeserializer::new( |
| 350 |
doc, |
| 351 |
DeserializerOptions::builder().human_readable(false).build(), |
| 352 |
)) |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
ElementType::Timestamp => { |
| 357 |
let ts = Timestamp::from_reader(&mut self.bytes)?; |
| 358 |
let mut d = TimestampDeserializer::new(ts); |
| 359 |
visitor.visit_map(TimestampAccess { |
| 360 |
deserializer: &mut d, |
| 361 |
}) |
| 362 |
} |
| 363 |
ElementType::Decimal128 => { |
| 364 |
let d128 = read_f128(&mut self.bytes)?; |
| 365 |
visitor.visit_map(Decimal128Access::new(d128)) |
| 366 |
} |
| 367 |
ElementType::MaxKey => { |
| 368 |
visitor.visit_map(RawBsonAccess::new("$maxKey", BsonContent::Int32(1))) |
| 369 |
} |
| 370 |
ElementType::MinKey => { |
| 371 |
visitor.visit_map(RawBsonAccess::new("$minKey", BsonContent::Int32(1))) |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> { |
| 378 |
type Error = Error; |
| 379 |
|
| 380 |
#[inline] |
| 381 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 382 |
where |
| 383 |
V: serde::de::Visitor<'de>, |
| 384 |
{ |
| 385 |
self.deserialize_next(visitor, DeserializerHint::None) |
| 386 |
} |
| 387 |
|
| 388 |
#[inline] |
| 389 |
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value> |
| 390 |
where |
| 391 |
V: serde::de::Visitor<'de>, |
| 392 |
{ |
| 393 |
match self.current_type { |
| 394 |
ElementType::Null => visitor.visit_none(), |
| 395 |
_ => visitor.visit_some(self), |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
fn deserialize_enum<V>( |
| 400 |
self, |
| 401 |
_name: &str, |
| 402 |
_variants: &'static [&'static str], |
| 403 |
visitor: V, |
| 404 |
) -> Result<V::Value> |
| 405 |
where |
| 406 |
V: serde::de::Visitor<'de>, |
| 407 |
{ |
| 408 |
match self.current_type { |
| 409 |
ElementType::String => visitor.visit_enum(self.deserialize_str()?.into_deserializer()), |
| 410 |
ElementType::EmbeddedDocument => { |
| 411 |
self.access_document(|access| visitor.visit_enum(access)) |
| 412 |
} |
| 413 |
t => Err(Error::custom(format!("expected enum, instead got {:?}", t))), |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value> |
| 418 |
where |
| 419 |
V: serde::de::Visitor<'de>, |
| 420 |
{ |
| 421 |
match self.current_type { |
| 422 |
ElementType::ObjectId => visitor.visit_borrowed_bytes(self.bytes.read_slice(12)?), |
| 423 |
_ => self.deserialize_any(visitor), |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value> |
| 428 |
where |
| 429 |
V: serde::de::Visitor<'de>, |
| 430 |
{ |
| 431 |
match name { |
| 432 |
UUID_NEWTYPE_NAME => self.deserialize_next( |
| 433 |
visitor, |
| 434 |
DeserializerHint::BinarySubtype(BinarySubtype::Uuid), |
| 435 |
), |
| 436 |
RAW_BSON_NEWTYPE => self.deserialize_next(visitor, DeserializerHint::RawBson), |
| 437 |
RAW_DOCUMENT_NEWTYPE => { |
| 438 |
if self.current_type != ElementType::EmbeddedDocument { |
| 439 |
return Err(serde::de::Error::custom(format!( |
| 440 |
"expected raw document, instead got {:?}", |
| 441 |
self.current_type |
| 442 |
))); |
| 443 |
} |
| 444 |
|
| 445 |
self.deserialize_next(visitor, DeserializerHint::RawBson) |
| 446 |
} |
| 447 |
RAW_ARRAY_NEWTYPE => { |
| 448 |
if self.current_type != ElementType::Array { |
| 449 |
return Err(serde::de::Error::custom(format!( |
| 450 |
"expected raw array, instead got {:?}", |
| 451 |
self.current_type |
| 452 |
))); |
| 453 |
} |
| 454 |
|
| 455 |
self.deserialize_next(visitor, DeserializerHint::RawBson) |
| 456 |
} |
| 457 |
_ => visitor.visit_newtype_struct(self), |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
fn is_human_readable(&self) -> bool { |
| 462 |
false |
| 463 |
} |
| 464 |
|
| 465 |
forward_to_deserialize_any! { |
| 466 |
bool char str byte_buf unit unit_struct string |
| 467 |
identifier seq tuple tuple_struct struct |
| 468 |
map ignored_any i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
/// Struct for accessing documents for deserialization purposes. |
| 473 |
/// This is used to deserialize maps, structs, sequences, and enums. |
| 474 |
struct DocumentAccess<'d, 'de> { |
| 475 |
root_deserializer: &'d mut Deserializer<'de>, |
| 476 |
length_remaining: &'d mut i32, |
| 477 |
} |
| 478 |
|
| 479 |
impl<'d, 'de> DocumentAccess<'d, 'de> { |
| 480 |
/// Read the next element type and update the root deserializer with it. |
| 481 |
/// |
| 482 |
/// Returns `Ok(None)` if the document has been fully read and has no more elements. |
| 483 |
fn read_next_type(&mut self) -> Result<Option<ElementType>> { |
| 484 |
let t = self.read(|s| s.root_deserializer.deserialize_next_type())?; |
| 485 |
|
| 486 |
if t.is_none() && *self.length_remaining != 0 { |
| 487 |
return Err(Error::custom(format!( |
| 488 |
"got null byte but still have length {} remaining", |
| 489 |
self.length_remaining |
| 490 |
))); |
| 491 |
} |
| 492 |
|
| 493 |
Ok(t) |
| 494 |
} |
| 495 |
|
| 496 |
/// Executes a closure that reads from the BSON bytes and returns an error if the number of |
| 497 |
/// bytes read exceeds length_remaining. |
| 498 |
/// |
| 499 |
/// A mutable reference to this [`DocumentAccess`] is passed into the closure. |
| 500 |
fn read<F, O>(&mut self, f: F) -> Result<O> |
| 501 |
where |
| 502 |
F: FnOnce(&mut Self) -> Result<O>, |
| 503 |
{ |
| 504 |
let start_bytes = self.root_deserializer.bytes.bytes_read(); |
| 505 |
let out = f(self)?; |
| 506 |
let bytes_read = self.root_deserializer.bytes.bytes_read() - start_bytes; |
| 507 |
let bytes_read: i32 = bytes_read |
| 508 |
.try_into() |
| 509 |
.map_err(|_| Error::custom("overflow in read size"))?; |
| 510 |
if bytes_read > *self.length_remaining { |
| 511 |
return Err(Error::custom("length of document too short")); |
| 512 |
} |
| 513 |
*self.length_remaining -= bytes_read; |
| 514 |
Ok(out) |
| 515 |
} |
| 516 |
|
| 517 |
/// Read the next value from the document. |
| 518 |
fn read_next_value<V>(&mut self, seed: V) -> Result<V::Value> |
| 519 |
where |
| 520 |
V: serde::de::DeserializeSeed<'de>, |
| 521 |
{ |
| 522 |
self.read(|s| seed.deserialize(&mut *s.root_deserializer)) |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
impl<'d, 'de> serde::de::MapAccess<'de> for DocumentAccess<'d, 'de> { |
| 527 |
type Error = crate::de::Error; |
| 528 |
|
| 529 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 530 |
where |
| 531 |
K: serde::de::DeserializeSeed<'de>, |
| 532 |
{ |
| 533 |
if self.read_next_type()?.is_none() { |
| 534 |
return Ok(None); |
| 535 |
} |
| 536 |
|
| 537 |
self.read(|s| { |
| 538 |
seed.deserialize(DocumentKeyDeserializer { |
| 539 |
root_deserializer: &mut *s.root_deserializer, |
| 540 |
}) |
| 541 |
}) |
| 542 |
.map(Some) |
| 543 |
} |
| 544 |
|
| 545 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 546 |
where |
| 547 |
V: serde::de::DeserializeSeed<'de>, |
| 548 |
{ |
| 549 |
self.read_next_value(seed) |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
impl<'d, 'de> serde::de::SeqAccess<'de> for DocumentAccess<'d, 'de> { |
| 554 |
type Error = Error; |
| 555 |
|
| 556 |
fn next_element_seed<S>(&mut self, seed: S) -> Result<Option<S::Value>> |
| 557 |
where |
| 558 |
S: serde::de::DeserializeSeed<'de>, |
| 559 |
{ |
| 560 |
if self.read_next_type()?.is_none() { |
| 561 |
return Ok(None); |
| 562 |
} |
| 563 |
let _index = self.read(|s| s.root_deserializer.deserialize_cstr())?; |
| 564 |
self.read_next_value(seed).map(Some) |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
impl<'d, 'de> EnumAccess<'de> for DocumentAccess<'d, 'de> { |
| 569 |
type Error = Error; |
| 570 |
type Variant = Self; |
| 571 |
|
| 572 |
fn variant_seed<V>(mut self, seed: V) -> Result<(V::Value, Self::Variant)> |
| 573 |
where |
| 574 |
V: serde::de::DeserializeSeed<'de>, |
| 575 |
{ |
| 576 |
if self.read_next_type()?.is_none() { |
| 577 |
return Err(Error::EndOfStream); |
| 578 |
} |
| 579 |
|
| 580 |
let key = self.read(|s| { |
| 581 |
seed.deserialize(DocumentKeyDeserializer { |
| 582 |
root_deserializer: &mut *s.root_deserializer, |
| 583 |
}) |
| 584 |
})?; |
| 585 |
|
| 586 |
Ok((key, self)) |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
impl<'d, 'de> VariantAccess<'de> for DocumentAccess<'d, 'de> { |
| 591 |
type Error = Error; |
| 592 |
|
| 593 |
fn unit_variant(self) -> Result<()> { |
| 594 |
Err(Error::custom( |
| 595 |
"expected a string enum, got a document instead", |
| 596 |
)) |
| 597 |
} |
| 598 |
|
| 599 |
fn newtype_variant_seed<S>(mut self, seed: S) -> Result<S::Value> |
| 600 |
where |
| 601 |
S: serde::de::DeserializeSeed<'de>, |
| 602 |
{ |
| 603 |
self.read_next_value(seed) |
| 604 |
} |
| 605 |
|
| 606 |
fn tuple_variant<V>(mut self, _len: usize, visitor: V) -> Result<V::Value> |
| 607 |
where |
| 608 |
V: serde::de::Visitor<'de>, |
| 609 |
{ |
| 610 |
self.read(|s| s.root_deserializer.deserialize_seq(visitor)) |
| 611 |
} |
| 612 |
|
| 613 |
fn struct_variant<V>(mut self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value> |
| 614 |
where |
| 615 |
V: serde::de::Visitor<'de>, |
| 616 |
{ |
| 617 |
self.read(|s| s.root_deserializer.deserialize_map(visitor)) |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
/// Deserializer used specifically for deserializing a document's cstring keys. |
| 622 |
struct DocumentKeyDeserializer<'d, 'de> { |
| 623 |
root_deserializer: &'d mut Deserializer<'de>, |
| 624 |
} |
| 625 |
|
| 626 |
impl<'d, 'de> serde::de::Deserializer<'de> for DocumentKeyDeserializer<'d, 'de> { |
| 627 |
type Error = Error; |
| 628 |
|
| 629 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 630 |
where |
| 631 |
V: serde::de::Visitor<'de>, |
| 632 |
{ |
| 633 |
let s = self.root_deserializer.deserialize_cstr()?; |
| 634 |
match s { |
| 635 |
Cow::Borrowed(b) => visitor.visit_borrowed_str(b), |
| 636 |
Cow::Owned(string) => visitor.visit_string(string), |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
fn deserialize_enum<V>( |
| 641 |
self, |
| 642 |
_name: &str, |
| 643 |
_variants: &'static [&'static str], |
| 644 |
visitor: V, |
| 645 |
) -> Result<V::Value> |
| 646 |
where |
| 647 |
V: serde::de::Visitor<'de>, |
| 648 |
{ |
| 649 |
visitor.visit_enum( |
| 650 |
self.root_deserializer |
| 651 |
.deserialize_cstr()? |
| 652 |
.into_deserializer(), |
| 653 |
) |
| 654 |
} |
| 655 |
|
| 656 |
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> |
| 657 |
where |
| 658 |
V: serde::de::Visitor<'de>, |
| 659 |
{ |
| 660 |
visitor.visit_newtype_struct(self) |
| 661 |
} |
| 662 |
|
| 663 |
fn is_human_readable(&self) -> bool { |
| 664 |
false |
| 665 |
} |
| 666 |
|
| 667 |
forward_to_deserialize_any! { |
| 668 |
bool char str bytes byte_buf option unit unit_struct string |
| 669 |
identifier seq tuple tuple_struct struct map |
| 670 |
ignored_any i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
/// Deserializer used to deserialize the given field name without any copies. |
| 675 |
struct FieldDeserializer { |
| 676 |
field_name: &'static str, |
| 677 |
} |
| 678 |
|
| 679 |
impl<'de> serde::de::Deserializer<'de> for FieldDeserializer { |
| 680 |
type Error = Error; |
| 681 |
|
| 682 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 683 |
where |
| 684 |
V: serde::de::Visitor<'de>, |
| 685 |
{ |
| 686 |
visitor.visit_borrowed_str(self.field_name) |
| 687 |
} |
| 688 |
|
| 689 |
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> |
| 690 |
where |
| 691 |
V: serde::de::Visitor<'de>, |
| 692 |
{ |
| 693 |
visitor.visit_newtype_struct(self) |
| 694 |
} |
| 695 |
|
| 696 |
fn is_human_readable(&self) -> bool { |
| 697 |
false |
| 698 |
} |
| 699 |
|
| 700 |
serde::forward_to_deserialize_any! { |
| 701 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 702 |
bytes byte_buf map struct option unit |
| 703 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
/// A [`MapAccess`] used to deserialize entire documents as chunks of bytes without deserializing |
| 708 |
/// the individual key/value pairs. |
| 709 |
struct RawDocumentAccess<'d> { |
| 710 |
deserializer: RawDocumentDeserializer<'d>, |
| 711 |
|
| 712 |
/// Whether the first key has been deserialized yet or not. |
| 713 |
deserialized_first: bool, |
| 714 |
|
| 715 |
/// Whether or not this document being deserialized is for an array or not. |
| 716 |
array: bool, |
| 717 |
} |
| 718 |
|
| 719 |
impl<'de> RawDocumentAccess<'de> { |
| 720 |
fn new(doc: &'de RawDocument) -> Self { |
| 721 |
Self { |
| 722 |
deserializer: RawDocumentDeserializer { raw_doc: doc }, |
| 723 |
deserialized_first: false, |
| 724 |
array: false, |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
fn for_array(doc: &'de RawDocument) -> Self { |
| 729 |
Self { |
| 730 |
deserializer: RawDocumentDeserializer { raw_doc: doc }, |
| 731 |
deserialized_first: false, |
| 732 |
array: true, |
| 733 |
} |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
impl<'de> serde::de::MapAccess<'de> for RawDocumentAccess<'de> { |
| 738 |
type Error = Error; |
| 739 |
|
| 740 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 741 |
where |
| 742 |
K: serde::de::DeserializeSeed<'de>, |
| 743 |
{ |
| 744 |
if !self.deserialized_first { |
| 745 |
self.deserialized_first = true; |
| 746 |
|
| 747 |
// the newtype name will indicate to the [`RawBson`] enum that the incoming |
| 748 |
// bytes are meant to be treated as a document or array instead of a binary value. |
| 749 |
seed.deserialize(FieldDeserializer { |
| 750 |
field_name: if self.array { |
| 751 |
RAW_ARRAY_NEWTYPE |
| 752 |
} else { |
| 753 |
RAW_DOCUMENT_NEWTYPE |
| 754 |
}, |
| 755 |
}) |
| 756 |
.map(Some) |
| 757 |
} else { |
| 758 |
Ok(None) |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 763 |
where |
| 764 |
V: serde::de::DeserializeSeed<'de>, |
| 765 |
{ |
| 766 |
seed.deserialize(self.deserializer) |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
#[derive(Clone, Copy)] |
| 771 |
struct RawDocumentDeserializer<'a> { |
| 772 |
raw_doc: &'a RawDocument, |
| 773 |
} |
| 774 |
|
| 775 |
impl<'de> serde::de::Deserializer<'de> for RawDocumentDeserializer<'de> { |
| 776 |
type Error = Error; |
| 777 |
|
| 778 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 779 |
where |
| 780 |
V: serde::de::Visitor<'de>, |
| 781 |
{ |
| 782 |
visitor.visit_borrowed_bytes(self.raw_doc.as_bytes()) |
| 783 |
} |
| 784 |
|
| 785 |
fn is_human_readable(&self) -> bool { |
| 786 |
false |
| 787 |
} |
| 788 |
|
| 789 |
serde::forward_to_deserialize_any! { |
| 790 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 791 |
bytes byte_buf map struct option unit newtype_struct |
| 792 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
struct ObjectIdAccess { |
| 797 |
oid: ObjectId, |
| 798 |
visited: bool, |
| 799 |
hint: DeserializerHint, |
| 800 |
} |
| 801 |
|
| 802 |
impl ObjectIdAccess { |
| 803 |
fn new(oid: ObjectId, hint: DeserializerHint) -> Self { |
| 804 |
Self { |
| 805 |
oid, |
| 806 |
visited: false, |
| 807 |
hint, |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
impl<'de> serde::de::MapAccess<'de> for ObjectIdAccess { |
| 813 |
type Error = Error; |
| 814 |
|
| 815 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 816 |
where |
| 817 |
K: serde::de::DeserializeSeed<'de>, |
| 818 |
{ |
| 819 |
if self.visited { |
| 820 |
return Ok(None); |
| 821 |
} |
| 822 |
self.visited = true; |
| 823 |
seed.deserialize(FieldDeserializer { field_name: "$oid" }) |
| 824 |
.map(Some) |
| 825 |
} |
| 826 |
|
| 827 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 828 |
where |
| 829 |
V: serde::de::DeserializeSeed<'de>, |
| 830 |
{ |
| 831 |
seed.deserialize(ObjectIdDeserializer { |
| 832 |
oid: self.oid, |
| 833 |
hint: self.hint, |
| 834 |
}) |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
struct ObjectIdDeserializer { |
| 839 |
oid: ObjectId, |
| 840 |
hint: DeserializerHint, |
| 841 |
} |
| 842 |
|
| 843 |
impl<'de> serde::de::Deserializer<'de> for ObjectIdDeserializer { |
| 844 |
type Error = Error; |
| 845 |
|
| 846 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 847 |
where |
| 848 |
V: serde::de::Visitor<'de>, |
| 849 |
{ |
| 850 |
// save an allocation when deserializing to raw bson |
| 851 |
match self.hint { |
| 852 |
DeserializerHint::RawBson => visitor.visit_bytes(&self.oid.bytes()), |
| 853 |
_ => visitor.visit_string(self.oid.to_hex()), |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
fn is_human_readable(&self) -> bool { |
| 858 |
false |
| 859 |
} |
| 860 |
|
| 861 |
serde::forward_to_deserialize_any! { |
| 862 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 863 |
bytes byte_buf map struct option unit newtype_struct |
| 864 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
pub(crate) struct Decimal128Access { |
| 869 |
decimal: Decimal128, |
| 870 |
visited: bool, |
| 871 |
} |
| 872 |
|
| 873 |
impl Decimal128Access { |
| 874 |
pub(crate) fn new(decimal: Decimal128) -> Self { |
| 875 |
Self { |
| 876 |
decimal, |
| 877 |
visited: false, |
| 878 |
} |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
impl<'de> serde::de::MapAccess<'de> for Decimal128Access { |
| 883 |
type Error = Error; |
| 884 |
|
| 885 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 886 |
where |
| 887 |
K: serde::de::DeserializeSeed<'de>, |
| 888 |
{ |
| 889 |
if self.visited { |
| 890 |
return Ok(None); |
| 891 |
} |
| 892 |
self.visited = true; |
| 893 |
seed.deserialize(FieldDeserializer { |
| 894 |
field_name: "$numberDecimalBytes", |
| 895 |
}) |
| 896 |
.map(Some) |
| 897 |
} |
| 898 |
|
| 899 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 900 |
where |
| 901 |
V: serde::de::DeserializeSeed<'de>, |
| 902 |
{ |
| 903 |
seed.deserialize(Decimal128Deserializer(self.decimal)) |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
struct Decimal128Deserializer(Decimal128); |
| 908 |
|
| 909 |
impl<'de> serde::de::Deserializer<'de> for Decimal128Deserializer { |
| 910 |
type Error = Error; |
| 911 |
|
| 912 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 913 |
where |
| 914 |
V: serde::de::Visitor<'de>, |
| 915 |
{ |
| 916 |
visitor.visit_bytes(&self.0.bytes) |
| 917 |
} |
| 918 |
|
| 919 |
fn is_human_readable(&self) -> bool { |
| 920 |
false |
| 921 |
} |
| 922 |
|
| 923 |
serde::forward_to_deserialize_any! { |
| 924 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 925 |
bytes byte_buf map struct option unit newtype_struct |
| 926 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
enum TimestampDeserializationStage { |
| 931 |
TopLevel, |
| 932 |
Time, |
| 933 |
Increment, |
| 934 |
Done, |
| 935 |
} |
| 936 |
|
| 937 |
struct TimestampAccess<'d> { |
| 938 |
deserializer: &'d mut TimestampDeserializer, |
| 939 |
} |
| 940 |
|
| 941 |
impl<'de, 'd> serde::de::MapAccess<'de> for TimestampAccess<'d> { |
| 942 |
type Error = Error; |
| 943 |
|
| 944 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 945 |
where |
| 946 |
K: serde::de::DeserializeSeed<'de>, |
| 947 |
{ |
| 948 |
match self.deserializer.stage { |
| 949 |
TimestampDeserializationStage::TopLevel => seed |
| 950 |
.deserialize(FieldDeserializer { |
| 951 |
field_name: "$timestamp", |
| 952 |
}) |
| 953 |
.map(Some), |
| 954 |
TimestampDeserializationStage::Time => seed |
| 955 |
.deserialize(FieldDeserializer { field_name: "t" }) |
| 956 |
.map(Some), |
| 957 |
TimestampDeserializationStage::Increment => seed |
| 958 |
.deserialize(FieldDeserializer { field_name: "i" }) |
| 959 |
.map(Some), |
| 960 |
TimestampDeserializationStage::Done => Ok(None), |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 965 |
where |
| 966 |
V: serde::de::DeserializeSeed<'de>, |
| 967 |
{ |
| 968 |
seed.deserialize(&mut *self.deserializer) |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
struct TimestampDeserializer { |
| 973 |
ts: Timestamp, |
| 974 |
stage: TimestampDeserializationStage, |
| 975 |
} |
| 976 |
|
| 977 |
impl TimestampDeserializer { |
| 978 |
fn new(ts: Timestamp) -> Self { |
| 979 |
Self { |
| 980 |
ts, |
| 981 |
stage: TimestampDeserializationStage::TopLevel, |
| 982 |
} |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut TimestampDeserializer { |
| 987 |
type Error = Error; |
| 988 |
|
| 989 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 990 |
where |
| 991 |
V: serde::de::Visitor<'de>, |
| 992 |
{ |
| 993 |
match self.stage { |
| 994 |
TimestampDeserializationStage::TopLevel => { |
| 995 |
self.stage = TimestampDeserializationStage::Time; |
| 996 |
visitor.visit_map(TimestampAccess { deserializer: self }) |
| 997 |
} |
| 998 |
TimestampDeserializationStage::Time => { |
| 999 |
self.stage = TimestampDeserializationStage::Increment; |
| 1000 |
visitor.visit_u32(self.ts.time) |
| 1001 |
} |
| 1002 |
TimestampDeserializationStage::Increment => { |
| 1003 |
self.stage = TimestampDeserializationStage::Done; |
| 1004 |
visitor.visit_u32(self.ts.increment) |
| 1005 |
} |
| 1006 |
TimestampDeserializationStage::Done => { |
| 1007 |
Err(Error::custom("timestamp fully deserialized already")) |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
fn is_human_readable(&self) -> bool { |
| 1013 |
false |
| 1014 |
} |
| 1015 |
|
| 1016 |
serde::forward_to_deserialize_any! { |
| 1017 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 1018 |
bytes byte_buf map struct option unit newtype_struct |
| 1019 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
/// A [`MapAccess`] providing access to a BSON datetime being deserialized. |
| 1024 |
/// |
| 1025 |
/// If hinted to be raw BSON, this deserializes the serde data model equivalent |
| 1026 |
/// of { "$date": <i64 ms from epoch> }. |
| 1027 |
/// |
| 1028 |
/// Otherwise, this deserializes the serde data model equivalent of |
| 1029 |
/// { "$date": { "$numberLong": <ms from epoch as a string> } }. |
| 1030 |
struct DateTimeAccess<'d> { |
| 1031 |
deserializer: &'d mut DateTimeDeserializer, |
| 1032 |
} |
| 1033 |
|
| 1034 |
impl<'de, 'd> serde::de::MapAccess<'de> for DateTimeAccess<'d> { |
| 1035 |
type Error = Error; |
| 1036 |
|
| 1037 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 1038 |
where |
| 1039 |
K: serde::de::DeserializeSeed<'de>, |
| 1040 |
{ |
| 1041 |
match self.deserializer.stage { |
| 1042 |
DateTimeDeserializationStage::TopLevel => seed |
| 1043 |
.deserialize(FieldDeserializer { |
| 1044 |
field_name: "$date", |
| 1045 |
}) |
| 1046 |
.map(Some), |
| 1047 |
DateTimeDeserializationStage::NumberLong => seed |
| 1048 |
.deserialize(FieldDeserializer { |
| 1049 |
field_name: "$numberLong", |
| 1050 |
}) |
| 1051 |
.map(Some), |
| 1052 |
DateTimeDeserializationStage::Done => Ok(None), |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 1057 |
where |
| 1058 |
V: serde::de::DeserializeSeed<'de>, |
| 1059 |
{ |
| 1060 |
seed.deserialize(&mut *self.deserializer) |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
struct DateTimeDeserializer { |
| 1065 |
dt: DateTime, |
| 1066 |
stage: DateTimeDeserializationStage, |
| 1067 |
hint: DeserializerHint, |
| 1068 |
} |
| 1069 |
|
| 1070 |
enum DateTimeDeserializationStage { |
| 1071 |
TopLevel, |
| 1072 |
NumberLong, |
| 1073 |
Done, |
| 1074 |
} |
| 1075 |
|
| 1076 |
impl DateTimeDeserializer { |
| 1077 |
fn new(dt: DateTime, hint: DeserializerHint) -> Self { |
| 1078 |
Self { |
| 1079 |
dt, |
| 1080 |
stage: DateTimeDeserializationStage::TopLevel, |
| 1081 |
hint, |
| 1082 |
} |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut DateTimeDeserializer { |
| 1087 |
type Error = Error; |
| 1088 |
|
| 1089 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 1090 |
where |
| 1091 |
V: serde::de::Visitor<'de>, |
| 1092 |
{ |
| 1093 |
match self.stage { |
| 1094 |
DateTimeDeserializationStage::TopLevel => match self.hint { |
| 1095 |
DeserializerHint::RawBson => { |
| 1096 |
self.stage = DateTimeDeserializationStage::Done; |
| 1097 |
visitor.visit_i64(self.dt.timestamp_millis()) |
| 1098 |
} |
| 1099 |
_ => { |
| 1100 |
self.stage = DateTimeDeserializationStage::NumberLong; |
| 1101 |
visitor.visit_map(DateTimeAccess { deserializer: self }) |
| 1102 |
} |
| 1103 |
}, |
| 1104 |
DateTimeDeserializationStage::NumberLong => { |
| 1105 |
self.stage = DateTimeDeserializationStage::Done; |
| 1106 |
visitor.visit_string(self.dt.timestamp_millis().to_string()) |
| 1107 |
} |
| 1108 |
DateTimeDeserializationStage::Done => { |
| 1109 |
Err(Error::custom("DateTime fully deserialized already")) |
| 1110 |
} |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> |
| 1115 |
where |
| 1116 |
V: serde::de::Visitor<'de>, |
| 1117 |
{ |
| 1118 |
visitor.visit_newtype_struct(self) |
| 1119 |
} |
| 1120 |
|
| 1121 |
fn is_human_readable(&self) -> bool { |
| 1122 |
false |
| 1123 |
} |
| 1124 |
|
| 1125 |
serde::forward_to_deserialize_any! { |
| 1126 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 1127 |
bytes byte_buf map struct option unit |
| 1128 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
/// A [`MapAccess`] providing access to a BSON binary being deserialized. |
| 1133 |
/// |
| 1134 |
/// If hinted to be raw BSON, this deserializes the serde data model equivalent |
| 1135 |
/// of { "$binary": { "subType": <u8>, "bytes": <borrowed bytes> } }. |
| 1136 |
/// |
| 1137 |
/// Otherwise, this deserializes the serde data model equivalent of |
| 1138 |
/// { "$binary": { "subType": <hex string>, "base64": <base64 encoded data> } }. |
| 1139 |
struct BinaryAccess<'d, 'de> { |
| 1140 |
deserializer: &'d mut BinaryDeserializer<'de>, |
| 1141 |
} |
| 1142 |
|
| 1143 |
impl<'de, 'd> serde::de::MapAccess<'de> for BinaryAccess<'d, 'de> { |
| 1144 |
type Error = Error; |
| 1145 |
|
| 1146 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 1147 |
where |
| 1148 |
K: serde::de::DeserializeSeed<'de>, |
| 1149 |
{ |
| 1150 |
let field_name = match self.deserializer.stage { |
| 1151 |
BinaryDeserializationStage::TopLevel => "$binary", |
| 1152 |
BinaryDeserializationStage::Subtype => "subType", |
| 1153 |
BinaryDeserializationStage::Bytes => match self.deserializer.hint { |
| 1154 |
DeserializerHint::RawBson => "bytes", |
| 1155 |
_ => "base64", |
| 1156 |
}, |
| 1157 |
BinaryDeserializationStage::Done => return Ok(None), |
| 1158 |
}; |
| 1159 |
|
| 1160 |
seed.deserialize(FieldDeserializer { field_name }).map(Some) |
| 1161 |
} |
| 1162 |
|
| 1163 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 1164 |
where |
| 1165 |
V: serde::de::DeserializeSeed<'de>, |
| 1166 |
{ |
| 1167 |
seed.deserialize(&mut *self.deserializer) |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
struct BinaryDeserializer<'a> { |
| 1172 |
binary: RawBinaryRef<'a>, |
| 1173 |
hint: DeserializerHint, |
| 1174 |
stage: BinaryDeserializationStage, |
| 1175 |
} |
| 1176 |
|
| 1177 |
impl<'a> BinaryDeserializer<'a> { |
| 1178 |
fn new(binary: RawBinaryRef<'a>, hint: DeserializerHint) -> Self { |
| 1179 |
Self { |
| 1180 |
binary, |
| 1181 |
hint, |
| 1182 |
stage: BinaryDeserializationStage::TopLevel, |
| 1183 |
} |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut BinaryDeserializer<'de> { |
| 1188 |
type Error = Error; |
| 1189 |
|
| 1190 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 1191 |
where |
| 1192 |
V: serde::de::Visitor<'de>, |
| 1193 |
{ |
| 1194 |
match self.stage { |
| 1195 |
BinaryDeserializationStage::TopLevel => { |
| 1196 |
self.stage = BinaryDeserializationStage::Subtype; |
| 1197 |
visitor.visit_map(BinaryAccess { deserializer: self }) |
| 1198 |
} |
| 1199 |
BinaryDeserializationStage::Subtype => { |
| 1200 |
self.stage = BinaryDeserializationStage::Bytes; |
| 1201 |
match self.hint { |
| 1202 |
DeserializerHint::RawBson => visitor.visit_u8(self.binary.subtype.into()), |
| 1203 |
_ => visitor.visit_string(hex::encode([u8::from(self.binary.subtype)])), |
| 1204 |
} |
| 1205 |
} |
| 1206 |
BinaryDeserializationStage::Bytes => { |
| 1207 |
self.stage = BinaryDeserializationStage::Done; |
| 1208 |
match self.hint { |
| 1209 |
DeserializerHint::RawBson => visitor.visit_borrowed_bytes(self.binary.bytes), |
| 1210 |
_ => visitor.visit_string(base64::encode(self.binary.bytes)), |
| 1211 |
} |
| 1212 |
} |
| 1213 |
BinaryDeserializationStage::Done => { |
| 1214 |
Err(Error::custom("Binary fully deserialized already")) |
| 1215 |
} |
| 1216 |
} |
| 1217 |
} |
| 1218 |
|
| 1219 |
fn is_human_readable(&self) -> bool { |
| 1220 |
false |
| 1221 |
} |
| 1222 |
|
| 1223 |
serde::forward_to_deserialize_any! { |
| 1224 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 1225 |
bytes byte_buf map struct option unit newtype_struct |
| 1226 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
enum BinaryDeserializationStage { |
| 1231 |
TopLevel, |
| 1232 |
Subtype, |
| 1233 |
Bytes, |
| 1234 |
Done, |
| 1235 |
} |
| 1236 |
|
| 1237 |
/// A [`MapAccess`] providing access to a BSON code with scope being deserialized. |
| 1238 |
/// |
| 1239 |
/// If hinted to be raw BSON, this deserializes the serde data model equivalent |
| 1240 |
/// of { "$code": <borrowed str>, "$scope": <&RawDocument> } }. |
| 1241 |
/// |
| 1242 |
/// Otherwise, this deserializes the serde data model equivalent of |
| 1243 |
/// { "$code": <borrowed str> "$scope": <map> }. |
| 1244 |
struct CodeWithScopeAccess<'de, 'd, 'a> { |
| 1245 |
deserializer: &'a mut CodeWithScopeDeserializer<'de, 'd>, |
| 1246 |
} |
| 1247 |
|
| 1248 |
impl<'de, 'd, 'a> CodeWithScopeAccess<'de, 'd, 'a> { |
| 1249 |
fn new(deserializer: &'a mut CodeWithScopeDeserializer<'de, 'd>) -> Self { |
| 1250 |
Self { deserializer } |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
impl<'de, 'd, 'a> serde::de::MapAccess<'de> for CodeWithScopeAccess<'de, 'd, 'a> { |
| 1255 |
type Error = Error; |
| 1256 |
|
| 1257 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 1258 |
where |
| 1259 |
K: serde::de::DeserializeSeed<'de>, |
| 1260 |
{ |
| 1261 |
match self.deserializer.stage { |
| 1262 |
CodeWithScopeDeserializationStage::Code => seed |
| 1263 |
.deserialize(FieldDeserializer { |
| 1264 |
field_name: "$code", |
| 1265 |
}) |
| 1266 |
.map(Some), |
| 1267 |
CodeWithScopeDeserializationStage::Scope => seed |
| 1268 |
.deserialize(FieldDeserializer { |
| 1269 |
field_name: "$scope", |
| 1270 |
}) |
| 1271 |
.map(Some), |
| 1272 |
CodeWithScopeDeserializationStage::Done => Ok(None), |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 1277 |
where |
| 1278 |
V: serde::de::DeserializeSeed<'de>, |
| 1279 |
{ |
| 1280 |
seed.deserialize(&mut *self.deserializer) |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
struct CodeWithScopeDeserializer<'de, 'a> { |
| 1285 |
root_deserializer: &'a mut Deserializer<'de>, |
| 1286 |
stage: CodeWithScopeDeserializationStage, |
| 1287 |
hint: DeserializerHint, |
| 1288 |
length_remaining: i32, |
| 1289 |
} |
| 1290 |
|
| 1291 |
impl<'de, 'a> CodeWithScopeDeserializer<'de, 'a> { |
| 1292 |
fn new(root_deserializer: &'a mut Deserializer<'de>, hint: DeserializerHint, len: i32) -> Self { |
| 1293 |
Self { |
| 1294 |
root_deserializer, |
| 1295 |
stage: CodeWithScopeDeserializationStage::Code, |
| 1296 |
hint, |
| 1297 |
length_remaining: len, |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
/// Executes a closure that reads from the BSON bytes and returns an error if the number of |
| 1302 |
/// bytes read exceeds length_remaining. |
| 1303 |
/// |
| 1304 |
/// A mutable reference to this [`CodeWithScopeDeserializer`] is passed into the closure. |
| 1305 |
fn read<F, O>(&mut self, f: F) -> Result<O> |
| 1306 |
where |
| 1307 |
F: FnOnce(&mut Self) -> Result<O>, |
| 1308 |
{ |
| 1309 |
let start_bytes = self.root_deserializer.bytes.bytes_read(); |
| 1310 |
let out = f(self)?; |
| 1311 |
let bytes_read = self.root_deserializer.bytes.bytes_read() - start_bytes; |
| 1312 |
self.length_remaining -= bytes_read as i32; |
| 1313 |
|
| 1314 |
if self.length_remaining < 0 { |
| 1315 |
return Err(Error::custom("length of CodeWithScope too short")); |
| 1316 |
} |
| 1317 |
Ok(out) |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
impl<'de, 'a, 'b> serde::de::Deserializer<'de> for &'b mut CodeWithScopeDeserializer<'de, 'a> { |
| 1322 |
type Error = Error; |
| 1323 |
|
| 1324 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 1325 |
where |
| 1326 |
V: serde::de::Visitor<'de>, |
| 1327 |
{ |
| 1328 |
match self.stage { |
| 1329 |
CodeWithScopeDeserializationStage::Code => { |
| 1330 |
self.stage = CodeWithScopeDeserializationStage::Scope; |
| 1331 |
match self.read(|s| s.root_deserializer.deserialize_str())? { |
| 1332 |
Cow::Borrowed(s) => visitor.visit_borrowed_str(s), |
| 1333 |
Cow::Owned(s) => visitor.visit_string(s), |
| 1334 |
} |
| 1335 |
} |
| 1336 |
CodeWithScopeDeserializationStage::Scope => { |
| 1337 |
self.stage = CodeWithScopeDeserializationStage::Done; |
| 1338 |
self.read(|s| { |
| 1339 |
s.root_deserializer.deserialize_document( |
| 1340 |
visitor, |
| 1341 |
s.hint, |
| 1342 |
DocumentType::EmbeddedDocument, |
| 1343 |
) |
| 1344 |
}) |
| 1345 |
} |
| 1346 |
CodeWithScopeDeserializationStage::Done => Err(Error::custom( |
| 1347 |
"JavaScriptCodeWithScope fully deserialized already", |
| 1348 |
)), |
| 1349 |
} |
| 1350 |
} |
| 1351 |
|
| 1352 |
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> |
| 1353 |
where |
| 1354 |
V: serde::de::Visitor<'de>, |
| 1355 |
{ |
| 1356 |
visitor.visit_newtype_struct(self) |
| 1357 |
} |
| 1358 |
|
| 1359 |
fn is_human_readable(&self) -> bool { |
| 1360 |
false |
| 1361 |
} |
| 1362 |
|
| 1363 |
serde::forward_to_deserialize_any! { |
| 1364 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 1365 |
bytes byte_buf map struct option unit |
| 1366 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 1367 |
} |
| 1368 |
} |
| 1369 |
|
| 1370 |
#[derive(Debug)] |
| 1371 |
enum CodeWithScopeDeserializationStage { |
| 1372 |
Code, |
| 1373 |
Scope, |
| 1374 |
Done, |
| 1375 |
} |
| 1376 |
|
| 1377 |
/// A [`MapAccess`] providing access to a BSON DB pointer being deserialized. |
| 1378 |
/// |
| 1379 |
/// Regardless of the hint, this deserializes the serde data model equivalent |
| 1380 |
/// of { "$dbPointer": { "$ref": <borrowed str>, "$id": <bytes> } }. |
| 1381 |
struct DbPointerAccess<'de, 'd, 'a> { |
| 1382 |
deserializer: &'a mut DbPointerDeserializer<'de, 'd>, |
| 1383 |
} |
| 1384 |
|
| 1385 |
impl<'de, 'd, 'a> DbPointerAccess<'de, 'd, 'a> { |
| 1386 |
fn new(deserializer: &'a mut DbPointerDeserializer<'de, 'd>) -> Self { |
| 1387 |
Self { deserializer } |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
impl<'de, 'd, 'a> serde::de::MapAccess<'de> for DbPointerAccess<'de, 'd, 'a> { |
| 1392 |
type Error = Error; |
| 1393 |
|
| 1394 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 1395 |
where |
| 1396 |
K: serde::de::DeserializeSeed<'de>, |
| 1397 |
{ |
| 1398 |
match self.deserializer.stage { |
| 1399 |
DbPointerDeserializationStage::TopLevel => seed |
| 1400 |
.deserialize(FieldDeserializer { |
| 1401 |
field_name: "$dbPointer", |
| 1402 |
}) |
| 1403 |
.map(Some), |
| 1404 |
DbPointerDeserializationStage::Namespace => seed |
| 1405 |
.deserialize(FieldDeserializer { field_name: "$ref" }) |
| 1406 |
.map(Some), |
| 1407 |
DbPointerDeserializationStage::Id => seed |
| 1408 |
.deserialize(FieldDeserializer { field_name: "$id" }) |
| 1409 |
.map(Some), |
| 1410 |
DbPointerDeserializationStage::Done => Ok(None), |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 1415 |
where |
| 1416 |
V: serde::de::DeserializeSeed<'de>, |
| 1417 |
{ |
| 1418 |
seed.deserialize(&mut *self.deserializer) |
| 1419 |
} |
| 1420 |
} |
| 1421 |
|
| 1422 |
struct DbPointerDeserializer<'de, 'a> { |
| 1423 |
root_deserializer: &'a mut Deserializer<'de>, |
| 1424 |
stage: DbPointerDeserializationStage, |
| 1425 |
hint: DeserializerHint, |
| 1426 |
} |
| 1427 |
|
| 1428 |
impl<'de, 'a> DbPointerDeserializer<'de, 'a> { |
| 1429 |
fn new(root_deserializer: &'a mut Deserializer<'de>, hint: DeserializerHint) -> Self { |
| 1430 |
Self { |
| 1431 |
root_deserializer, |
| 1432 |
stage: DbPointerDeserializationStage::TopLevel, |
| 1433 |
hint, |
| 1434 |
} |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
impl<'de, 'a, 'b> serde::de::Deserializer<'de> for &'b mut DbPointerDeserializer<'de, 'a> { |
| 1439 |
type Error = Error; |
| 1440 |
|
| 1441 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 1442 |
where |
| 1443 |
V: serde::de::Visitor<'de>, |
| 1444 |
{ |
| 1445 |
match self.stage { |
| 1446 |
DbPointerDeserializationStage::TopLevel => { |
| 1447 |
self.stage = DbPointerDeserializationStage::Namespace; |
| 1448 |
visitor.visit_map(DbPointerAccess::new(self)) |
| 1449 |
} |
| 1450 |
DbPointerDeserializationStage::Namespace => { |
| 1451 |
self.stage = DbPointerDeserializationStage::Id; |
| 1452 |
match self.root_deserializer.deserialize_str()? { |
| 1453 |
Cow::Borrowed(s) => visitor.visit_borrowed_str(s), |
| 1454 |
Cow::Owned(s) => visitor.visit_string(s), |
| 1455 |
} |
| 1456 |
} |
| 1457 |
DbPointerDeserializationStage::Id => { |
| 1458 |
self.stage = DbPointerDeserializationStage::Done; |
| 1459 |
self.root_deserializer |
| 1460 |
.deserialize_objectid(visitor, self.hint) |
| 1461 |
} |
| 1462 |
DbPointerDeserializationStage::Done => { |
| 1463 |
Err(Error::custom("DbPointer fully deserialized already")) |
| 1464 |
} |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> |
| 1469 |
where |
| 1470 |
V: serde::de::Visitor<'de>, |
| 1471 |
{ |
| 1472 |
visitor.visit_newtype_struct(self) |
| 1473 |
} |
| 1474 |
|
| 1475 |
fn is_human_readable(&self) -> bool { |
| 1476 |
false |
| 1477 |
} |
| 1478 |
|
| 1479 |
serde::forward_to_deserialize_any! { |
| 1480 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 1481 |
bytes byte_buf map struct option unit |
| 1482 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
#[derive(Debug)] |
| 1487 |
enum DbPointerDeserializationStage { |
| 1488 |
TopLevel, |
| 1489 |
Namespace, |
| 1490 |
Id, |
| 1491 |
Done, |
| 1492 |
} |
| 1493 |
|
| 1494 |
/// A [`MapAccess`] providing access to a BSON regular expression being deserialized. |
| 1495 |
/// |
| 1496 |
/// Regardless of the hint, this deserializes the serde data model equivalent |
| 1497 |
/// of { "$regularExpression": { "pattern": <borrowed str>, "options": <borrowed str> } }. |
| 1498 |
struct RegexAccess<'de, 'd, 'a> { |
| 1499 |
deserializer: &'a mut RegexDeserializer<'de, 'd>, |
| 1500 |
} |
| 1501 |
|
| 1502 |
impl<'de, 'd, 'a> RegexAccess<'de, 'd, 'a> { |
| 1503 |
fn new(deserializer: &'a mut RegexDeserializer<'de, 'd>) -> Self { |
| 1504 |
Self { deserializer } |
| 1505 |
} |
| 1506 |
} |
| 1507 |
|
| 1508 |
impl<'de, 'd, 'a> serde::de::MapAccess<'de> for RegexAccess<'de, 'd, 'a> { |
| 1509 |
type Error = Error; |
| 1510 |
|
| 1511 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 1512 |
where |
| 1513 |
K: serde::de::DeserializeSeed<'de>, |
| 1514 |
{ |
| 1515 |
match self.deserializer.stage { |
| 1516 |
RegexDeserializationStage::TopLevel => seed |
| 1517 |
.deserialize(FieldDeserializer { |
| 1518 |
field_name: "$regularExpression", |
| 1519 |
}) |
| 1520 |
.map(Some), |
| 1521 |
RegexDeserializationStage::Pattern => seed |
| 1522 |
.deserialize(FieldDeserializer { |
| 1523 |
field_name: "pattern", |
| 1524 |
}) |
| 1525 |
.map(Some), |
| 1526 |
RegexDeserializationStage::Options => seed |
| 1527 |
.deserialize(FieldDeserializer { |
| 1528 |
field_name: "options", |
| 1529 |
}) |
| 1530 |
.map(Some), |
| 1531 |
RegexDeserializationStage::Done => Ok(None), |
| 1532 |
} |
| 1533 |
} |
| 1534 |
|
| 1535 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 1536 |
where |
| 1537 |
V: serde::de::DeserializeSeed<'de>, |
| 1538 |
{ |
| 1539 |
seed.deserialize(&mut *self.deserializer) |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
struct RegexDeserializer<'de, 'a> { |
| 1544 |
root_deserializer: &'a mut Deserializer<'de>, |
| 1545 |
stage: RegexDeserializationStage, |
| 1546 |
} |
| 1547 |
|
| 1548 |
impl<'de, 'a> RegexDeserializer<'de, 'a> { |
| 1549 |
fn new(root_deserializer: &'a mut Deserializer<'de>) -> Self { |
| 1550 |
Self { |
| 1551 |
root_deserializer, |
| 1552 |
stage: RegexDeserializationStage::TopLevel, |
| 1553 |
} |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
impl<'de, 'a, 'b> serde::de::Deserializer<'de> for &'b mut RegexDeserializer<'de, 'a> { |
| 1558 |
type Error = Error; |
| 1559 |
|
| 1560 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 1561 |
where |
| 1562 |
V: serde::de::Visitor<'de>, |
| 1563 |
{ |
| 1564 |
match self.stage { |
| 1565 |
RegexDeserializationStage::TopLevel => { |
| 1566 |
self.stage.advance(); |
| 1567 |
visitor.visit_map(RegexAccess::new(self)) |
| 1568 |
} |
| 1569 |
RegexDeserializationStage::Pattern | RegexDeserializationStage::Options => { |
| 1570 |
self.stage.advance(); |
| 1571 |
match self.root_deserializer.deserialize_cstr()? { |
| 1572 |
Cow::Borrowed(s) => visitor.visit_borrowed_str(s), |
| 1573 |
Cow::Owned(s) => visitor.visit_string(s), |
| 1574 |
} |
| 1575 |
} |
| 1576 |
RegexDeserializationStage::Done => { |
| 1577 |
Err(Error::custom("DbPointer fully deserialized already")) |
| 1578 |
} |
| 1579 |
} |
| 1580 |
} |
| 1581 |
|
| 1582 |
fn is_human_readable(&self) -> bool { |
| 1583 |
false |
| 1584 |
} |
| 1585 |
|
| 1586 |
serde::forward_to_deserialize_any! { |
| 1587 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 1588 |
bytes byte_buf map struct option unit newtype_struct |
| 1589 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
#[derive(Debug)] |
| 1594 |
enum RegexDeserializationStage { |
| 1595 |
TopLevel, |
| 1596 |
Pattern, |
| 1597 |
Options, |
| 1598 |
Done, |
| 1599 |
} |
| 1600 |
|
| 1601 |
impl RegexDeserializationStage { |
| 1602 |
fn advance(&mut self) { |
| 1603 |
*self = match self { |
| 1604 |
RegexDeserializationStage::TopLevel => RegexDeserializationStage::Pattern, |
| 1605 |
RegexDeserializationStage::Pattern => RegexDeserializationStage::Options, |
| 1606 |
RegexDeserializationStage::Options => RegexDeserializationStage::Done, |
| 1607 |
RegexDeserializationStage::Done => RegexDeserializationStage::Done, |
| 1608 |
} |
| 1609 |
} |
| 1610 |
} |
| 1611 |
|
| 1612 |
/// Helper access struct for visiting the extended JSON model of simple BSON types. |
| 1613 |
/// e.g. Symbol, Timestamp, etc. |
| 1614 |
struct RawBsonAccess<'a> { |
| 1615 |
key: &'static str, |
| 1616 |
value: BsonContent<'a>, |
| 1617 |
first: bool, |
| 1618 |
} |
| 1619 |
|
| 1620 |
/// Enum value representing some cached BSON data needed to represent a given |
| 1621 |
/// BSON type's extended JSON model. |
| 1622 |
#[derive(Debug, Clone, Copy)] |
| 1623 |
enum BsonContent<'a> { |
| 1624 |
Str(&'a str), |
| 1625 |
Int32(i32), |
| 1626 |
Boolean(bool), |
| 1627 |
} |
| 1628 |
|
| 1629 |
impl<'a> RawBsonAccess<'a> { |
| 1630 |
fn new(key: &'static str, value: BsonContent<'a>) -> Self { |
| 1631 |
Self { |
| 1632 |
key, |
| 1633 |
value, |
| 1634 |
first: true, |
| 1635 |
} |
| 1636 |
} |
| 1637 |
} |
| 1638 |
|
| 1639 |
impl<'de> MapAccess<'de> for RawBsonAccess<'de> { |
| 1640 |
type Error = Error; |
| 1641 |
|
| 1642 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
| 1643 |
where |
| 1644 |
K: serde::de::DeserializeSeed<'de>, |
| 1645 |
{ |
| 1646 |
if self.first { |
| 1647 |
self.first = false; |
| 1648 |
seed.deserialize(FieldDeserializer { |
| 1649 |
field_name: self.key, |
| 1650 |
}) |
| 1651 |
.map(Some) |
| 1652 |
} else { |
| 1653 |
Ok(None) |
| 1654 |
} |
| 1655 |
} |
| 1656 |
|
| 1657 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
| 1658 |
where |
| 1659 |
V: serde::de::DeserializeSeed<'de>, |
| 1660 |
{ |
| 1661 |
seed.deserialize(RawBsonDeserializer { value: self.value }) |
| 1662 |
} |
| 1663 |
} |
| 1664 |
|
| 1665 |
struct RawBsonDeserializer<'a> { |
| 1666 |
value: BsonContent<'a>, |
| 1667 |
} |
| 1668 |
|
| 1669 |
impl<'de> serde::de::Deserializer<'de> for RawBsonDeserializer<'de> { |
| 1670 |
type Error = Error; |
| 1671 |
|
| 1672 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
| 1673 |
where |
| 1674 |
V: serde::de::Visitor<'de>, |
| 1675 |
{ |
| 1676 |
match self.value { |
| 1677 |
BsonContent::Boolean(b) => visitor.visit_bool(b), |
| 1678 |
BsonContent::Str(s) => visitor.visit_borrowed_str(s), |
| 1679 |
BsonContent::Int32(i) => visitor.visit_i32(i), |
| 1680 |
} |
| 1681 |
} |
| 1682 |
|
| 1683 |
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> |
| 1684 |
where |
| 1685 |
V: serde::de::Visitor<'de>, |
| 1686 |
{ |
| 1687 |
visitor.visit_newtype_struct(self) |
| 1688 |
} |
| 1689 |
|
| 1690 |
fn is_human_readable(&self) -> bool { |
| 1691 |
false |
| 1692 |
} |
| 1693 |
|
| 1694 |
serde::forward_to_deserialize_any! { |
| 1695 |
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 1696 |
bytes byte_buf map struct option unit |
| 1697 |
ignored_any unit_struct tuple_struct tuple enum identifier |
| 1698 |
} |
| 1699 |
} |
| 1700 |
|
| 1701 |
/// Struct wrapping a slice of BSON bytes. |
| 1702 |
struct BsonBuf<'a> { |
| 1703 |
bytes: &'a [u8], |
| 1704 |
index: usize, |
| 1705 |
|
| 1706 |
/// Whether or not to insert replacement characters in place of invalid UTF-8 sequences when |
| 1707 |
/// deserializing strings. |
| 1708 |
utf8_lossy: bool, |
| 1709 |
} |
| 1710 |
|
| 1711 |
impl<'a> Read for BsonBuf<'a> { |
| 1712 |
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 1713 |
self.index_check()?; |
| 1714 |
let bytes_read = self.bytes[self.index..].as_ref().read(buf)?; |
| 1715 |
self.index += bytes_read; |
| 1716 |
Ok(bytes_read) |
| 1717 |
} |
| 1718 |
} |
| 1719 |
|
| 1720 |
impl<'a> BsonBuf<'a> { |
| 1721 |
fn new(bytes: &'a [u8], utf8_lossy: bool) -> Self { |
| 1722 |
Self { |
| 1723 |
bytes, |
| 1724 |
index: 0, |
| 1725 |
utf8_lossy, |
| 1726 |
} |
| 1727 |
} |
| 1728 |
|
| 1729 |
fn bytes_read(&self) -> usize { |
| 1730 |
self.index |
| 1731 |
} |
| 1732 |
|
| 1733 |
fn bytes_remaining(&self) -> usize { |
| 1734 |
self.bytes.len() - self.bytes_read() |
| 1735 |
} |
| 1736 |
|
| 1737 |
/// Verify the index has not run out of bounds. |
| 1738 |
fn index_check(&self) -> std::io::Result<()> { |
| 1739 |
if self.index >= self.bytes.len() { |
| 1740 |
return Err(ErrorKind::UnexpectedEof.into()); |
| 1741 |
} |
| 1742 |
Ok(()) |
| 1743 |
} |
| 1744 |
|
| 1745 |
/// Get the string starting at the provided index and ending at the buffer's current index. |
| 1746 |
/// |
| 1747 |
/// Can optionally override the global UTF-8 lossy setting to ensure bytes are not allocated. |
| 1748 |
fn str(&mut self, start: usize, utf8_lossy_override: Option<bool>) -> Result<Cow<'a, str>> { |
| 1749 |
let bytes = &self.bytes[start..self.index]; |
| 1750 |
let s = if utf8_lossy_override.unwrap_or(self.utf8_lossy) { |
| 1751 |
String::from_utf8_lossy(bytes) |
| 1752 |
} else { |
| 1753 |
Cow::Borrowed(std::str::from_utf8(bytes).map_err(Error::custom)?) |
| 1754 |
}; |
| 1755 |
|
| 1756 |
// consume the null byte |
| 1757 |
if self.bytes[self.index] != 0 { |
| 1758 |
return Err(Error::custom("string was not null-terminated")); |
| 1759 |
} |
| 1760 |
self.index += 1; |
| 1761 |
self.index_check()?; |
| 1762 |
|
| 1763 |
Ok(s) |
| 1764 |
} |
| 1765 |
|
| 1766 |
/// Attempts to read a null-terminated UTF-8 cstring from the data. |
| 1767 |
/// |
| 1768 |
/// If utf8_lossy and invalid UTF-8 is encountered, the unicode replacement character will be |
| 1769 |
/// inserted in place of the offending data, resulting in an owned [`String`]. Otherwise, the |
| 1770 |
/// data will be borrowed as-is. |
| 1771 |
fn read_cstr(&mut self) -> Result<Cow<'a, str>> { |
| 1772 |
let start = self.index; |
| 1773 |
while self.index < self.bytes.len() && self.bytes[self.index] != 0 { |
| 1774 |
self.index += 1 |
| 1775 |
} |
| 1776 |
|
| 1777 |
self.index_check()?; |
| 1778 |
|
| 1779 |
self.str(start, None) |
| 1780 |
} |
| 1781 |
|
| 1782 |
fn _advance_to_len_encoded_str(&mut self) -> Result<usize> { |
| 1783 |
let len = read_i32(self)?; |
| 1784 |
let start = self.index; |
| 1785 |
|
| 1786 |
// UTF-8 String must have at least 1 byte (the last 0x00). |
| 1787 |
if len < 1 { |
| 1788 |
return Err(Error::invalid_length( |
| 1789 |
len as usize, |
| 1790 |
&"UTF-8 string must have at least 1 byte", |
| 1791 |
)); |
| 1792 |
} |
| 1793 |
|
| 1794 |
self.index += (len - 1) as usize; |
| 1795 |
self.index_check()?; |
| 1796 |
|
| 1797 |
Ok(start) |
| 1798 |
} |
| 1799 |
|
| 1800 |
/// Attempts to read a null-terminated UTF-8 string from the data. |
| 1801 |
/// |
| 1802 |
/// If invalid UTF-8 is encountered, the unicode replacement character will be inserted in place |
| 1803 |
/// of the offending data, resulting in an owned [`String`]. Otherwise, the data will be |
| 1804 |
/// borrowed as-is. |
| 1805 |
fn read_str(&mut self) -> Result<Cow<'a, str>> { |
| 1806 |
let start = self._advance_to_len_encoded_str()?; |
| 1807 |
self.str(start, None) |
| 1808 |
} |
| 1809 |
|
| 1810 |
/// Attempts to read a null-terminated UTF-8 string from the data. |
| 1811 |
fn read_borrowed_str(&mut self) -> Result<&'a str> { |
| 1812 |
let start = self._advance_to_len_encoded_str()?; |
| 1813 |
match self.str(start, Some(false))? { |
| 1814 |
Cow::Borrowed(s) => Ok(s), |
| 1815 |
Cow::Owned(_) => panic!("should have errored when encountering invalid UTF-8"), |
| 1816 |
} |
| 1817 |
} |
| 1818 |
|
| 1819 |
fn slice(&self, length: usize) -> Result<&'a [u8]> { |
| 1820 |
if self.index + length > self.bytes.len() { |
| 1821 |
return Err(Error::Io(Arc::new( |
| 1822 |
std::io::ErrorKind::UnexpectedEof.into(), |
| 1823 |
))); |
| 1824 |
} |
| 1825 |
|
| 1826 |
Ok(&self.bytes[self.index..(self.index + length)]) |
| 1827 |
} |
| 1828 |
|
| 1829 |
fn read_slice(&mut self, length: usize) -> Result<&'a [u8]> { |
| 1830 |
let slice = self.slice(length)?; |
| 1831 |
self.index += length; |
| 1832 |
Ok(slice) |
| 1833 |
} |
| 1834 |
} |