| 1 |
use std::{borrow::Cow, convert::TryFrom}; |
| 2 |
|
| 3 |
use serde::de::{Error as SerdeError, Visitor}; |
| 4 |
use serde_bytes::ByteBuf; |
| 5 |
|
| 6 |
use crate::{ |
| 7 |
de::convert_unsigned_to_signed_raw, |
| 8 |
extjson::models::{ |
| 9 |
BorrowedBinaryBody, |
| 10 |
BorrowedDbPointerBody, |
| 11 |
BorrowedRegexBody, |
| 12 |
TimestampBody, |
| 13 |
}, |
| 14 |
oid::ObjectId, |
| 15 |
raw::{RAW_ARRAY_NEWTYPE, RAW_DOCUMENT_NEWTYPE}, |
| 16 |
spec::BinarySubtype, |
| 17 |
Binary, |
| 18 |
DateTime, |
| 19 |
DbPointer, |
| 20 |
Decimal128, |
| 21 |
RawArray, |
| 22 |
RawArrayBuf, |
| 23 |
RawBinaryRef, |
| 24 |
RawBson, |
| 25 |
RawBsonRef, |
| 26 |
RawDbPointerRef, |
| 27 |
RawDocument, |
| 28 |
RawDocumentBuf, |
| 29 |
RawJavaScriptCodeWithScope, |
| 30 |
RawJavaScriptCodeWithScopeRef, |
| 31 |
RawRegexRef, |
| 32 |
Regex, |
| 33 |
Timestamp, |
| 34 |
}; |
| 35 |
|
| 36 |
use super::{ |
| 37 |
CowByteBuffer, |
| 38 |
CowStr, |
| 39 |
OwnedOrBorrowedRawBson, |
| 40 |
OwnedOrBorrowedRawDocument, |
| 41 |
SeededVisitor, |
| 42 |
}; |
| 43 |
|
| 44 |
/// A visitor used to deserialize types backed by raw BSON. |
| 45 |
pub(crate) struct OwnedOrBorrowedRawBsonVisitor; |
| 46 |
|
| 47 |
impl<'de> Visitor<'de> for OwnedOrBorrowedRawBsonVisitor { |
| 48 |
type Value = OwnedOrBorrowedRawBson<'de>; |
| 49 |
|
| 50 |
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 51 |
write!(formatter, "a raw BSON value") |
| 52 |
} |
| 53 |
|
| 54 |
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> |
| 55 |
where |
| 56 |
E: SerdeError, |
| 57 |
{ |
| 58 |
Ok(RawBsonRef::String(v).into()) |
| 59 |
} |
| 60 |
|
| 61 |
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
| 62 |
where |
| 63 |
E: SerdeError, |
| 64 |
{ |
| 65 |
Ok(RawBson::String(v.to_string()).into()) |
| 66 |
} |
| 67 |
|
| 68 |
fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
| 69 |
where |
| 70 |
E: SerdeError, |
| 71 |
{ |
| 72 |
Ok(RawBson::String(v).into()) |
| 73 |
} |
| 74 |
|
| 75 |
fn visit_borrowed_bytes<E>(self, bytes: &'de [u8]) -> Result<Self::Value, E> |
| 76 |
where |
| 77 |
E: SerdeError, |
| 78 |
{ |
| 79 |
Ok(RawBsonRef::Binary(RawBinaryRef { |
| 80 |
bytes, |
| 81 |
subtype: BinarySubtype::Generic, |
| 82 |
}) |
| 83 |
.into()) |
| 84 |
} |
| 85 |
|
| 86 |
fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E> |
| 87 |
where |
| 88 |
E: SerdeError, |
| 89 |
{ |
| 90 |
Ok(RawBsonRef::Int32(v.into()).into()) |
| 91 |
} |
| 92 |
|
| 93 |
fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E> |
| 94 |
where |
| 95 |
E: SerdeError, |
| 96 |
{ |
| 97 |
Ok(RawBsonRef::Int32(v.into()).into()) |
| 98 |
} |
| 99 |
|
| 100 |
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E> |
| 101 |
where |
| 102 |
E: SerdeError, |
| 103 |
{ |
| 104 |
Ok(RawBsonRef::Int32(v).into()) |
| 105 |
} |
| 106 |
|
| 107 |
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> |
| 108 |
where |
| 109 |
E: SerdeError, |
| 110 |
{ |
| 111 |
Ok(RawBsonRef::Int64(v).into()) |
| 112 |
} |
| 113 |
|
| 114 |
fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E> |
| 115 |
where |
| 116 |
E: SerdeError, |
| 117 |
{ |
| 118 |
Ok(convert_unsigned_to_signed_raw(value.into())?.into()) |
| 119 |
} |
| 120 |
|
| 121 |
fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E> |
| 122 |
where |
| 123 |
E: SerdeError, |
| 124 |
{ |
| 125 |
Ok(convert_unsigned_to_signed_raw(value.into())?.into()) |
| 126 |
} |
| 127 |
|
| 128 |
fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E> |
| 129 |
where |
| 130 |
E: SerdeError, |
| 131 |
{ |
| 132 |
Ok(convert_unsigned_to_signed_raw(value.into())?.into()) |
| 133 |
} |
| 134 |
|
| 135 |
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> |
| 136 |
where |
| 137 |
E: SerdeError, |
| 138 |
{ |
| 139 |
Ok(convert_unsigned_to_signed_raw(value)?.into()) |
| 140 |
} |
| 141 |
|
| 142 |
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E> |
| 143 |
where |
| 144 |
E: SerdeError, |
| 145 |
{ |
| 146 |
Ok(RawBsonRef::Boolean(v).into()) |
| 147 |
} |
| 148 |
|
| 149 |
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E> |
| 150 |
where |
| 151 |
E: SerdeError, |
| 152 |
{ |
| 153 |
Ok(RawBsonRef::Double(v).into()) |
| 154 |
} |
| 155 |
|
| 156 |
fn visit_none<E>(self) -> Result<Self::Value, E> |
| 157 |
where |
| 158 |
E: SerdeError, |
| 159 |
{ |
| 160 |
Ok(RawBsonRef::Null.into()) |
| 161 |
} |
| 162 |
|
| 163 |
fn visit_unit<E>(self) -> Result<Self::Value, E> |
| 164 |
where |
| 165 |
E: SerdeError, |
| 166 |
{ |
| 167 |
Ok(RawBsonRef::Null.into()) |
| 168 |
} |
| 169 |
|
| 170 |
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
| 171 |
where |
| 172 |
D: serde::Deserializer<'de>, |
| 173 |
{ |
| 174 |
deserializer.deserialize_any(self) |
| 175 |
} |
| 176 |
|
| 177 |
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
| 178 |
where |
| 179 |
E: SerdeError, |
| 180 |
{ |
| 181 |
Ok(RawBson::Binary(Binary { |
| 182 |
bytes: v, |
| 183 |
subtype: BinarySubtype::Generic, |
| 184 |
}) |
| 185 |
.into()) |
| 186 |
} |
| 187 |
|
| 188 |
fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error> |
| 189 |
where |
| 190 |
A: serde::de::SeqAccess<'de>, |
| 191 |
{ |
| 192 |
let mut buffer = CowByteBuffer::new(); |
| 193 |
let seeded_visitor = SeededVisitor::new(&mut buffer); |
| 194 |
seeded_visitor.visit_seq(seq)?; |
| 195 |
|
| 196 |
match OwnedOrBorrowedRawDocument::try_from(buffer).map_err(SerdeError::custom)? { |
| 197 |
OwnedOrBorrowedRawDocument::Borrowed(borrowed) => { |
| 198 |
let raw_array = RawArray::from_doc(borrowed); |
| 199 |
Ok(RawBsonRef::Array(raw_array).into()) |
| 200 |
} |
| 201 |
OwnedOrBorrowedRawDocument::Owned(owned) => { |
| 202 |
let raw_array = RawArrayBuf::from_raw_document_buf(owned); |
| 203 |
Ok(RawBson::Array(raw_array).into()) |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
| 209 |
where |
| 210 |
A: serde::de::MapAccess<'de>, |
| 211 |
{ |
| 212 |
let first_key = match map.next_key::<CowStr>()? { |
| 213 |
Some(k) => k, |
| 214 |
None => return Ok(RawBson::Document(RawDocumentBuf::new()).into()), |
| 215 |
}; |
| 216 |
|
| 217 |
match first_key.0.as_ref() { |
| 218 |
"$oid" => { |
| 219 |
let oid: ObjectId = map.next_value()?; |
| 220 |
Ok(RawBsonRef::ObjectId(oid).into()) |
| 221 |
} |
| 222 |
"$symbol" => { |
| 223 |
let s: CowStr = map.next_value()?; |
| 224 |
match s.0 { |
| 225 |
Cow::Borrowed(s) => Ok(RawBsonRef::Symbol(s).into()), |
| 226 |
Cow::Owned(s) => Ok(RawBson::Symbol(s).into()), |
| 227 |
} |
| 228 |
} |
| 229 |
"$numberDecimalBytes" => { |
| 230 |
let bytes: ByteBuf = map.next_value()?; |
| 231 |
return Ok(RawBsonRef::Decimal128(Decimal128::deserialize_from_slice( |
| 232 |
bytes.as_ref(), |
| 233 |
)?) |
| 234 |
.into()); |
| 235 |
} |
| 236 |
"$regularExpression" => { |
| 237 |
let body: BorrowedRegexBody = map.next_value()?; |
| 238 |
|
| 239 |
match (body.pattern, body.options) { |
| 240 |
(Cow::Borrowed(p), Cow::Borrowed(o)) => { |
| 241 |
Ok(RawBsonRef::RegularExpression(RawRegexRef { |
| 242 |
pattern: p, |
| 243 |
options: o, |
| 244 |
}) |
| 245 |
.into()) |
| 246 |
} |
| 247 |
(p, o) => Ok(RawBson::RegularExpression(Regex { |
| 248 |
pattern: p.into_owned(), |
| 249 |
options: o.into_owned(), |
| 250 |
}) |
| 251 |
.into()), |
| 252 |
} |
| 253 |
} |
| 254 |
"$undefined" => { |
| 255 |
let _: bool = map.next_value()?; |
| 256 |
Ok(RawBsonRef::Undefined.into()) |
| 257 |
} |
| 258 |
"$binary" => { |
| 259 |
let v: BorrowedBinaryBody = map.next_value()?; |
| 260 |
|
| 261 |
if let Cow::Borrowed(bytes) = v.bytes { |
| 262 |
Ok(RawBsonRef::Binary(RawBinaryRef { |
| 263 |
bytes, |
| 264 |
subtype: v.subtype.into(), |
| 265 |
}) |
| 266 |
.into()) |
| 267 |
} else { |
| 268 |
Ok(RawBson::Binary(Binary { |
| 269 |
bytes: v.bytes.into_owned(), |
| 270 |
subtype: v.subtype.into(), |
| 271 |
}) |
| 272 |
.into()) |
| 273 |
} |
| 274 |
} |
| 275 |
"$date" => { |
| 276 |
let date: i64 = map.next_value()?; |
| 277 |
Ok(RawBsonRef::DateTime(DateTime::from_millis(date)).into()) |
| 278 |
} |
| 279 |
"$timestamp" => { |
| 280 |
let timestamp: TimestampBody = map.next_value()?; |
| 281 |
Ok(RawBsonRef::Timestamp(Timestamp { |
| 282 |
time: timestamp.t, |
| 283 |
increment: timestamp.i, |
| 284 |
}) |
| 285 |
.into()) |
| 286 |
} |
| 287 |
"$minKey" => { |
| 288 |
let _ = map.next_value::<i32>()?; |
| 289 |
Ok(RawBsonRef::MinKey.into()) |
| 290 |
} |
| 291 |
"$maxKey" => { |
| 292 |
let _ = map.next_value::<i32>()?; |
| 293 |
Ok(RawBsonRef::MaxKey.into()) |
| 294 |
} |
| 295 |
"$code" => { |
| 296 |
let code = map.next_value::<CowStr>()?; |
| 297 |
if let Some(key) = map.next_key::<CowStr>()? { |
| 298 |
if key.0.as_ref() == "$scope" { |
| 299 |
let scope = map.next_value::<OwnedOrBorrowedRawDocument>()?; |
| 300 |
match (code.0, scope) { |
| 301 |
(Cow::Borrowed(code), OwnedOrBorrowedRawDocument::Borrowed(scope)) => { |
| 302 |
Ok(RawBsonRef::JavaScriptCodeWithScope( |
| 303 |
RawJavaScriptCodeWithScopeRef { code, scope }, |
| 304 |
) |
| 305 |
.into()) |
| 306 |
} |
| 307 |
(code, scope) => Ok(RawBson::JavaScriptCodeWithScope( |
| 308 |
RawJavaScriptCodeWithScope { |
| 309 |
code: code.into_owned(), |
| 310 |
scope: scope.into_owned(), |
| 311 |
}, |
| 312 |
) |
| 313 |
.into()), |
| 314 |
} |
| 315 |
} else { |
| 316 |
Err(SerdeError::unknown_field(&key.0, &["$scope"])) |
| 317 |
} |
| 318 |
} else if let Cow::Borrowed(code) = code.0 { |
| 319 |
Ok(RawBsonRef::JavaScriptCode(code).into()) |
| 320 |
} else { |
| 321 |
Ok(RawBson::JavaScriptCode(code.0.into_owned()).into()) |
| 322 |
} |
| 323 |
} |
| 324 |
"$dbPointer" => { |
| 325 |
let db_pointer: BorrowedDbPointerBody = map.next_value()?; |
| 326 |
if let Cow::Borrowed(ns) = db_pointer.ns.0 { |
| 327 |
Ok(RawBsonRef::DbPointer(RawDbPointerRef { |
| 328 |
namespace: ns, |
| 329 |
id: db_pointer.id, |
| 330 |
}) |
| 331 |
.into()) |
| 332 |
} else { |
| 333 |
Ok(RawBson::DbPointer(DbPointer { |
| 334 |
namespace: db_pointer.ns.0.into_owned(), |
| 335 |
id: db_pointer.id, |
| 336 |
}) |
| 337 |
.into()) |
| 338 |
} |
| 339 |
} |
| 340 |
RAW_DOCUMENT_NEWTYPE => { |
| 341 |
let bson = map.next_value::<&[u8]>()?; |
| 342 |
let doc = RawDocument::from_bytes(bson).map_err(SerdeError::custom)?; |
| 343 |
Ok(RawBsonRef::Document(doc).into()) |
| 344 |
} |
| 345 |
RAW_ARRAY_NEWTYPE => { |
| 346 |
let bson = map.next_value::<&[u8]>()?; |
| 347 |
let doc = RawDocument::from_bytes(bson).map_err(SerdeError::custom)?; |
| 348 |
Ok(RawBsonRef::Array(RawArray::from_doc(doc)).into()) |
| 349 |
} |
| 350 |
_ => { |
| 351 |
let mut buffer = CowByteBuffer::new(); |
| 352 |
let seeded_visitor = SeededVisitor::new(&mut buffer); |
| 353 |
seeded_visitor.iterate_map(first_key, map)?; |
| 354 |
|
| 355 |
match OwnedOrBorrowedRawDocument::try_from(buffer).map_err(SerdeError::custom)? { |
| 356 |
OwnedOrBorrowedRawDocument::Borrowed(borrowed) => { |
| 357 |
Ok(RawBsonRef::Document(borrowed).into()) |
| 358 |
} |
| 359 |
OwnedOrBorrowedRawDocument::Owned(owned) => Ok(RawBson::Document(owned).into()), |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
} |