mg_settings_macros/
lib.rs

1/*
2 * Copyright (c) 2016-2017 Boucher, Antoni <bouanto@zoho.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of
5 * this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to
7 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8 * the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22#![recursion_limit="128"]
23
24extern crate env_logger;
25#[macro_use]
26extern crate log;
27extern crate proc_macro;
28#[macro_use]
29extern crate quote;
30extern crate syn;
31
32mod attributes;
33mod commands;
34mod settings;
35mod string;
36
37use std::env;
38use std::io::Write;
39
40use env_logger::Builder;
41use proc_macro::TokenStream;
42
43use commands::expand_commands_enum;
44use settings::{expand_setting_enum, expand_settings_enum};
45
46fn init_logger() {
47    let mut builder = Builder::new();
48    builder.format(|buf, record| {
49        writeln!(buf, "{}", record.args())
50    });
51    if let Ok(rust_log) = env::var("RUST_LOG") {
52        builder.parse(&rust_log);
53    }
54    builder.try_init();
55}
56
57#[proc_macro_derive(Commands, attributes(completion, count, help, special_command))]
58/// Derive Commands.
59pub fn commands(input: TokenStream) -> TokenStream {
60    init_logger();
61    let ast = syn::parse(input).unwrap();
62    let expanded = expand_commands_enum(ast);
63    warn!("{}", expanded.to_string());
64    expanded.into()
65}
66
67#[proc_macro_derive(Setting, attributes(default))]
68/// Derive Setting.
69pub fn setting(input: TokenStream) -> TokenStream {
70    init_logger();
71    let ast = syn::parse(input).unwrap();
72    let expanded = expand_setting_enum(ast);
73    warn!("{}", expanded.to_string());
74    expanded.into()
75}
76
77#[proc_macro_derive(Settings, attributes(help))]
78/// Derive Settings.
79pub fn settings(input: TokenStream) -> TokenStream {
80    init_logger();
81    let ast = syn::parse(input).unwrap();
82    let expanded = expand_settings_enum(ast);
83    warn!("{}", expanded.to_string());
84    expanded.into()
85}