Macro config

Source
macro_rules! config {
    ($opts:ident, $prefix:tt) => { ... };
    ($opts:ident, $prefix:tt; ($db_url:tt, $db_name:tt, $collection_name:tt)) => { ... };
    ($opts:ident) => { ... };
    ($opts:ident; $db_url:tt, $db_name:tt, $collection_name:tt) => { ... };
}
Expand description

Quick and easy creates a new configuration to connect to MongoDB

Creating a configuration structure when using one database in a project:

use mongodb_macro::Parser;
mongodb_macro::config!(Opts);

fn main() {
    std::env::set_var("DB_URL", "mongodb://root:root@localhost:27017");
    std::env::set_var("DB_NAME", "test");
    std::env::set_var("COLLECTION_NAME", "users");
 
    let opts = Opts::parse();
}

Creating a configuration structure when using multiple databases in a project:

This sets the prefix to the environment variables.

use mongodb_macro::Parser;
mongodb_macro::config!(Opts, "MONGO");

fn main() {
    std::env::set_var("MONGO_DB_URL", "mongodb://root:root@localhost:27017");
    std::env::set_var("MONGO_DB_NAME", "test");
    std::env::set_var("MONGO_COLLECTION_NAME", "users");
 
    let opts = Opts::parse();
}

Creating a configuration structure with explicit fields:

use mongodb_macro::Parser;
mongodb_macro::config!(Opts; "MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME");

fn main() {
    std::env::set_var("MONGO_DB_URL", "mongodb://root:root@localhost:27017");
    std::env::set_var("MONGO_DB_NAME", "test");
    std::env::set_var("MONGO_COLLECTION_NAME", "users");
 
    let opts = Opts::parse();
}