Macro database_config

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

Creates a new configuration structure to initialize the MongoDB database

Create a new configuration structure to initialize the MongoDB database with a standard environment variable

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

fn main() {
    std::env::set_var("MONGODB_HOST", "localhost");
    std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");
    std::env::set_var("DB_NAME", "test");
 
    let opts = Opts::parse();
 
    assert_eq!(&opts.db_url, "mongodb://root:root@localhost:27017");
}

Create a new configuration structure to initialize the MongoDB database with the specified environment variable

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

fn main() {
    std::env::set_var("MONGODB_HOST", "localhost");
    std::env::set_var("MONGO_DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");
    std::env::set_var("MONGO_DB_NAME", "test");
 
    let opts = Opts::parse();
 
    assert_eq!(&opts.db_url, "mongodb://root:root@localhost:27017");
}