mongodb_macro/
client.rs

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