| 1 |
use std::convert::{TryFrom, TryInto}; |
| 2 |
|
| 3 |
use serde::{ser::SerializeStruct, Deserialize, Serialize}; |
| 4 |
use serde_bytes::Bytes; |
| 5 |
|
| 6 |
use super::{ |
| 7 |
bson::RawBson, |
| 8 |
serde::{bson_visitor::OwnedOrBorrowedRawBsonVisitor, OwnedOrBorrowedRawBson}, |
| 9 |
Error, |
| 10 |
RawArray, |
| 11 |
RawDocument, |
| 12 |
Result, |
| 13 |
}; |
| 14 |
use crate::{ |
| 15 |
extjson, |
| 16 |
oid::{self, ObjectId}, |
| 17 |
raw::{RawJavaScriptCodeWithScope, RAW_BSON_NEWTYPE}, |
| 18 |
spec::{BinarySubtype, ElementType}, |
| 19 |
Binary, |
| 20 |
Bson, |
| 21 |
DbPointer, |
| 22 |
Decimal128, |
| 23 |
RawArrayBuf, |
| 24 |
RawDocumentBuf, |
| 25 |
Regex, |
| 26 |
Timestamp, |
| 27 |
}; |
| 28 |
|
| 29 |
/// A BSON value referencing raw bytes stored elsewhere. |
| 30 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 31 |
pub enum RawBsonRef<'a> { |
| 32 |
/// 64-bit binary floating point |
| 33 |
Double(f64), |
| 34 |
/// UTF-8 string |
| 35 |
String(&'a str), |
| 36 |
/// Array |
| 37 |
Array(&'a RawArray), |
| 38 |
/// Embedded document |
| 39 |
Document(&'a RawDocument), |
| 40 |
/// Boolean value |
| 41 |
Boolean(bool), |
| 42 |
/// Null value |
| 43 |
Null, |
| 44 |
/// Regular expression |
| 45 |
RegularExpression(RawRegexRef<'a>), |
| 46 |
/// JavaScript code |
| 47 |
JavaScriptCode(&'a str), |
| 48 |
/// JavaScript code w/ scope |
| 49 |
JavaScriptCodeWithScope(RawJavaScriptCodeWithScopeRef<'a>), |
| 50 |
/// 32-bit signed integer |
| 51 |
Int32(i32), |
| 52 |
/// 64-bit signed integer |
| 53 |
Int64(i64), |
| 54 |
/// Timestamp |
| 55 |
Timestamp(Timestamp), |
| 56 |
/// Binary data |
| 57 |
Binary(RawBinaryRef<'a>), |
| 58 |
/// [ObjectId](http://dochub.mongodb.org/core/objectids) |
| 59 |
ObjectId(oid::ObjectId), |
| 60 |
/// UTC datetime |
| 61 |
DateTime(crate::DateTime), |
| 62 |
/// Symbol (Deprecated) |
| 63 |
Symbol(&'a str), |
| 64 |
/// [128-bit decimal floating point](https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst) |
| 65 |
Decimal128(Decimal128), |
| 66 |
/// Undefined value (Deprecated) |
| 67 |
Undefined, |
| 68 |
/// Max key |
| 69 |
MaxKey, |
| 70 |
/// Min key |
| 71 |
MinKey, |
| 72 |
/// DBPointer (Deprecated) |
| 73 |
DbPointer(RawDbPointerRef<'a>), |
| 74 |
} |
| 75 |
|
| 76 |
impl<'a> RawBsonRef<'a> { |
| 77 |
/// Get the [`ElementType`] of this value. |
| 78 |
pub fn element_type(&self) -> ElementType { |
| 79 |
match *self { |
| 80 |
RawBsonRef::Double(..) => ElementType::Double, |
| 81 |
RawBsonRef::String(..) => ElementType::String, |
| 82 |
RawBsonRef::Array(..) => ElementType::Array, |
| 83 |
RawBsonRef::Document(..) => ElementType::EmbeddedDocument, |
| 84 |
RawBsonRef::Boolean(..) => ElementType::Boolean, |
| 85 |
RawBsonRef::Null => ElementType::Null, |
| 86 |
RawBsonRef::RegularExpression(..) => ElementType::RegularExpression, |
| 87 |
RawBsonRef::JavaScriptCode(..) => ElementType::JavaScriptCode, |
| 88 |
RawBsonRef::JavaScriptCodeWithScope(..) => ElementType::JavaScriptCodeWithScope, |
| 89 |
RawBsonRef::Int32(..) => ElementType::Int32, |
| 90 |
RawBsonRef::Int64(..) => ElementType::Int64, |
| 91 |
RawBsonRef::Timestamp(..) => ElementType::Timestamp, |
| 92 |
RawBsonRef::Binary(..) => ElementType::Binary, |
| 93 |
RawBsonRef::ObjectId(..) => ElementType::ObjectId, |
| 94 |
RawBsonRef::DateTime(..) => ElementType::DateTime, |
| 95 |
RawBsonRef::Symbol(..) => ElementType::Symbol, |
| 96 |
RawBsonRef::Decimal128(..) => ElementType::Decimal128, |
| 97 |
RawBsonRef::Undefined => ElementType::Undefined, |
| 98 |
RawBsonRef::MaxKey => ElementType::MaxKey, |
| 99 |
RawBsonRef::MinKey => ElementType::MinKey, |
| 100 |
RawBsonRef::DbPointer(..) => ElementType::DbPointer, |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/// Gets the `f64` that's referenced or returns [`None`] if the referenced value isn't a BSON |
| 105 |
/// double. |
| 106 |
pub fn as_f64(self) -> Option<f64> { |
| 107 |
match self { |
| 108 |
RawBsonRef::Double(d) => Some(d), |
| 109 |
_ => None, |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/// Gets the `&str` that's referenced or returns [`None`] if the referenced value isn't a BSON |
| 114 |
/// String. |
| 115 |
pub fn as_str(self) -> Option<&'a str> { |
| 116 |
match self { |
| 117 |
RawBsonRef::String(s) => Some(s), |
| 118 |
_ => None, |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/// Gets the [`RawArray`] that's referenced or returns [`None`] if the referenced value |
| 123 |
/// isn't a BSON array. |
| 124 |
pub fn as_array(self) -> Option<&'a RawArray> { |
| 125 |
match self { |
| 126 |
RawBsonRef::Array(v) => Some(v), |
| 127 |
_ => None, |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/// Gets the [`RawDocument`] that's referenced or returns [`None`] if the referenced value |
| 132 |
/// isn't a BSON document. |
| 133 |
pub fn as_document(self) -> Option<&'a RawDocument> { |
| 134 |
match self { |
| 135 |
RawBsonRef::Document(v) => Some(v), |
| 136 |
_ => None, |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/// Gets the `bool` that's referenced or returns [`None`] if the referenced value isn't a BSON |
| 141 |
/// boolean. |
| 142 |
pub fn as_bool(self) -> Option<bool> { |
| 143 |
match self { |
| 144 |
RawBsonRef::Boolean(v) => Some(v), |
| 145 |
_ => None, |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/// Gets the `i32` that's referenced or returns [`None`] if the referenced value isn't a BSON |
| 150 |
/// Int32. |
| 151 |
pub fn as_i32(self) -> Option<i32> { |
| 152 |
match self { |
| 153 |
RawBsonRef::Int32(v) => Some(v), |
| 154 |
_ => None, |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/// Gets the `i64` that's referenced or returns [`None`] if the referenced value isn't a BSON |
| 159 |
/// Int64. |
| 160 |
pub fn as_i64(self) -> Option<i64> { |
| 161 |
match self { |
| 162 |
RawBsonRef::Int64(v) => Some(v), |
| 163 |
_ => None, |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/// Gets the [`crate::oid::ObjectId`] that's referenced or returns [`None`] if the referenced |
| 168 |
/// value isn't a BSON ObjectID. |
| 169 |
pub fn as_object_id(self) -> Option<oid::ObjectId> { |
| 170 |
match self { |
| 171 |
RawBsonRef::ObjectId(v) => Some(v), |
| 172 |
_ => None, |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/// Gets the [`RawBinaryRef`] that's referenced or returns [`None`] if the referenced value |
| 177 |
/// isn't a BSON binary. |
| 178 |
pub fn as_binary(self) -> Option<RawBinaryRef<'a>> { |
| 179 |
match self { |
| 180 |
RawBsonRef::Binary(v) => Some(v), |
| 181 |
_ => None, |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/// Gets the [`RawRegexRef`] that's referenced or returns [`None`] if the referenced value isn't |
| 186 |
/// a BSON regular expression. |
| 187 |
pub fn as_regex(self) -> Option<RawRegexRef<'a>> { |
| 188 |
match self { |
| 189 |
RawBsonRef::RegularExpression(v) => Some(v), |
| 190 |
_ => None, |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/// Gets the [`crate::DateTime`] that's referenced or returns [`None`] if the referenced value |
| 195 |
/// isn't a BSON datetime. |
| 196 |
pub fn as_datetime(self) -> Option<crate::DateTime> { |
| 197 |
match self { |
| 198 |
RawBsonRef::DateTime(v) => Some(v), |
| 199 |
_ => None, |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/// Gets the symbol that's referenced or returns [`None`] if the referenced value isn't a BSON |
| 204 |
/// symbol. |
| 205 |
pub fn as_symbol(self) -> Option<&'a str> { |
| 206 |
match self { |
| 207 |
RawBsonRef::Symbol(v) => Some(v), |
| 208 |
_ => None, |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/// Gets the [`crate::Timestamp`] that's referenced or returns [`None`] if the referenced value |
| 213 |
/// isn't a BSON timestamp. |
| 214 |
pub fn as_timestamp(self) -> Option<Timestamp> { |
| 215 |
match self { |
| 216 |
RawBsonRef::Timestamp(timestamp) => Some(timestamp), |
| 217 |
_ => None, |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/// Gets the null value that's referenced or returns [`None`] if the referenced value isn't a |
| 222 |
/// BSON null. |
| 223 |
pub fn as_null(self) -> Option<()> { |
| 224 |
match self { |
| 225 |
RawBsonRef::Null => Some(()), |
| 226 |
_ => None, |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/// Gets the [`RawDbPointerRef`] that's referenced or returns [`None`] if the referenced value |
| 231 |
/// isn't a BSON DB pointer. |
| 232 |
pub fn as_db_pointer(self) -> Option<RawDbPointerRef<'a>> { |
| 233 |
match self { |
| 234 |
RawBsonRef::DbPointer(d) => Some(d), |
| 235 |
_ => None, |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/// Gets the code that's referenced or returns [`None`] if the referenced value isn't a BSON |
| 240 |
/// JavaScript. |
| 241 |
pub fn as_javascript(self) -> Option<&'a str> { |
| 242 |
match self { |
| 243 |
RawBsonRef::JavaScriptCode(s) => Some(s), |
| 244 |
_ => None, |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/// Gets the [`RawJavaScriptCodeWithScope`] that's referenced or returns [`None`] if the |
| 249 |
/// referenced value isn't a BSON JavaScript with scope. |
| 250 |
pub fn as_javascript_with_scope(self) -> Option<RawJavaScriptCodeWithScopeRef<'a>> { |
| 251 |
match self { |
| 252 |
RawBsonRef::JavaScriptCodeWithScope(s) => Some(s), |
| 253 |
_ => None, |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/// Convert this [`RawBsonRef`] to the equivalent [`RawBson`]. |
| 258 |
pub fn to_raw_bson(self) -> RawBson { |
| 259 |
match self { |
| 260 |
RawBsonRef::Double(d) => RawBson::Double(d), |
| 261 |
RawBsonRef::String(s) => RawBson::String(s.to_string()), |
| 262 |
RawBsonRef::Array(a) => RawBson::Array(a.to_owned()), |
| 263 |
RawBsonRef::Document(d) => RawBson::Document(d.to_owned()), |
| 264 |
RawBsonRef::Boolean(b) => RawBson::Boolean(b), |
| 265 |
RawBsonRef::Null => RawBson::Null, |
| 266 |
RawBsonRef::RegularExpression(re) => { |
| 267 |
RawBson::RegularExpression(Regex::new(re.pattern, re.options)) |
| 268 |
} |
| 269 |
RawBsonRef::JavaScriptCode(c) => RawBson::JavaScriptCode(c.to_owned()), |
| 270 |
RawBsonRef::JavaScriptCodeWithScope(c_w_s) => { |
| 271 |
RawBson::JavaScriptCodeWithScope(RawJavaScriptCodeWithScope { |
| 272 |
code: c_w_s.code.to_string(), |
| 273 |
scope: c_w_s.scope.to_owned(), |
| 274 |
}) |
| 275 |
} |
| 276 |
RawBsonRef::Int32(i) => RawBson::Int32(i), |
| 277 |
RawBsonRef::Int64(i) => RawBson::Int64(i), |
| 278 |
RawBsonRef::Timestamp(t) => RawBson::Timestamp(t), |
| 279 |
RawBsonRef::Binary(b) => RawBson::Binary(Binary { |
| 280 |
bytes: b.bytes.to_vec(), |
| 281 |
subtype: b.subtype, |
| 282 |
}), |
| 283 |
RawBsonRef::ObjectId(o) => RawBson::ObjectId(o), |
| 284 |
RawBsonRef::DateTime(dt) => RawBson::DateTime(dt), |
| 285 |
RawBsonRef::Symbol(s) => RawBson::Symbol(s.to_string()), |
| 286 |
RawBsonRef::Decimal128(d) => RawBson::Decimal128(d), |
| 287 |
RawBsonRef::Undefined => RawBson::Undefined, |
| 288 |
RawBsonRef::MaxKey => RawBson::MaxKey, |
| 289 |
RawBsonRef::MinKey => RawBson::MinKey, |
| 290 |
RawBsonRef::DbPointer(d) => RawBson::DbPointer(DbPointer { |
| 291 |
namespace: d.namespace.to_string(), |
| 292 |
id: d.id, |
| 293 |
}), |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
impl<'de: 'a, 'a> Deserialize<'de> for RawBsonRef<'a> { |
| 299 |
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> |
| 300 |
where |
| 301 |
D: serde::Deserializer<'de>, |
| 302 |
{ |
| 303 |
match deserializer |
| 304 |
.deserialize_newtype_struct(RAW_BSON_NEWTYPE, OwnedOrBorrowedRawBsonVisitor)? |
| 305 |
{ |
| 306 |
OwnedOrBorrowedRawBson::Borrowed(b) => Ok(b), |
| 307 |
o => Err(serde::de::Error::custom(format!( |
| 308 |
"RawBson must be deserialized from borrowed content, instead got {:?}", |
| 309 |
o |
| 310 |
))), |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
impl<'a> Serialize for RawBsonRef<'a> { |
| 316 |
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> |
| 317 |
where |
| 318 |
S: serde::Serializer, |
| 319 |
{ |
| 320 |
match self { |
| 321 |
RawBsonRef::Double(v) => serializer.serialize_f64(*v), |
| 322 |
RawBsonRef::String(v) => serializer.serialize_str(v), |
| 323 |
RawBsonRef::Array(v) => v.serialize(serializer), |
| 324 |
RawBsonRef::Document(v) => v.serialize(serializer), |
| 325 |
RawBsonRef::Boolean(v) => serializer.serialize_bool(*v), |
| 326 |
RawBsonRef::Null => serializer.serialize_unit(), |
| 327 |
RawBsonRef::Int32(v) => serializer.serialize_i32(*v), |
| 328 |
RawBsonRef::Int64(v) => serializer.serialize_i64(*v), |
| 329 |
RawBsonRef::ObjectId(oid) => oid.serialize(serializer), |
| 330 |
RawBsonRef::DateTime(dt) => dt.serialize(serializer), |
| 331 |
RawBsonRef::Binary(b) => b.serialize(serializer), |
| 332 |
RawBsonRef::JavaScriptCode(c) => { |
| 333 |
let mut state = serializer.serialize_struct("$code", 1)?; |
| 334 |
state.serialize_field("$code", c)?; |
| 335 |
state.end() |
| 336 |
} |
| 337 |
RawBsonRef::JavaScriptCodeWithScope(code_w_scope) => code_w_scope.serialize(serializer), |
| 338 |
RawBsonRef::DbPointer(dbp) => dbp.serialize(serializer), |
| 339 |
RawBsonRef::Symbol(s) => { |
| 340 |
let mut state = serializer.serialize_struct("$symbol", 1)?; |
| 341 |
state.serialize_field("$symbol", s)?; |
| 342 |
state.end() |
| 343 |
} |
| 344 |
RawBsonRef::RegularExpression(re) => re.serialize(serializer), |
| 345 |
RawBsonRef::Timestamp(t) => t.serialize(serializer), |
| 346 |
RawBsonRef::Decimal128(d) => d.serialize(serializer), |
| 347 |
RawBsonRef::Undefined => { |
| 348 |
let mut state = serializer.serialize_struct("$undefined", 1)?; |
| 349 |
state.serialize_field("$undefined", &true)?; |
| 350 |
state.end() |
| 351 |
} |
| 352 |
RawBsonRef::MaxKey => { |
| 353 |
let mut state = serializer.serialize_struct("$maxKey", 1)?; |
| 354 |
state.serialize_field("$maxKey", &1)?; |
| 355 |
state.end() |
| 356 |
} |
| 357 |
RawBsonRef::MinKey => { |
| 358 |
let mut state = serializer.serialize_struct("$minKey", 1)?; |
| 359 |
state.serialize_field("$minKey", &1)?; |
| 360 |
state.end() |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
impl<'a> TryFrom<RawBsonRef<'a>> for Bson { |
| 367 |
type Error = Error; |
| 368 |
|
| 369 |
fn try_from(rawbson: RawBsonRef<'a>) -> Result<Bson> { |
| 370 |
rawbson.to_raw_bson().try_into() |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
impl<'a> From<i32> for RawBsonRef<'a> { |
| 375 |
fn from(i: i32) -> Self { |
| 376 |
RawBsonRef::Int32(i) |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
impl<'a> From<i64> for RawBsonRef<'a> { |
| 381 |
fn from(i: i64) -> Self { |
| 382 |
RawBsonRef::Int64(i) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
impl<'a> From<&'a str> for RawBsonRef<'a> { |
| 387 |
fn from(s: &'a str) -> Self { |
| 388 |
RawBsonRef::String(s) |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
impl<'a> From<f64> for RawBsonRef<'a> { |
| 393 |
fn from(f: f64) -> Self { |
| 394 |
RawBsonRef::Double(f) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
impl<'a> From<bool> for RawBsonRef<'a> { |
| 399 |
fn from(b: bool) -> Self { |
| 400 |
RawBsonRef::Boolean(b) |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
impl<'a> From<&'a RawDocumentBuf> for RawBsonRef<'a> { |
| 405 |
fn from(d: &'a RawDocumentBuf) -> Self { |
| 406 |
RawBsonRef::Document(d.as_ref()) |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
impl<'a> From<&'a RawDocument> for RawBsonRef<'a> { |
| 411 |
fn from(d: &'a RawDocument) -> Self { |
| 412 |
RawBsonRef::Document(d) |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
impl<'a> From<&'a RawArray> for RawBsonRef<'a> { |
| 417 |
fn from(a: &'a RawArray) -> Self { |
| 418 |
RawBsonRef::Array(a) |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
impl<'a> From<&'a RawArrayBuf> for RawBsonRef<'a> { |
| 423 |
fn from(a: &'a RawArrayBuf) -> Self { |
| 424 |
RawBsonRef::Array(a) |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
impl<'a> From<crate::DateTime> for RawBsonRef<'a> { |
| 429 |
fn from(dt: crate::DateTime) -> Self { |
| 430 |
RawBsonRef::DateTime(dt) |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
impl<'a> From<Timestamp> for RawBsonRef<'a> { |
| 435 |
fn from(ts: Timestamp) -> Self { |
| 436 |
RawBsonRef::Timestamp(ts) |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
impl<'a> From<ObjectId> for RawBsonRef<'a> { |
| 441 |
fn from(oid: ObjectId) -> Self { |
| 442 |
RawBsonRef::ObjectId(oid) |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
impl<'a> From<Decimal128> for RawBsonRef<'a> { |
| 447 |
fn from(d: Decimal128) -> Self { |
| 448 |
RawBsonRef::Decimal128(d) |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
/// A BSON binary value referencing raw bytes stored elsewhere. |
| 453 |
#[derive(Clone, Copy, Debug, PartialEq)] |
| 454 |
pub struct RawBinaryRef<'a> { |
| 455 |
/// The subtype of the binary value. |
| 456 |
pub subtype: BinarySubtype, |
| 457 |
|
| 458 |
/// The binary bytes. |
| 459 |
pub bytes: &'a [u8], |
| 460 |
} |
| 461 |
|
| 462 |
impl<'a> RawBinaryRef<'a> { |
| 463 |
/// Copy the contents into a [`Binary`]. |
| 464 |
pub fn to_binary(&self) -> Binary { |
| 465 |
Binary { |
| 466 |
subtype: self.subtype, |
| 467 |
bytes: self.bytes.to_owned(), |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
pub(crate) fn len(&self) -> i32 { |
| 472 |
match self.subtype { |
| 473 |
BinarySubtype::BinaryOld => self.bytes.len() as i32 + 4, |
| 474 |
_ => self.bytes.len() as i32, |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
impl<'de: 'a, 'a> Deserialize<'de> for RawBinaryRef<'a> { |
| 480 |
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> |
| 481 |
where |
| 482 |
D: serde::Deserializer<'de>, |
| 483 |
{ |
| 484 |
match RawBsonRef::deserialize(deserializer)? { |
| 485 |
RawBsonRef::Binary(b) => Ok(b), |
| 486 |
c => Err(serde::de::Error::custom(format!( |
| 487 |
"expected binary, but got {:?} instead", |
| 488 |
c |
| 489 |
))), |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
impl<'a> Serialize for RawBinaryRef<'a> { |
| 495 |
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> |
| 496 |
where |
| 497 |
S: serde::Serializer, |
| 498 |
{ |
| 499 |
if let BinarySubtype::Generic = self.subtype { |
| 500 |
serializer.serialize_bytes(self.bytes) |
| 501 |
} else if !serializer.is_human_readable() { |
| 502 |
#[derive(Serialize)] |
| 503 |
struct BorrowedBinary<'a> { |
| 504 |
bytes: &'a Bytes, |
| 505 |
|
| 506 |
#[serde(rename = "subType")] |
| 507 |
subtype: u8, |
| 508 |
} |
| 509 |
|
| 510 |
let mut state = serializer.serialize_struct("$binary", 1)?; |
| 511 |
let body = BorrowedBinary { |
| 512 |
bytes: Bytes::new(self.bytes), |
| 513 |
subtype: self.subtype.into(), |
| 514 |
}; |
| 515 |
state.serialize_field("$binary", &body)?; |
| 516 |
state.end() |
| 517 |
} else { |
| 518 |
let mut state = serializer.serialize_struct("$binary", 1)?; |
| 519 |
let body = extjson::models::BinaryBody { |
| 520 |
base64: base64::encode(self.bytes), |
| 521 |
subtype: hex::encode([self.subtype.into()]), |
| 522 |
}; |
| 523 |
state.serialize_field("$binary", &body)?; |
| 524 |
state.end() |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
impl<'a> From<RawBinaryRef<'a>> for RawBsonRef<'a> { |
| 530 |
fn from(b: RawBinaryRef<'a>) -> Self { |
| 531 |
RawBsonRef::Binary(b) |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
impl<'a> From<&'a Binary> for RawBsonRef<'a> { |
| 536 |
fn from(bin: &'a Binary) -> Self { |
| 537 |
bin.as_raw_binary().into() |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
/// A BSON regex referencing raw bytes stored elsewhere. |
| 542 |
#[derive(Clone, Copy, Debug, PartialEq)] |
| 543 |
pub struct RawRegexRef<'a> { |
| 544 |
/// The regex pattern to match. |
| 545 |
pub pattern: &'a str, |
| 546 |
|
| 547 |
/// The options for the regex. |
| 548 |
/// |
| 549 |
/// Options are identified by characters, which must be stored in |
| 550 |
/// alphabetical order. Valid options are 'i' for case insensitive matching, 'm' for |
| 551 |
/// multiline matching, 'x' for verbose mode, 'l' to make \w, \W, etc. locale dependent, |
| 552 |
/// 's' for dotall mode ('.' matches everything), and 'u' to make \w, \W, etc. match |
| 553 |
/// unicode. |
| 554 |
pub options: &'a str, |
| 555 |
} |
| 556 |
|
| 557 |
impl<'de: 'a, 'a> Deserialize<'de> for RawRegexRef<'a> { |
| 558 |
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> |
| 559 |
where |
| 560 |
D: serde::Deserializer<'de>, |
| 561 |
{ |
| 562 |
match RawBsonRef::deserialize(deserializer)? { |
| 563 |
RawBsonRef::RegularExpression(b) => Ok(b), |
| 564 |
c => Err(serde::de::Error::custom(format!( |
| 565 |
"expected Regex, but got {:?} instead", |
| 566 |
c |
| 567 |
))), |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
impl<'a> Serialize for RawRegexRef<'a> { |
| 573 |
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> |
| 574 |
where |
| 575 |
S: serde::Serializer, |
| 576 |
{ |
| 577 |
#[derive(Serialize)] |
| 578 |
struct BorrowedRegexBody<'a> { |
| 579 |
pattern: &'a str, |
| 580 |
options: &'a str, |
| 581 |
} |
| 582 |
|
| 583 |
let mut state = serializer.serialize_struct("$regularExpression", 1)?; |
| 584 |
let body = BorrowedRegexBody { |
| 585 |
pattern: self.pattern, |
| 586 |
options: self.options, |
| 587 |
}; |
| 588 |
state.serialize_field("$regularExpression", &body)?; |
| 589 |
state.end() |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
impl<'a> From<RawRegexRef<'a>> for RawBsonRef<'a> { |
| 594 |
fn from(re: RawRegexRef<'a>) -> Self { |
| 595 |
RawBsonRef::RegularExpression(re) |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
/// A BSON "code with scope" value referencing raw bytes stored elsewhere. |
| 600 |
#[derive(Clone, Copy, Debug, PartialEq)] |
| 601 |
pub struct RawJavaScriptCodeWithScopeRef<'a> { |
| 602 |
/// The JavaScript code. |
| 603 |
pub code: &'a str, |
| 604 |
|
| 605 |
/// The scope document containing variable bindings. |
| 606 |
pub scope: &'a RawDocument, |
| 607 |
} |
| 608 |
|
| 609 |
impl<'a> RawJavaScriptCodeWithScopeRef<'a> { |
| 610 |
pub(crate) fn len(self) -> i32 { |
| 611 |
4 + 4 + self.code.len() as i32 + 1 + self.scope.as_bytes().len() as i32 |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
impl<'de: 'a, 'a> Deserialize<'de> for RawJavaScriptCodeWithScopeRef<'a> { |
| 616 |
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> |
| 617 |
where |
| 618 |
D: serde::Deserializer<'de>, |
| 619 |
{ |
| 620 |
match RawBsonRef::deserialize(deserializer)? { |
| 621 |
RawBsonRef::JavaScriptCodeWithScope(b) => Ok(b), |
| 622 |
c => Err(serde::de::Error::custom(format!( |
| 623 |
"expected CodeWithScope, but got {:?} instead", |
| 624 |
c |
| 625 |
))), |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
impl<'a> Serialize for RawJavaScriptCodeWithScopeRef<'a> { |
| 631 |
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> |
| 632 |
where |
| 633 |
S: serde::Serializer, |
| 634 |
{ |
| 635 |
let mut state = serializer.serialize_struct("$codeWithScope", 2)?; |
| 636 |
state.serialize_field("$code", &self.code)?; |
| 637 |
state.serialize_field("$scope", &self.scope)?; |
| 638 |
state.end() |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
impl<'a> From<RawJavaScriptCodeWithScopeRef<'a>> for RawBsonRef<'a> { |
| 643 |
fn from(code_w_scope: RawJavaScriptCodeWithScopeRef<'a>) -> Self { |
| 644 |
RawBsonRef::JavaScriptCodeWithScope(code_w_scope) |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
/// A BSON DB pointer value referencing raw bytes stored elesewhere. |
| 649 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 650 |
pub struct RawDbPointerRef<'a> { |
| 651 |
pub(crate) namespace: &'a str, |
| 652 |
pub(crate) id: ObjectId, |
| 653 |
} |
| 654 |
|
| 655 |
impl<'de: 'a, 'a> Deserialize<'de> for RawDbPointerRef<'a> { |
| 656 |
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> |
| 657 |
where |
| 658 |
D: serde::Deserializer<'de>, |
| 659 |
{ |
| 660 |
match RawBsonRef::deserialize(deserializer)? { |
| 661 |
RawBsonRef::DbPointer(b) => Ok(b), |
| 662 |
c => Err(serde::de::Error::custom(format!( |
| 663 |
"expected DbPointer, but got {:?} instead", |
| 664 |
c |
| 665 |
))), |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
impl<'a> Serialize for RawDbPointerRef<'a> { |
| 671 |
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> |
| 672 |
where |
| 673 |
S: serde::Serializer, |
| 674 |
{ |
| 675 |
#[derive(Serialize)] |
| 676 |
struct BorrowedDbPointerBody<'a> { |
| 677 |
#[serde(rename = "$ref")] |
| 678 |
ref_ns: &'a str, |
| 679 |
|
| 680 |
#[serde(rename = "$id")] |
| 681 |
id: ObjectId, |
| 682 |
} |
| 683 |
|
| 684 |
let mut state = serializer.serialize_struct("$dbPointer", 1)?; |
| 685 |
let body = BorrowedDbPointerBody { |
| 686 |
ref_ns: self.namespace, |
| 687 |
id: self.id, |
| 688 |
}; |
| 689 |
state.serialize_field("$dbPointer", &body)?; |
| 690 |
state.end() |
| 691 |
} |
| 692 |
} |