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
/*!
MongoDB Macro is a crate with macros for quickly creating structures when working with mongodb.
In particular, macros are implemented for quick initialization of structures of the "factory" pattern.
The purpose of this crate is to reduce the amount of boilerplate code when initializing standard structures.

# installation

Install using cargo:

`cargo install mongodb-macro`

Make sure you also add to the project:

> mongodb = "*"
>
> clap = { version = "*", features = ["derive", "env"] }

# Usage
## Macro: Collection
```no_run

use mongodb::bson::Bson;

// env DB_URL should contain a link to the mongodb url
// env DB_NAME should contain the database name
// env COLLECTION_NAME should contain the collection name
mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts);
// or with a specified env
// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME"));

async fn main() -> std::io::Result<()> {

    // std::env::set_var("MONGODB_HOST", "localhost");
    // std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");

    let factory = CollectionFactory::parse();

    let collection = factory.create::<Bson>().await.expect("failed to connect");
    
    ...
}
```

## Macro: Database
```no_run

use mongodb::bson::Bson;

// env DB_URL should contain a link to the mongodb url
// env DB_NAME should contain the database name
mongodb_macro::database!(DbFactory; DbFactoryOpts);
// or with a specified env
// mongodb_macro::database!(DbFactory; DbFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME"));

async fn main() -> std::io::Result<()> {

    let factory = DbFactory::parse();

    let db = factory.create().await.expect("failed to connect");

    let collection = db.collection::<Bson>("users");
    
    ...
}
```

## Macro: Client
```no_run

use mongodb::bson::Bson;

// env DB_URL should contain a link to the mongodb url
mongodb_macro::client!(ClientFactory; ClientFactoryOpts);
// or with a specified env
// mongodb_macro::client!(ClientFactory; ClientFactoryOpts; "MONGO_DB_URL");

async fn main() -> std::io::Result<()> {

    let factory = ClientFactory::parse();

    let client = factory.create().await.expect("failed to connect");

    let db = client.database("demo");
    let collection = db.collection::<Bson>("users");
    
    ...
}
```

## Macro: Config
```no_run

use mongodb::bson::Bson;

mongodb_macro::config!(Opts);
// equivalent to
// mongodb_macro::config!(Opts; "DB_NAME", "COLLECTION_NAME", "DB_URL");
//
// or with prefix
//
// mongodb_macro::config!(Opts, "MONGO");
// equivalent to
// mongodb_macro::config!(Opts; "MONGO_DB_NAME", "MONGO_COLLECTION_NAME", "MONGO_DB_URL");

async fn main() -> std::io::Result<()> {

    let opts = Opts::parse();

    let client = mongodb::Client::with_uri_str(&opts.db_url).await.expect("failed to connect");
    let db = client.database(&opts.db_name);
    let collection = db.collection::<Bson>(&opts.collection_name);
    
    ...
}
```
*/
#![warn(missing_docs)]


mod env;
/// Macros for creating MongoDB configurations from environment variables
#[doc(hidden)]
pub mod opts;

/// Macros for creating MongoDB client configurations and factories from environment variables
#[doc(hidden)]
pub mod client;

/// Macros for creating MongoDB database configurations and factories from environment variables
#[doc(hidden)]
pub mod database;

/// Macros for creating MongoDB collection configurations and factories from environment variables
#[doc(hidden)]
pub mod collection;

pub use env::env_expand;