#code #generation #proc-macro #config #rocket-config

nightly macro rocket-config-codegen

Procedural macros for the rocket-config library

2 releases

✓ Uses Rust 2018 edition

0.0.2 Oct 4, 2019
0.0.1 Oct 4, 2019

#47 in Configuration


Used in 2 crates (1 directly)

MIT/Apache

7KB
100 lines

rocket-config

rocket-config is a Fairing designed for Rocket, a web framework for Rust (nightly).

#![feature(proc_macro_hygiene)]

#[macro_use] extern crate rocket;
extern crate rocket_config;
#[macro_use] extern crate rocket_config_codegen;

// This will generate the DieselConfiguration struct
// used below.
configuration!("diesel");

use rocket_config::Factory as ConfigurationsFairing;

// Here, `_configuration` contains the parsed configuration
// file "diesel.{json,yml,yaml}"
#[get("/<name>/<age>")]
fn hello(_configuration: DieselConfiguration, name: String, age: u8)
-> String
{
    format!("Hello, {} year old named {}!", age, name)
}

fn main() {
    rocket::ignite()
        .attach(ConfigurationsFairing::new())
        .mount("/hello", routes![hello]).launch();
}

lib.rs:

Rocket-Config - Code Generation

This crate implements the code generation portions of Rocket-config. This includes procedural macros.

Procedural Macros

This crate implements the following procedural macros:

  • configuration

The syntax for the configuration macro is:

 macro := configuration!(CONFIGURATION_FILE_STEM)
 

Usage

You should not directly depend on this library. To use the macros, it suffices to depend on rocket-config in Cargo.toml:

[dependencies]
rocket-config = "0.0.1"

And to import the macros via #[macro_use] in the crate root:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket_config;

configuration!("test");

// ...

Or, alternatively, selectively import from the top-level scope:

#![feature(proc_macro_hygiene, decl_macro)]

extern crate rocket_config;

use rocket_config::configuration;

configuration!("test");

// ...

Dependencies

~0.8–1MB
~23K SLoC