1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
// The MIT License (MIT)
// Copyright (c) 2015 Y. T. Chung <zonyitoo@gmail.com>
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//! BSON definition
use std::{
convert::{TryFrom, TryInto},
fmt::{self, Debug, Display, Formatter},
};
use serde_json::{json, Value};
pub use crate::document::Document;
use crate::{
oid::{self, ObjectId},
spec::{BinarySubtype, ElementType},
Binary,
Decimal128,
};
/// Possible BSON value types.
#[derive(Clone, PartialEq)]
pub enum Bson {
/// 64-bit binary floating point
Double(f64),
/// UTF-8 string
String(String),
/// Array
Array(Array),
/// Embedded document
Document(Document),
/// Boolean value
Boolean(bool),
/// Null value
Null,
/// Regular expression
RegularExpression(Regex),
/// JavaScript code
JavaScriptCode(String),
/// JavaScript code w/ scope
JavaScriptCodeWithScope(JavaScriptCodeWithScope),
/// 32-bit signed integer
Int32(i32),
/// 64-bit signed integer
Int64(i64),
/// Timestamp
Timestamp(Timestamp),
/// Binary data
Binary(Binary),
/// [ObjectId](http://dochub.mongodb.org/core/objectids)
ObjectId(oid::ObjectId),
/// UTC datetime
DateTime(crate::DateTime),
/// Symbol (Deprecated)
Symbol(String),
/// [128-bit decimal floating point](https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst)
Decimal128(Decimal128),
/// Undefined value (Deprecated)
Undefined,
/// Max key
MaxKey,
/// Min key
MinKey,
/// DBPointer (Deprecated)
DbPointer(DbPointer),
}
/// Alias for `Vec<Bson>`.
pub type Array = Vec<Bson>;
impl Default for Bson {
fn default() -> Self {
Bson::Null
}
}
impl Display for Bson {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Bson::Double(f) => write!(fmt, "{}", f),
Bson::String(ref s) => write!(fmt, "\"{}\"", s),
Bson::Array(ref vec) => {
fmt.write_str("[")?;
let mut first = true;
for bson in vec {
if !first {
fmt.write_str(", ")?;
}
write!(fmt, "{}", bson)?;
first = false;
}
fmt.write_str("]")
}
Bson::Document(ref doc) => write!(fmt, "{}", doc),
Bson::Boolean(b) => write!(fmt, "{}", b),
Bson::Null => write!(fmt, "null"),
Bson::RegularExpression(ref x) => write!(fmt, "{}", x),
Bson::JavaScriptCode(ref code)
| Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope { ref code, .. }) => {
fmt.write_str(code)
}
Bson::Int32(i) => write!(fmt, "{}", i),
Bson::Int64(i) => write!(fmt, "{}", i),
Bson::Timestamp(ref x) => write!(fmt, "{}", x),
Bson::Binary(ref x) => write!(fmt, "{}", x),
Bson::ObjectId(ref id) => write!(fmt, "ObjectId(\"{}\")", id),
Bson::DateTime(date_time) => write!(fmt, "DateTime(\"{}\")", date_time),
Bson::Symbol(ref sym) => write!(fmt, "Symbol(\"{}\")", sym),
Bson::Decimal128(ref d) => write!(fmt, "{}", d),
Bson::Undefined => write!(fmt, "undefined"),
Bson::MinKey => write!(fmt, "MinKey"),
Bson::MaxKey => write!(fmt, "MaxKey"),
Bson::DbPointer(DbPointer {
ref namespace,
ref id,
}) => write!(fmt, "DbPointer({}, {})", namespace, id),
}
}
}
impl Debug for Bson {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
match *self {
Bson::Double(f) => fmt.debug_tuple("Double").field(&f).finish(),
Bson::String(ref s) => fmt.debug_tuple("String").field(s).finish(),
Bson::Array(ref vec) => {
write!(fmt, "Array(")?;
Debug::fmt(vec, fmt)?;
write!(fmt, ")")
}
Bson::Document(ref doc) => Debug::fmt(doc, fmt),
Bson::Boolean(b) => fmt.debug_tuple("Boolean").field(&b).finish(),
Bson::Null => write!(fmt, "Null"),
Bson::RegularExpression(ref regex) => Debug::fmt(regex, fmt),
Bson::JavaScriptCode(ref code) => {
fmt.debug_tuple("JavaScriptCode").field(code).finish()
}
Bson::JavaScriptCodeWithScope(ref code) => Debug::fmt(code, fmt),
Bson::Int32(i) => fmt.debug_tuple("Int32").field(&i).finish(),
Bson::Int64(i) => fmt.debug_tuple("Int64").field(&i).finish(),
Bson::Timestamp(ref t) => Debug::fmt(t, fmt),
Bson::Binary(ref b) => Debug::fmt(b, fmt),
Bson::ObjectId(ref id) => Debug::fmt(id, fmt),
Bson::DateTime(ref date_time) => Debug::fmt(date_time, fmt),
Bson::Symbol(ref sym) => fmt.debug_tuple("Symbol").field(sym).finish(),
Bson::Decimal128(ref d) => Debug::fmt(d, fmt),
Bson::Undefined => write!(fmt, "Undefined"),
Bson::MinKey => write!(fmt, "MinKey"),
Bson::MaxKey => write!(fmt, "MaxKey"),
Bson::DbPointer(ref pointer) => Debug::fmt(pointer, fmt),
}
}
}
impl From<f32> for Bson {
fn from(a: f32) -> Bson {
Bson::Double(a.into())
}
}
impl From<f64> for Bson {
fn from(a: f64) -> Bson {
Bson::Double(a)
}
}
impl From<&str> for Bson {
fn from(s: &str) -> Bson {
Bson::String(s.to_owned())
}
}
impl From<String> for Bson {
fn from(a: String) -> Bson {
Bson::String(a)
}
}
impl From<Document> for Bson {
fn from(a: Document) -> Bson {
Bson::Document(a)
}
}
impl From<bool> for Bson {
fn from(a: bool) -> Bson {
Bson::Boolean(a)
}
}
impl From<Regex> for Bson {
fn from(regex: Regex) -> Bson {
Bson::RegularExpression(regex)
}
}
impl From<JavaScriptCodeWithScope> for Bson {
fn from(code_with_scope: JavaScriptCodeWithScope) -> Bson {
Bson::JavaScriptCodeWithScope(code_with_scope)
}
}
impl From<Binary> for Bson {
fn from(binary: Binary) -> Bson {
Bson::Binary(binary)
}
}
impl From<Timestamp> for Bson {
fn from(ts: Timestamp) -> Bson {
Bson::Timestamp(ts)
}
}
impl<T> From<&T> for Bson
where
T: Clone + Into<Bson>,
{
fn from(t: &T) -> Bson {
t.clone().into()
}
}
impl<T> From<Vec<T>> for Bson
where
T: Into<Bson>,
{
fn from(v: Vec<T>) -> Bson {
Bson::Array(v.into_iter().map(|val| val.into()).collect())
}
}
impl<T> From<&[T]> for Bson
where
T: Clone + Into<Bson>,
{
fn from(s: &[T]) -> Bson {
Bson::Array(s.iter().cloned().map(|val| val.into()).collect())
}
}
impl<T: Into<Bson>> ::std::iter::FromIterator<T> for Bson {
/// # Examples
///
/// ```
/// use std::iter::FromIterator;
/// use bson::Bson;
///
/// let x: Bson = Bson::from_iter(vec!["lorem", "ipsum", "dolor"]);
/// // or
/// let x: Bson = vec!["lorem", "ipsum", "dolor"].into_iter().collect();
/// ```
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Bson::Array(iter.into_iter().map(Into::into).collect())
}
}
impl From<i32> for Bson {
fn from(a: i32) -> Bson {
Bson::Int32(a)
}
}
impl From<i64> for Bson {
fn from(a: i64) -> Bson {
Bson::Int64(a)
}
}
impl From<u32> for Bson {
fn from(a: u32) -> Bson {
if let Ok(i) = i32::try_from(a) {
Bson::Int32(i)
} else {
Bson::Int64(a.into())
}
}
}
impl From<[u8; 12]> for Bson {
fn from(a: [u8; 12]) -> Bson {
Bson::ObjectId(oid::ObjectId::from_bytes(a))
}
}
impl From<oid::ObjectId> for Bson {
fn from(a: oid::ObjectId) -> Bson {
Bson::ObjectId(a)
}
}
#[cfg(feature = "time-0_3")]
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))]
impl From<time::OffsetDateTime> for Bson {
fn from(a: time::OffsetDateTime) -> Bson {
Bson::DateTime(crate::DateTime::from(a))
}
}
#[cfg(feature = "chrono-0_4")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for Bson {
fn from(a: chrono::DateTime<T>) -> Bson {
Bson::DateTime(crate::DateTime::from(a))
}
}
#[cfg(feature = "uuid-0_8")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
impl From<uuid_0_8::Uuid> for Bson {
fn from(uuid: uuid_0_8::Uuid) -> Self {
Bson::Binary(uuid.into())
}
}
#[cfg(feature = "uuid-1")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
impl From<uuid::Uuid> for Bson {
fn from(uuid: uuid::Uuid) -> Self {
Bson::Binary(uuid.into())
}
}
impl From<crate::DateTime> for Bson {
fn from(dt: crate::DateTime) -> Self {
Bson::DateTime(dt)
}
}
impl From<DbPointer> for Bson {
fn from(a: DbPointer) -> Bson {
Bson::DbPointer(a)
}
}
impl From<Decimal128> for Bson {
fn from(d: Decimal128) -> Self {
Bson::Decimal128(d)
}
}
impl<T> From<Option<T>> for Bson
where
T: Into<Bson>,
{
fn from(a: Option<T>) -> Bson {
match a {
None => Bson::Null,
Some(t) => t.into(),
}
}
}
/// This will create the [relaxed Extended JSON v2](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/) representation of the provided [`Bson`](../enum.Bson.html).
impl From<Bson> for Value {
fn from(bson: Bson) -> Self {
bson.into_relaxed_extjson()
}
}
impl Bson {
/// Converts the Bson value into its [relaxed extended JSON representation](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/).
///
/// Note: If this method is called on a case which contains a `Decimal128` value, it will panic.
pub fn into_relaxed_extjson(self) -> Value {
match self {
Bson::Double(v) if v.is_nan() => {
let s = if v.is_sign_negative() { "-NaN" } else { "NaN" };
json!({ "$numberDouble": s })
}
Bson::Double(v) if v.is_infinite() => {
let s = if v.is_sign_negative() {
"-Infinity"
} else {
"Infinity"
};
json!({ "$numberDouble": s })
}
Bson::Double(v) => json!(v),
Bson::String(v) => json!(v),
Bson::Array(v) => Value::Array(v.into_iter().map(Bson::into_relaxed_extjson).collect()),
Bson::Document(v) => Value::Object(
v.into_iter()
.map(|(k, v)| (k, v.into_relaxed_extjson()))
.collect(),
),
Bson::Boolean(v) => json!(v),
Bson::Null => Value::Null,
Bson::RegularExpression(Regex { pattern, options }) => {
let mut chars: Vec<_> = options.chars().collect();
chars.sort_unstable();
let options: String = chars.into_iter().collect();
json!({
"$regularExpression": {
"pattern": pattern,
"options": options,
}
})
}
Bson::JavaScriptCode(code) => json!({ "$code": code }),
Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope { code, scope }) => json!({
"$code": code,
"$scope": scope,
}),
Bson::Int32(v) => v.into(),
Bson::Int64(v) => v.into(),
Bson::Timestamp(Timestamp { time, increment }) => json!({
"$timestamp": {
"t": time,
"i": increment,
}
}),
Bson::Binary(Binary { subtype, ref bytes }) => {
let tval: u8 = From::from(subtype);
json!({
"$binary": {
"base64": base64::encode(bytes),
"subType": hex::encode([tval]),
}
})
}
Bson::ObjectId(v) => json!({"$oid": v.to_hex()}),
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_time_0_3().year() <= 9999 => {
json!({
// Unwrap safety: timestamps in the guarded range can always be formatted.
"$date": v.try_to_rfc3339_string().unwrap(),
})
}
Bson::DateTime(v) => json!({
"$date": { "$numberLong": v.timestamp_millis().to_string() },
}),
Bson::Symbol(v) => json!({ "$symbol": v }),
Bson::Decimal128(v) => json!({ "$numberDecimal": v.to_string() }),
Bson::Undefined => json!({ "$undefined": true }),
Bson::MinKey => json!({ "$minKey": 1 }),
Bson::MaxKey => json!({ "$maxKey": 1 }),
Bson::DbPointer(DbPointer {
ref namespace,
ref id,
}) => json!({
"$dbPointer": {
"$ref": namespace,
"$id": {
"$oid": id.to_hex()
}
}
}),
}
}
/// Converts the Bson value into its [canonical extended JSON representation](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/).
pub fn into_canonical_extjson(self) -> Value {
match self {
Bson::Int32(i) => json!({ "$numberInt": i.to_string() }),
Bson::Int64(i) => json!({ "$numberLong": i.to_string() }),
Bson::Double(f) if f.is_normal() => {
let mut s = f.to_string();
if f.fract() == 0.0 {
s.push_str(".0");
}
json!({ "$numberDouble": s })
}
Bson::Double(f) if f == 0.0 => {
let s = if f.is_sign_negative() { "-0.0" } else { "0.0" };
json!({ "$numberDouble": s })
}
Bson::DateTime(date) => {
json!({ "$date": { "$numberLong": date.timestamp_millis().to_string() } })
}
Bson::Array(arr) => {
Value::Array(arr.into_iter().map(Bson::into_canonical_extjson).collect())
}
Bson::Document(arr) => Value::Object(
arr.into_iter()
.map(|(k, v)| (k, v.into_canonical_extjson()))
.collect(),
),
Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope { code, scope }) => json!({
"$code": code,
"$scope": Bson::Document(scope).into_canonical_extjson(),
}),
other => other.into_relaxed_extjson(),
}
}
/// Get the [`ElementType`] of this value.
pub fn element_type(&self) -> ElementType {
match *self {
Bson::Double(..) => ElementType::Double,
Bson::String(..) => ElementType::String,
Bson::Array(..) => ElementType::Array,
Bson::Document(..) => ElementType::EmbeddedDocument,
Bson::Boolean(..) => ElementType::Boolean,
Bson::Null => ElementType::Null,
Bson::RegularExpression(..) => ElementType::RegularExpression,
Bson::JavaScriptCode(..) => ElementType::JavaScriptCode,
Bson::JavaScriptCodeWithScope(..) => ElementType::JavaScriptCodeWithScope,
Bson::Int32(..) => ElementType::Int32,
Bson::Int64(..) => ElementType::Int64,
Bson::Timestamp(..) => ElementType::Timestamp,
Bson::Binary(..) => ElementType::Binary,
Bson::ObjectId(..) => ElementType::ObjectId,
Bson::DateTime(..) => ElementType::DateTime,
Bson::Symbol(..) => ElementType::Symbol,
Bson::Decimal128(..) => ElementType::Decimal128,
Bson::Undefined => ElementType::Undefined,
Bson::MaxKey => ElementType::MaxKey,
Bson::MinKey => ElementType::MinKey,
Bson::DbPointer(..) => ElementType::DbPointer,
}
}
/// Converts to extended format.
/// This function mainly used for [extended JSON format](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/).
// TODO RUST-426: Investigate either removing this from the serde implementation or unifying
// with the extended JSON implementation.
pub(crate) fn into_extended_document(self, rawbson: bool) -> Document {
match self {
Bson::RegularExpression(Regex {
ref pattern,
ref options,
}) => {
let mut chars: Vec<_> = options.chars().collect();
chars.sort_unstable();
let options: String = chars.into_iter().collect();
doc! {
"$regularExpression": {
"pattern": pattern,
"options": options,
}
}
}
Bson::JavaScriptCode(ref code) => {
doc! {
"$code": code,
}
}
Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope { code, scope }) => {
doc! {
"$code": code,
"$scope": scope,
}
}
Bson::Timestamp(Timestamp { time, increment }) => {
doc! {
"$timestamp": {
"t": time,
"i": increment,
}
}
}
Bson::Binary(Binary { subtype, bytes }) => {
let tval: u8 = From::from(subtype);
if rawbson {
doc! {
"$binary": {
"bytes": Binary { subtype: BinarySubtype::Generic, bytes },
"subType": Bson::Int32(tval.into())
}
}
} else {
doc! {
"$binary": {
"base64": base64::encode(bytes),
"subType": hex::encode([tval]),
}
}
}
}
Bson::ObjectId(ref v) => {
doc! {
"$oid": v.to_string(),
}
}
Bson::DateTime(v) if rawbson => doc! {
"$date": v.timestamp_millis(),
},
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_time_0_3().year() <= 9999 => {
doc! {
// Unwrap safety: timestamps in the guarded range can always be formatted.
"$date": v.try_to_rfc3339_string().unwrap(),
}
}
Bson::DateTime(v) => doc! {
"$date": { "$numberLong": v.timestamp_millis().to_string() },
},
Bson::Symbol(ref v) => {
doc! {
"$symbol": v.to_owned(),
}
}
Bson::Undefined => {
doc! {
"$undefined": true,
}
}
Bson::MinKey => {
doc! {
"$minKey": 1,
}
}
Bson::MaxKey => {
doc! {
"$maxKey": 1,
}
}
Bson::DbPointer(DbPointer {
ref namespace,
ref id,
}) => {
doc! {
"$dbPointer": {
"$ref": namespace,
"$id": {
"$oid": id.to_string()
}
}
}
}
_ => panic!("Attempted conversion of invalid data type: {}", self),
}
}
pub(crate) fn from_extended_document(doc: Document) -> Bson {
if doc.len() > 2 {
return Bson::Document(doc);
}
let mut keys: Vec<_> = doc.keys().map(|s| s.as_str()).collect();
keys.sort_unstable();
match keys.as_slice() {
["$oid"] => {
if let Ok(oid) = doc.get_str("$oid") {
if let Ok(oid) = ObjectId::parse_str(oid) {
return Bson::ObjectId(oid);
}
}
}
["$symbol"] => {
if let Ok(symbol) = doc.get_str("$symbol") {
return Bson::Symbol(symbol.into());
}
}
["$numberInt"] => {
if let Ok(i) = doc.get_str("$numberInt") {
if let Ok(i) = i.parse() {
return Bson::Int32(i);
}
}
}
["$numberLong"] => {
if let Ok(i) = doc.get_str("$numberLong") {
if let Ok(i) = i.parse() {
return Bson::Int64(i);
}
}
}
["$numberDouble"] => match doc.get_str("$numberDouble") {
Ok("Infinity") => return Bson::Double(std::f64::INFINITY),
Ok("-Infinity") => return Bson::Double(std::f64::NEG_INFINITY),
Ok("NaN") => return Bson::Double(std::f64::NAN),
Ok(other) => {
if let Ok(d) = other.parse() {
return Bson::Double(d);
}
}
_ => {}
},
["$numberDecimal"] => {
if let Ok(d) = doc.get_str("$numberDecimal") {
if let Ok(d) = d.parse() {
return Bson::Decimal128(d);
}
}
}
["$numberDecimalBytes"] => {
if let Ok(bytes) = doc.get_binary_generic("$numberDecimalBytes") {
if let Ok(b) = bytes.clone().try_into() {
return Bson::Decimal128(Decimal128 { bytes: b });
}
}
}
["$binary"] => {
if let Some(binary) = Binary::from_extended_doc(&doc) {
return Bson::Binary(binary);
}
}
["$code"] => {
if let Ok(code) = doc.get_str("$code") {
return Bson::JavaScriptCode(code.into());
}
}
["$code", "$scope"] => {
if let Ok(code) = doc.get_str("$code") {
if let Ok(scope) = doc.get_document("$scope") {
return Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope {
code: code.into(),
scope: scope.clone(),
});
}
}
}
["$timestamp"] => {
if let Ok(timestamp) = doc.get_document("$timestamp") {
if let Ok(t) = timestamp.get_i32("t") {
if let Ok(i) = timestamp.get_i32("i") {
return Bson::Timestamp(Timestamp {
time: t as u32,
increment: i as u32,
});
}
}
if let Ok(t) = timestamp.get_i64("t") {
if let Ok(i) = timestamp.get_i64("i") {
if t >= 0
&& i >= 0
&& t <= (std::u32::MAX as i64)
&& i <= (std::u32::MAX as i64)
{
return Bson::Timestamp(Timestamp {
time: t as u32,
increment: i as u32,
});
}
}
}
}
}
["$regularExpression"] => {
if let Ok(regex) = doc.get_document("$regularExpression") {
if let Ok(pattern) = regex.get_str("pattern") {
if let Ok(options) = regex.get_str("options") {
return Bson::RegularExpression(Regex::new(pattern, options));
}
}
}
}
["$dbPointer"] => {
if let Ok(db_pointer) = doc.get_document("$dbPointer") {
if let Ok(ns) = db_pointer.get_str("$ref") {
if let Ok(id) = db_pointer.get_object_id("$id") {
return Bson::DbPointer(DbPointer {
namespace: ns.into(),
id,
});
}
}
}
}
["$date"] => {
if let Ok(date) = doc.get_i64("$date") {
return Bson::DateTime(crate::DateTime::from_millis(date));
}
if let Ok(date) = doc.get_str("$date") {
if let Ok(dt) = crate::DateTime::parse_rfc3339_str(date) {
return Bson::DateTime(dt);
}
}
}
["$minKey"] => {
let min_key = doc.get("$minKey");
if min_key == Some(&Bson::Int32(1)) || min_key == Some(&Bson::Int64(1)) {
return Bson::MinKey;
}
}
["$maxKey"] => {
let max_key = doc.get("$maxKey");
if max_key == Some(&Bson::Int32(1)) || max_key == Some(&Bson::Int64(1)) {
return Bson::MaxKey;
}
}
["$undefined"] => {
if doc.get("$undefined") == Some(&Bson::Boolean(true)) {
return Bson::Undefined;
}
}
_ => {}
};
Bson::Document(
doc.into_iter()
.map(|(k, v)| {
let v = match v {
Bson::Document(v) => Bson::from_extended_document(v),
other => other,
};
(k, v)
})
.collect(),
)
}
}
/// Value helpers
impl Bson {
/// If `self` is [`Double`](Bson::Double), return its value as an `f64`. Returns [`None`]
/// otherwise.
pub fn as_f64(&self) -> Option<f64> {
match *self {
Bson::Double(v) => Some(v),
_ => None,
}
}
/// If `self` is [`String`](Bson::String), return its value as a `&str`. Returns [`None`]
/// otherwise.
pub fn as_str(&self) -> Option<&str> {
match *self {
Bson::String(ref s) => Some(s),
_ => None,
}
}
/// If `self` is [`String`](Bson::String), return a mutable reference to its value as a [`str`].
/// Returns [`None`] otherwise.
pub fn as_str_mut(&mut self) -> Option<&mut str> {
match *self {
Bson::String(ref mut s) => Some(s),
_ => None,
}
}
/// If `self` is [`Array`](Bson::Array), return its value. Returns [`None`] otherwise.
pub fn as_array(&self) -> Option<&Array> {
match *self {
Bson::Array(ref v) => Some(v),
_ => None,
}
}
/// If `self` is [`Array`](Bson::Array), return a mutable reference to its value. Returns
/// [`None`] otherwise.
pub fn as_array_mut(&mut self) -> Option<&mut Array> {
match *self {
Bson::Array(ref mut v) => Some(v),
_ => None,
}
}
/// If `self` is [`Document`](Bson::Document), return its value. Returns [`None`] otherwise.
pub fn as_document(&self) -> Option<&Document> {
match *self {
Bson::Document(ref v) => Some(v),
_ => None,
}
}
/// If `self` is [`Document`](Bson::Document), return a mutable reference to its value. Returns
/// [`None`] otherwise.
pub fn as_document_mut(&mut self) -> Option<&mut Document> {
match *self {
Bson::Document(ref mut v) => Some(v),
_ => None,
}
}
/// If `self` is [`Boolean`](Bson::Boolean), return its value. Returns [`None`] otherwise.
pub fn as_bool(&self) -> Option<bool> {
match *self {
Bson::Boolean(v) => Some(v),
_ => None,
}
}
/// If `self` is [`Int32`](Bson::Int32), return its value. Returns [`None`] otherwise.
pub fn as_i32(&self) -> Option<i32> {
match *self {
Bson::Int32(v) => Some(v),
_ => None,
}
}
/// If `self` is [`Int64`](Bson::Int64), return its value. Returns [`None`] otherwise.
pub fn as_i64(&self) -> Option<i64> {
match *self {
Bson::Int64(v) => Some(v),
_ => None,
}
}
/// If `self` is [`ObjectId`](Bson::ObjectId), return its value. Returns [`None`] otherwise.
pub fn as_object_id(&self) -> Option<oid::ObjectId> {
match *self {
Bson::ObjectId(v) => Some(v),
_ => None,
}
}
/// If `self` is [`ObjectId`](Bson::ObjectId), return a mutable reference to its value. Returns
/// [`None`] otherwise.
pub fn as_object_id_mut(&mut self) -> Option<&mut oid::ObjectId> {
match *self {
Bson::ObjectId(ref mut v) => Some(v),
_ => None,
}
}
/// If `self` is [`DateTime`](Bson::DateTime), return its value. Returns [`None`] otherwise.
pub fn as_datetime(&self) -> Option<&crate::DateTime> {
match *self {
Bson::DateTime(ref v) => Some(v),
_ => None,
}
}
/// If `self` is [`DateTime`](Bson::DateTime), return a mutable reference to its value. Returns
/// [`None`] otherwise.
pub fn as_datetime_mut(&mut self) -> Option<&mut crate::DateTime> {
match *self {
Bson::DateTime(ref mut v) => Some(v),
_ => None,
}
}
/// If `self` is [`Symbol`](Bson::Symbol), return its value. Returns [`None`] otherwise.
pub fn as_symbol(&self) -> Option<&str> {
match *self {
Bson::Symbol(ref v) => Some(v),
_ => None,
}
}
/// If `self` is [`Symbol`](Bson::Symbol), return a mutable reference to its value. Returns
/// [`None`] otherwise.
pub fn as_symbol_mut(&mut self) -> Option<&mut str> {
match *self {
Bson::Symbol(ref mut v) => Some(v),
_ => None,
}
}
/// If `self` is [`Timestamp`](Bson::Timestamp), return its value. Returns [`None`] otherwise.
pub fn as_timestamp(&self) -> Option<Timestamp> {
match *self {
Bson::Timestamp(timestamp) => Some(timestamp),
_ => None,
}
}
/// If `self` is [`Null`](Bson::Null), return `()`. Returns [`None`] otherwise.
pub fn as_null(&self) -> Option<()> {
match *self {
Bson::Null => Some(()),
_ => None,
}
}
/// If `self` is [`DbPointer`](Bson::DbPointer), return its value. Returns [`None`] otherwise.
pub fn as_db_pointer(&self) -> Option<&DbPointer> {
match self {
Bson::DbPointer(ref db_pointer) => Some(db_pointer),
_ => None,
}
}
}
/// Represents a BSON timestamp value.
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd, Clone, Copy, Hash)]
pub struct Timestamp {
/// The number of seconds since the Unix epoch.
pub time: u32,
/// An incrementing value to order timestamps with the same number of seconds in the `time`
/// field.
pub increment: u32,
}
impl Display for Timestamp {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "Timestamp({}, {})", self.time, self.increment)
}
}
impl Timestamp {
pub(crate) fn to_le_i64(self) -> i64 {
let upper = (self.time.to_le() as u64) << 32;
let lower = self.increment.to_le() as u64;
(upper | lower) as i64
}
pub(crate) fn from_le_i64(val: i64) -> Self {
let ts = val.to_le();
Timestamp {
time: ((ts as u64) >> 32) as u32,
increment: (ts & 0xFFFF_FFFF) as u32,
}
}
}
/// Represents a BSON regular expression value.
#[derive(Debug, Clone, PartialEq)]
pub struct Regex {
/// The regex pattern to match.
pub pattern: String,
/// The options for the regex.
///
/// Options are identified by characters, which must be stored in
/// alphabetical order. Valid options are 'i' for case insensitive matching, 'm' for
/// multiline matching, 'x' for verbose mode, 'l' to make \w, \W, etc. locale dependent,
/// 's' for dotall mode ('.' matches everything), and 'u' to make \w, \W, etc. match
/// unicode.
pub options: String,
}
impl Regex {
pub(crate) fn new(pattern: impl AsRef<str>, options: impl AsRef<str>) -> Self {
let mut chars: Vec<_> = options.as_ref().chars().collect();
chars.sort_unstable();
let options: String = chars.into_iter().collect();
Self {
pattern: pattern.as_ref().to_string(),
options,
}
}
}
impl Display for Regex {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "/{}/{}", self.pattern, self.options)
}
}
/// Represents a BSON code with scope value.
#[derive(Debug, Clone, PartialEq)]
pub struct JavaScriptCodeWithScope {
/// The JavaScript code.
pub code: String,
/// The scope document containing variable bindings.
pub scope: Document,
}
impl Display for JavaScriptCodeWithScope {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(&self.code)
}
}
/// Represents a DBPointer. (Deprecated)
#[derive(Debug, Clone, PartialEq)]
pub struct DbPointer {
pub(crate) namespace: String,
pub(crate) id: oid::ObjectId,
}