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
//! <center>
//!   <b>Pro</b>cedural <b>M</b>acro <b>Attri</b>but<b>e</b>s
//!   <br>
//!   For when you need the best dressed procedural macro.
//! </center>
//!
//! `prom_attire` lets you define a struct (or multiple) that you can use to
//! parse the attributes passed in to your procedural macro.
//!
//! # Examples
//!
//! ## Basic example
//!
//! ```rust
//! # #[macro_use] extern crate prom_attire;
//! # extern crate syn;
//! # fn main() {
//! # fn foo() -> Result<(), String> {
//! #[derive(PromAttire, PartialEq, Debug)]
//! struct Attributes<'a> {
//!     awesome: Option<&'a str>,
//! }
// TODO: rustdoc has issues with raw string literals, switch to those once
// fixed (extra TODO, open rust-lang/rust bug ticket about them and link)
//! let ast = syn::parse_derive_input("
//!     #[awesome = \"yes\"]
//!     struct Foo {}
//! ")?;
//! let attrs = Attributes::from(ast.attrs.as_slice());
//! assert_eq!(attrs, Attributes {
//!     awesome: Some("yes"),
//! });
//! # Ok(())
//! # }
//! # foo().unwrap()
//! # }
//! ```
//!
//! More examples **Coming Soon**, for now if you check [the list of examples
//! to add](https://github.com/Nemo157/prom-attire-rs/issues/5) and [look at
//! the tests](https://github.com/Nemo157/prom-attire-rs/tree/master/tests) you
//! may be able to work out how to do what you want.


extern crate proc_macro;
extern crate syn;
extern crate error_chain;

#[macro_use]
extern crate prom_attire_bootstrap;
extern crate prom_attire_impl;

use error_chain::ChainedError;

#[derive(PromAttireBootstrap)]
struct Attributes<'a> {
    #[attire_bootstrap(scope)]
    scope: Option<&'a str>,
    #[attire_bootstrap(docs)]
    docs: Option<&'a str>,
}

#[derive(PromAttireBootstrap)]
struct FieldAttributes<'a> {
    #[attire_bootstrap(field_attribute)]
    attribute: Option<&'a str>,
    #[attire_bootstrap(field_split_attribute_of)]
    split_attribute_of: Option<&'a str>,
    #[attire_bootstrap(field_default)]
    default: prom_attire_impl::Defaulted,
    #[attire_bootstrap(field_flag_value)]
    flag_value: Option<&'a str>,
}

/// The procedural macro implementing `#[derive(PromAttire)]`
#[proc_macro_derive(PromAttire, attributes(attire))]
pub fn app(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = input.to_string();

    let ast = match syn::parse_derive_input(&input) {
        Ok(ast) => ast,
        Err(err) => {
            println!("{}", err);
            panic!("Internal error in prom-attire (probably)");
        }
    };

    let attrs = match Attributes::try_from(ast.attrs.as_slice()) {
        Ok(attrs) => attrs,
        Err(errs) => {
            for err in errs {
                println!("{}", err);
            }
            panic!("Invalid attributes specified for prom-attire macro");
        }
    };

    let config = prom_attire_impl::Config {
        scope: attrs.scope,
        docs: attrs.docs,
        parse_field_config: &|attrs| {
            let attrs = match FieldAttributes::try_from(attrs) {
                Ok(attrs) => attrs,
                Err(errs) => {
                    for err in errs {
                        println!("{}", err);
                    }
                    panic!("Invalid attributes specified for prom-attire macro");
                }
            };
            prom_attire_impl::FieldConfig {
                attribute: attrs.attribute,
                split_attribute_of: attrs.split_attribute_of,
                default: attrs.default,
                flag_value: attrs.flag_value,
            }
        }
    };

    let expanded = match prom_attire_impl::derive(&input, &config) {
        Ok(expanded) => expanded,
        Err(err) => {
            println!("{}", err.display());
            panic!("Expanding prom-attire failed");
        }
    };

    match expanded.parse() {
        Ok(parsed) => parsed,
        Err(err) => {
            println!("{:?}", err);
            panic!("Internal error in prom-attire");
        }
    }
}