mongodb_macro/
collection.rs

1
2
3/// Creates a new configuration structure to initialize the MongoDB collection
4/// 
5/// Create a new configuration structure to initialize the MongoDB collection with a standard environment variable
6/// 
7/// ```
8/// use mongodb_macro::Parser;
9/// mongodb_macro::collection_config!(Opts);
10///
11/// fn main() {
12///     std::env::set_var("DB_URL", "mongodb://root:root@localhost:27017");
13///     std::env::set_var("DB_NAME", "test");
14///     std::env::set_var("COLLECTION_NAME", "users");
15/// 
16///     let opts = Opts::parse();
17/// }
18/// ```
19/// 
20/// Create a new configuration structure to initialize the MongoDB collection with the specified environment variable
21/// 
22/// ```
23/// use mongodb_macro::Parser;
24/// mongodb_macro::collection_config!(Opts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME"));
25///
26/// fn main() {
27///     std::env::set_var("MONGO_DB_URL", "mongodb://root:root@localhost:27017");
28///     std::env::set_var("MONGO_DB_NAME", "test");
29///     std::env::set_var("MONGO_COLLECTION_NAME", "users");
30/// 
31///     let opts = Opts::parse();
32/// }
33/// ```
34#[macro_export]
35macro_rules! collection_config {
36    ($opts:ident) => ($crate::config!{$opts});
37
38    ($opts:ident; ($db_url:tt, $db_name:tt, $collection_name:tt)) => 
39    ($crate::config!{$opts; $db_url, $db_name, $collection_name});
40}
41
42/// Creates a new factory to create a MongoDB collection
43/// 
44/// Create mongodb collection factory with standard environment variable for database url, database name and collection name
45/// 
46/// ```
47/// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts);
48///
49/// fn main() {
50///     std::env::set_var("DB_URL", "mongodb://root:root@localhost:27017");
51///     std::env::set_var("DB_NAME", "test");
52///     std::env::set_var("COLLECTION_NAME", "users");
53/// 
54///     let factory = CollectionFactory::parse();
55/// 
56///     // let collection = factory.create<Bson>().await.expect("failed to connect");
57/// }
58/// ```
59/// 
60/// Create mongodb collection factory with specified environment variable for database url, database name and collection name
61/// 
62/// ```
63/// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME"));
64///
65/// fn main() {
66///     std::env::set_var("MONGO_DB_URL", "mongodb://root:root@localhost:27017");
67///     std::env::set_var("MONGO_DB_NAME", "test");
68///     std::env::set_var("MONGO_COLLECTION_NAME", "users");
69/// 
70///     let factory = CollectionFactory::parse();
71///
72///     // let collection = factory.create<Bson>().await.expect("failed to connect");
73/// }
74/// ```
75/// 
76/// Create a mongodb collection factory with nested environment variables into standard environment variables
77/// 
78/// ```
79/// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts);
80///
81/// fn main() {
82///     std::env::set_var("MONGODB_HOST", "localhost");
83///     std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");
84///     std::env::set_var("DB_NAME", "test");
85///     std::env::set_var("COLLECTION_NAME", "users");
86/// 
87///     let factory = CollectionFactory::parse();
88/// 
89///     // let collection = factory.create<Bson>().await.expect("failed to connect");
90/// 
91///     assert_eq!(&factory.config().db_url, "mongodb://root:root@localhost:27017");
92/// }
93/// ```
94#[macro_export]
95macro_rules! collection {
96    ($collection_factory:ident; $opts:ident) => ($crate::collection!{$collection_factory; $opts; ("DB_URL", "DB_NAME", "COLLECTION_NAME")});
97
98    ($collection_factory:ident; $opts:ident; ($db_url:tt, $db_name:tt, $collection_name:tt)) => {
99
100        $crate::collection_config!($opts; ($db_url, $db_name, $collection_name));
101
102        #[derive(Clone, Debug, PartialEq, Eq)]
103        pub struct $collection_factory($opts);
104
105        impl $collection_factory {
106            fn parse() -> Self {
107                use $crate::Parser;
108                
109                let opts = $opts::parse();
110
111                Self(opts)
112            }
113
114            pub fn config(&self) -> &$opts {
115                &self.0
116            }
117
118            pub async fn create<T>(&self) -> Result<::mongodb::Collection<T>, ::mongodb::error::Error> {
119                let client = ::mongodb::Client::with_uri_str(&self.0.db_url).await?;
120                let db = client.database(&self.0.db_name);
121                Ok(db.collection::<T>(&self.0.collection_name))
122            }
123        }
124    };
125}