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
//! This crate provides the macro [`mongo_db`] to model a mongoDB database.
//!
//! # Features
//!
//! Feature flags are documented here.
//!
//! ## `default`
//!
//! This feature enables the following feature(s):
//!
//! - `mongodb-gridfs`
//!
//! ## `mongodb-gridfs`
//!
//! Enabling this feature creates automatic implementations of the then-available trait `GridFSDb`.
/// To make [`mongo_db`] work reliably a couple of re-exports are needed, these are not relevant for using the macro.
#[doc(hidden)]
pub use {async_trait, mongodb, mongodb_ext_derive, paste, serde, typed_builder};
#[doc(hidden)]
pub mod traits;
#[doc(hidden)]
pub use crate::mongodb_ext_derive::case;
#[cfg(feature = "mongodb-gridfs")]
pub use crate::traits::GridFSDb;
pub use crate::traits::{MongoClient, MongoCollection};
/// Defines the default type inside an [`Option`] for the `_id` field.
///
/// Re-export from [`mongodb::bson::oid::ObjectId`].
///
pub use mongodb::bson::oid::ObjectId as DefaultId;
/// Defines the default value used as schema version in [`MongoCollection::SCHEMA_VERSION`] if not specified otherwise.
pub const DEFAULT_SCHEMA_VERSION: i32 = 1;
/// This macro parses the per-collection parameters in a more usable format.
#[macro_export]
#[doc(hidden)]
macro_rules! parse_collection_params {
(
version: $version:literal,
_id: $id:ident
$($rest:tt)*
) => {
$crate::expand_collection_version! {
version = $version;
id = $id;
$($rest)*
}
};
(
_id: $id:ident,
version: $version:literal
$($rest:tt)*
) => {
$crate::expand_collection_version! {
version = $version;
id = $id;
$($rest)*
}
};
(
version: $version:literal
$($rest:tt)*
) => {
$crate::expand_collection_version! {
version = $version;
id = ;
$($rest)*
}
};
(
_id: $id:ident
$($rest:tt)*
) => {
$crate::expand_collection_version! {
version = ;
id = $id;
$($rest)*
}
};
(
$($rest:tt)*
) => {
$crate::expand_collection_version! {
version = ;
id = ;
$($rest)*
}
};
}
/// Expands schema version that is given in `<` / `>` behind each collection.
#[macro_export]
#[doc(hidden)]
macro_rules! expand_collection_version {
(
version = ;
$($rest:tt)*
) => {
$crate::expand_collection_id!{
version = $crate::DEFAULT_SCHEMA_VERSION;
$($rest)*
}
};
(
version = $version:literal;
$($rest:tt)*
) => {
$crate::expand_collection_id!{
version = $version;
$($rest)*
}
};
}
/// Expands collection _id that is given in `<` / `>` behind each collection.
#[macro_export]
#[doc(hidden)]
macro_rules! expand_collection_id {
(
version = $version:expr;
id = ;
$($rest:tt)*
) => {
$crate::expand_collection!{
@add_id
version = $version;
id = $crate::DefaultId;
$($rest)*
}
};
(
version = $version:expr;
id = none;
$($rest:tt)*
) => {
$crate::expand_collection!{
@final
version = $version;
id = none;
$($rest)*
}
};
(
version = $version:expr;
id = $id:ty;
$($rest:tt)*
) => {
$crate::expand_collection!{
@add_id
version = $version;
id = $id;
$($rest)*
}
};
}
/// Expands one collection.
///
/// Needed internally, but has no big use on its own.
/// Thus hidden from documentation.
#[macro_export]
#[doc(hidden)]
macro_rules! expand_collection {
// invoked with `_id: none`, thus assume `_id` is added already and finally expand to collection
(
@final
version = $schema_version:expr;
id = none;
$(#[$additional_coll_attr:meta])*
$coll_name:ident {
$(
$(#[$additional_field_attr:meta])*
$field:ident: $field_type:ty
),*$(,)?
}
$(-{
$($inner_tokens2:tt)+
})?
) => {
$crate::paste::paste! {
#[doc = "Represents the [`" $coll_name "`] collection in mongodb."]
#[derive($crate::serde::Deserialize, $crate::serde::Serialize, $crate::typed_builder::TypedBuilder)]
#[serde(rename_all = "camelCase")]
$(#[$additional_coll_attr])*
pub struct $coll_name {
$(
$(#[$additional_field_attr])*
pub $field: $field_type
),*
}
impl $crate::MongoCollection for $coll_name {
const NAME: &'static str = $crate::case!($coll_name => Camel);
const SCHEMA_VERSION: i32 = $schema_version;
}
$(
impl $coll_name {
$($inner_tokens2)+
}
)?
}
};
// specific type for `_id` given, add it and invoke again with `_id: none` to avoid adding the `_id` field again
(
@add_id
version = $schema_version:expr;
id = $explicit_id_type:ty;
$(#[$additional_coll_attr:meta])*
$coll_name:ident {
$(
$(#[$additional_field_attr:meta])*
$field:ident: $field_type:ty
),*$(,)?
}
$(-{
$($inner_tokens2:tt)+
})?
) => {
$crate::expand_collection! {
@final
version = $schema_version;
id = none;
$(#[$additional_coll_attr])*
$coll_name {
#[serde(skip_serializing_if = "std::option::Option::is_none")]
#[serde(rename = "_id")]
#[builder(default)]
_id: std::option::Option<$explicit_id_type>,
$(
$(#[$additional_field_attr])*
$field: $field_type
),*
}-{
#[doc = "Returns a reference to the `_id` field."]
#[allow(dead_code)]
pub fn id(&self) -> &Option<$explicit_id_type> {
&self._id
}
$($($inner_tokens2)+)?
}
}
};
}
/// Expands the main database client.
///
/// Needed internally, but has no big use on its own.
/// Thus hidden from documentation.
#[macro_export]
#[doc(hidden)]
macro_rules! expand_main_client {
(
$(#[$additional_db_attr:meta])*
$db_name:ident {
$(
$(#[$additional_coll_attr:meta])*
$coll_name:ident<_id: none> {
$(
$(#[$additional_field_attr:meta])*
$field:ident: $field_type:ty
),*$(,)?
}
),+
}
$(-{
$($impl:tt)+
})?
) => {
$crate::paste::paste! {
#[doc = "Client to interact with the `" $db_name "` database."]
$(#[$additional_db_attr])*
pub struct $db_name {
pub client: $crate::mongodb::Client,
pub database: $crate::mongodb::Database,
$(
#[doc = "Handle to the `" $coll_name "` collection"]
pub [<$coll_name:snake:lower _coll>]: $crate::mongodb::Collection<schema::$coll_name>
),+
}
#[$crate::async_trait::async_trait]
impl $crate::MongoClient for $db_name {
const NAME: &'static str = $crate::case!($db_name => Camel);
async fn new(connection_str: &str) -> $crate::mongodb::error::Result<Self> {
let client = match $crate::mongodb::Client::with_uri_str(connection_str).await {
$crate::mongodb::error::Result::Ok(client) => client,
$crate::mongodb::error::Result::Err(e) => return $crate::mongodb::error::Result::Err(e),
};
let database = client.database(Self::NAME);
// create a scope here to hygienically `use` the trait.
{
use $crate::MongoCollection;
$(
let [<$coll_name:snake:lower _coll>] = database.collection(schema::$coll_name::NAME);
)+
$crate::mongodb::error::Result::Ok(Self {
client,
database,
$([<$coll_name:snake:lower _coll>]),+
})
}
}
async fn ping(&self) -> $crate::mongodb::error::Result<$crate::mongodb::bson::document::Document> {
self.database.run_command($crate::mongodb::bson::doc!{"ping": 1}, std::option::Option::None).await
}
fn database(&self) -> &$crate::mongodb::Database {
&self.database
}
fn client(&self) -> &$crate::mongodb::Client {
&self.client
}
}
$(
impl $db_name {
$($impl)+
}
)?
}
};
}
/// Model a mongodb database.
///
/// This macro creates structs / functions / constants / modules that represent a mongoDB database.
/// Being a macro (which is expanded at compile time) there is no run time performance penalty when using this macro.
///
/// For a detailled syntax demonstration see [Examples](#examples).
///
/// # Structure
///
/// This macro wraps everything in a module called `mongo`.
///
/// The main database handler has the following attributes:
/// - Its name represents the database's name (eg. a database named `MyDatabase` has a struct `mongo::MyDatabase`).
/// - It implements the [`MongoClient`] trait.
/// - It contains handles to all given collections inside the database.
/// These handles have the format `{collection_name}_coll` where `{collection_name}` represents the collection's name in `snake_case`.
/// - It also contains a [`client`](mongodb::Client) and a [`database`](mongodb::Database) field for you to use.
///
/// All collections are wrapped in an additional public module named `schema`.
///
/// Each collection has its own struct which stores all specified fields.
/// All collection structs implement [`Serialize`](serde::Serialize), [`Deserialize`](serde::Deserialize) and [`MongoCollection`].
///
/// By default a field `_id` gets added to each collection automatically:
/// `pub _id: Option<DefaultId>` (see [`DefaultId`] for more info).
/// This field needs to exist for you to be able to obtain an `_id` field from the database.
/// When serializing, `_id` gets skipped if it is [`None`].
/// All fields except `_id` get renamed to `camelCase` when serializing (converting `_id` to `camelCase` results in `id`).
///
/// _Note_: All structs' names in `camelCase` can be accessed via the [`MongoClient`] / [`MongoCollection`] trait.
///
/// # Examples
///
/// ## General Examples
///
/// ```rust
/// use mongodb_ext::{mongo_db, MongoClient, MongoCollection, DefaultId};
/// use serde_json::ser;
///
/// mongo_db! {
/// // database name
/// SomeDatabase {
/// // additional attributes for the collection
/// #[derive(Debug, Clone)]
/// // collection name
/// SomeCollection {
/// // collection fields
/// first_name: String,
/// }
/// }
/// }
///
/// let mut some_document = mongo::schema::SomeCollection {
/// _id: None,
/// first_name: String::from("alice")
/// };
///
/// // When serializing, `_id` is skipped only if it is `None`.
/// // Note the key conversion to `camelCase`.
/// assert_eq!(
/// ser::to_string(&some_document).unwrap(),
/// String::from("{\"firstName\":\"alice\"}")
/// );
///
/// // update `_id` field to include in serialization.
/// let oid = DefaultId::parse_str("0123456789ABCDEF01234567").unwrap();
/// some_document._id = Some(oid);
/// assert_eq!(
/// ser::to_string(&some_document).unwrap(),
/// String::from("{\"_id\":{\"$oid\":\"0123456789abcdef01234567\"},\"firstName\":\"alice\"}")
/// );
///
/// // constants store the collection / database names in `camelCase` + collection version
/// assert_eq!("someCollection", mongo::schema::SomeCollection::NAME);
/// assert_eq!(1, mongo::schema::SomeCollection::SCHEMA_VERSION);
/// assert_eq!("someDatabase", mongo::SomeDatabase::NAME);
/// ```
///
/// Multiple collections need to be separated by `;`, a trailing `;` is optional:
///
/// ```rust
/// use mongodb_ext::{mongo_db, MongoCollection, MongoClient};
///
/// mongo_db! {
/// #[derive(Debug, Clone)]
/// MyDatabase {
/// #[derive(Debug, Clone)]
/// MyFirstCollection {
/// first_name: String,
/// last_name: String,
/// age: u8,
/// };
/// #[derive(Debug)]
/// AnotherCollection {
/// some_field: String
/// };
/// }
/// }
///
/// // all constants that were defined
/// assert_eq!("myDatabase", mongo::MyDatabase::NAME);
/// assert_eq!("myFirstCollection", mongo::schema::MyFirstCollection::NAME);
/// assert_eq!(1, mongo::schema::MyFirstCollection::SCHEMA_VERSION);
/// assert_eq!("anotherCollection", mongo::schema::AnotherCollection::NAME);
/// assert_eq!(1, mongo::schema::AnotherCollection::SCHEMA_VERSION);
///
/// // initializer function and general usage
/// // note that `tokio_test::block_on` is just a test function to run `async` code in doc tests
///
/// let mongo = tokio_test::block_on(mongo::MyDatabase::new("mongodb://example.com"))
/// .expect("Could not create mongoDB client");
///
/// let bob = mongo::schema::MyFirstCollection {
/// _id: None,
/// first_name: String::from("Bob"),
/// last_name: String::from("Bob's last name"),
/// age: 255,
/// };
///
/// // This should fail beause there is no actual mongoDB service running at the specified
/// // connection.
/// assert!(tokio_test::block_on(
/// mongo.my_first_collection_coll.insert_one(bob, None)
/// ).is_err());
/// ```
///
/// ## Manipulating / Removing `_id`
///
/// You can specify any type (that implements [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize)) to be used inside the `_id` [`Option`] by specifying it in `<` / `>` after the collection name:
///
/// ```rust
/// use mongodb_ext::mongo_db;
///
/// mongo_db! {
/// SomeDatabase {
/// SomeCollection<_id: u128> {
/// first_name: String,
/// }
/// }
/// }
///
/// // _id is now `u128` instead of `DefaultId`
/// let some_document = mongo::schema::SomeCollection {
/// _id: Some(255),
/// first_name: String::from("Bob")
/// };
/// ```
///
/// It is also possible to disable the generation of an `_id` field all together by using `<_id: none>`.
///
/// ```rust
/// use mongodb_ext::mongo_db;
///
/// mongo_db! {
/// SomeDatabase {
/// SomeCollection<_id: none> {
/// #[serde(skip_serializing_if = "Option::is_none")]
/// email_address: Option<String>,
/// first_name: String,
/// }
/// }
/// }
///
/// // no `_id` exists, this example assumes that users are addressed via their email address
/// let some_document = mongo::schema::SomeCollection {
/// email_address: Some(String::from("bob@example.com")),
/// first_name: String::from("Bob")
/// };
/// ```
///
/// These features are unique for each collection:
///
/// ```rust
/// use mongodb_ext::{mongo_db, DefaultId};
///
/// mongo_db! {
/// SomeDatabase {
/// SomeCollection<_id: u128> {
/// first_name: String,
/// };
/// Another {
/// some_field: u32,
/// };
/// AndYetAnother<_id: none> {
/// email: String,
/// name: String,
/// }
/// }
/// }
///
/// // `_id` type changed to `u128`
/// let some_document = mongo::schema::SomeCollection {
/// _id: Some(255),
/// first_name: String::from("Bob")
/// };
/// // `_id` type default, eg. `DefaultId`
/// let oid = DefaultId::parse_str("0123456789ABCDEF01234567").unwrap();
/// let another_document = mongo::schema::Another {
/// _id: Some(oid),
/// some_field: 1,
/// };
/// // `_id` field disabled
/// let and_yet_another_document = mongo::schema::AndYetAnother {
/// name: String::from("Bob"),
/// email: String::from("bob@example.com")
/// };
/// ```
///
/// Each collection that does not have a parameter of `id: none` implements a function `id(&self)` that returns a reference to its ID:
///
/// ```rust
/// use mongodb_ext::{mongo_db, DefaultId};
///
/// mongo_db! {
/// SomeDatabase {
/// SomeCollection<_id: u128> {};
/// Another {};
/// }
/// }
///
/// // `id` returns `&Option<u128>`
/// let some_collection = mongo::schema::SomeCollection {
/// _id: Some(255),
/// };
/// assert_eq!(
/// *some_collection.id(),
/// Some(255)
/// );
///
/// // `id` returns `&Option<DefaultId>`
/// let oid = DefaultId::parse_str("0123456789ABCDEF01234567").unwrap();
/// let another = mongo::schema::Another {
/// _id: Some(oid.clone()),
/// };
/// assert_eq!(
/// *another.id(),
/// Some(oid)
/// );
/// ```
///
/// ## Versioning of your schema
///
/// Your database schema version is managed via [`MongoCollection::SCHEMA_VERSION`].
///
/// This can be modified like so:
///
/// ```rust
/// use mongodb_ext::{mongo_db, MongoCollection};
/// use serde_json::ser;
///
/// mongo_db! {
/// SomeDatabase {
/// // no schema version defaults to const `DEFAULT_SCHEMA_VERSION`
/// Items {
/// name: String,
/// };
/// // schema version of 200
/// Queue<version: 200> {
/// item: i32,
/// };
/// // schema version of 4
/// SomeCollection<version: 4, _id: none> {
/// first_name: String,
/// };
/// // schema version of 5
/// FourthCollection<_id: String, version: 5> {};
/// }
/// }
///
/// // default schema version is 1
/// assert_eq!(1, mongodb_ext::DEFAULT_SCHEMA_VERSION);
///
/// assert_eq!(mongo::schema::Items::SCHEMA_VERSION, 1);
/// assert_eq!(mongo::schema::Queue::SCHEMA_VERSION, 200);
/// assert_eq!(mongo::schema::SomeCollection::SCHEMA_VERSION, 4);
/// assert_eq!(mongo::schema::FourthCollection::SCHEMA_VERSION, 5);
/// ```
///
/// ## Serializing from [`json!`](serde_json::json) and [`doc!`](mongodb::bson::doc) macros
///
/// ```rust
/// use mongodb_ext::mongo_db;
/// use serde_json::{json, Value};
/// use mongodb::{bson::{doc, Document}, bson};
///
/// mongo_db! {
/// #[derive(Debug, Clone)]
/// DatabaseOfItems {
/// #[derive(Debug, Clone, PartialEq)]
/// Items {
/// counter: u16,
/// name: String
/// };
/// }
/// }
///
/// // Note that `_id` is not specified here
/// let my_item: Value = json! ({
/// "counter": 0,
/// "name": "my_special_item"
/// });
///
/// let my_collection_entry: mongo::schema::Items =
/// serde_json::from_value(my_item)
/// .expect("Could not convert json Value to collection document");
///
/// assert_eq!(
/// my_collection_entry,
/// mongo::schema::Items {
/// _id: None,
/// counter: 0,
/// name: String::from("my_special_item")
/// }
/// );
///
/// // Note that `_id` is not specified here
/// let my_item: Document = doc! {
/// "counter": 0,
/// "name": "my_special_item"
/// };
///
/// let my_collection_entry: mongo::schema::Items = bson::de::from_document(my_item)
/// .expect("Could not convert mongodb bson Document to collection document");
///
/// assert_eq!(
/// my_collection_entry,
/// mongo::schema::Items {
/// _id: None,
/// counter: 0,
/// name: String::from("my_special_item")
/// }
/// );
/// ```
///
/// ## Adding your own code
///
/// Additional code for the `mongo` and `schema` modules can be specified in curly braces (`{` / `}`).
///
/// ```rust
/// use mongodb_ext::mongo_db;
///
/// mongo_db! {
/// // specify code to be in `mongo` here:
/// {
/// pub fn this_is_a_function_in_mongo() -> bool { true }
/// }
/// SomeDatabase {
/// // specify code to be in `schema` here:
/// {
/// pub fn this_is_a_function_in_schema() -> bool { true }
/// use std::collections::HashMap;
/// }
/// SomeCollection {
/// dict: HashMap<String, u32>,
/// }
/// }
/// }
///
/// assert!(mongo::this_is_a_function_in_mongo());
/// assert!(mongo::schema::this_is_a_function_in_schema());
/// ```
///
/// ### Code positioning
///
/// `Impl`ementations can be easily added by using the preset feature:
///
/// ```rust
/// use mongodb_ext::{mongo_db, DefaultId};
///
/// mongo_db! {
/// // specify globally needed code in `mongo` here:
/// {
/// use std::collections::HashMap;
/// }
/// SomeDatabase {
/// // specify globally needed code in `schema` here:
/// {
/// use {
/// std::collections::HashMap,
/// mongodb::bson::oid::ObjectId
/// };
/// }
///
/// // specify collection-dependent code in an additional block below the
/// // collection connected with a `-`:
/// SomeCollection {
/// dict: HashMap<String, u32>,
/// }-{
/// pub fn some_collection_function() -> bool { true }
/// };
/// #[derive(Debug, PartialEq)]
/// AnotherCollection {}-{
/// pub fn from_id(id: ObjectId) -> Self { Self { _id: Some(id) } }
/// }
/// }-{
/// // specify implementations on the database handler here:
/// pub fn give_bool() -> bool { true }
/// }
/// }
///
/// assert!(mongo::SomeDatabase::give_bool());
/// assert!(mongo::schema::SomeCollection::some_collection_function());
///
/// let oid = DefaultId::parse_str("0123456789ABCDEF01234567").unwrap();
/// assert_eq!(
/// mongo::schema::AnotherCollection::from_id(oid.clone()),
/// mongo::schema::AnotherCollection {
/// _id: Some(oid),
/// },
/// );
/// ```
///
/// ## [`TypedBuilder`](typed_builder::TypedBuilder)
///
/// Each schema implements [`TypedBuilder`](typed_builder::TypedBuilder) which lets you create a collection more easily.
///
/// If `_id` is not set to `none`, the `_id` field will have a `builder` attribute set to `default`.
/// This enables you to skip specifying `_id` as [`None`].
///
/// ```rust
/// use mongodb_ext::{mongo_db, MongoClient, MongoCollection};
///
/// mongo_db! {
/// MyDatabase {
/// #[derive(Debug, PartialEq)]
/// MyCollection<version: 2, _id: u128> {
/// name: String,
/// counter: u32,
/// schema_version: i32
/// }
/// }
/// }
///
/// use mongo::schema::MyCollection;
///
/// assert_eq!(
/// // constructing using the builder
/// // note that no field `_id` is specified, thus `None` is used
/// MyCollection::builder()
/// .name("Alice".to_string())
/// .counter(1)
/// .schema_version(MyCollection::SCHEMA_VERSION)
/// .build(),
/// // constructing normally
/// MyCollection {
/// _id: None,
/// name: "Alice".to_string(),
/// counter: 1,
/// schema_version: MyCollection::SCHEMA_VERSION
/// }
/// );
/// ```
///
/// Combining the schema version with the typed builder can be very useful:
///
/// ```rust
/// use mongodb_ext::{mongo_db, MongoClient, MongoCollection};
///
/// mongo_db! {
/// MyDatabase {
/// {
/// use mongodb_ext::MongoCollection;
/// }
/// #[derive(Debug, PartialEq)]
/// MyCollection<version: 2, _id: u128> {
/// name: String,
/// counter: u32,
/// #[builder(default = <MyCollection as MongoCollection>::SCHEMA_VERSION)]
/// schema_version: i32
/// }
/// }
/// }
///
/// use mongo::schema::MyCollection;
///
/// assert_eq!(
/// // specifying no version takes version constant by default
/// MyCollection::builder()
/// .name("Alice".to_string())
/// .counter(255)
/// .build(),
/// MyCollection {
/// _id: None,
/// name: "Alice".to_string(),
/// counter: 255,
/// schema_version: 2
/// }
/// );
/// ```
#[macro_export]
macro_rules! mongo_db {
// only one match, the real magic happens in `expand_collection` and `expand_main_client`
(
$({
$($outer_tokens:tt)+
})?
$(#[$additional_db_attr:meta])*
$db_name:ident {
$({
$($inner_tokens:tt)+
})?
$(
$(#[$additional_coll_attr:meta])*
$coll_name:ident$(<$($collection_param_name:ident: $collection_param_value:tt),+>)? {
$(
$(#[$additional_field_attr:meta])*
$field:ident: $field_type:ty
),*$(,)?
}
$(-{
$($inner_impl:tt)+
})?
);+$(;)?
}
$(-{
$($outer_impl:tt)+
})?
) => {
pub mod mongo {
$($($outer_tokens)*)?
pub mod schema {
$($($inner_tokens)*)?
$(
$crate::parse_collection_params! {
$(
$($collection_param_name: $collection_param_value),+
)?
$(#[$additional_coll_attr])*
$coll_name {
$(
$(#[$additional_field_attr])*
$field: $field_type
),*
}
$(-{
$($inner_impl)+
})?
}
)+
}
$crate::expand_main_client ! {
$(#[$additional_db_attr])*
$db_name {
$(
$(#[$additional_coll_attr])*
$coll_name<_id: none> {
$(
$(#[$additional_field_attr])*
$field: $field_type
),*
}
),+
}
$(-{
$($outer_impl)+
})?
}
}
};
}