blob: c2b5900c39941b6166c85440f357a59b662c39f1 [file] [log] [blame]
David Tolnayf38cdf62016-09-23 19:07:09 -07001use super::*;
2
3#[derive(Debug, Clone, Eq, PartialEq)]
4pub struct MacroInput {
5 pub ident: Ident,
6 pub vis: Visibility,
7 pub attrs: Vec<Attribute>,
8 pub generics: Generics,
9 pub body: Body,
10}
11
12#[derive(Debug, Clone, Eq, PartialEq)]
13pub enum Body {
14 Enum(Vec<Variant>),
15 Struct(VariantData),
16}
17
18#[cfg(feature = "parsing")]
19pub mod parsing {
20 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -070021 use attr::parsing::outer_attr;
David Tolnayf38cdf62016-09-23 19:07:09 -070022 use data::parsing::{visibility, struct_body, enum_body};
23 use generics::parsing::generics;
24 use ident::parsing::ident;
25 use nom::multispace;
26
27 named!(pub macro_input -> MacroInput, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -070028 attrs: many0!(outer_attr) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070029 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -070030 which: alt!(keyword!("struct") | keyword!("enum")) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070031 id: ident >>
32 generics: generics >>
33 item: switch!(value!(which),
34 "struct" => map!(struct_body, move |body| MacroInput {
35 ident: id,
36 vis: vis,
37 attrs: attrs,
38 generics: generics,
39 body: Body::Struct(body),
40 })
41 |
42 "enum" => map!(enum_body, move |body| MacroInput {
43 ident: id,
44 vis: vis,
45 attrs: attrs,
46 generics: generics,
47 body: Body::Enum(body),
48 })
49 ) >>
50 option!(multispace) >>
51 (item)
52 ));
53}
54
David Tolnayc2dfbf42016-09-23 23:52:15 -070055#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -070056mod printing {
57 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -070058 use attr::FilterAttrs;
David Tolnayf38cdf62016-09-23 19:07:09 -070059 use data::{Visibility, VariantData};
60 use quote::{Tokens, ToTokens};
61
62 impl ToTokens for MacroInput {
63 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -070064 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -070065 attr.to_tokens(tokens);
66 }
67 if let Visibility::Public = self.vis {
68 tokens.append("pub");
69 }
70 match self.body {
71 Body::Enum(_) => tokens.append("enum"),
72 Body::Struct(_) => tokens.append("struct"),
73 }
74 self.ident.to_tokens(tokens);
75 self.generics.to_tokens(tokens);
76 self.generics.where_clause.to_tokens(tokens);
77 self.body.to_tokens(tokens);
78 }
79 }
80
81 impl ToTokens for Body {
82 fn to_tokens(&self, tokens: &mut Tokens) {
83 match *self {
84 Body::Enum(ref variants) => {
85 tokens.append("{");
86 for variant in variants {
87 variant.to_tokens(tokens);
88 tokens.append(",");
89 }
90 tokens.append("}");
91 }
92 Body::Struct(ref variant_data) => {
93 variant_data.to_tokens(tokens);
94 match *variant_data {
95 VariantData::Struct(_) => { /* no semicolon */ }
96 VariantData::Tuple(_) |
97 VariantData::Unit => tokens.append(";"),
98 }
99 }
100 }
101 }
102 }
103}