blob: 149b6d7bad2110a57e8dbcd5659db7c549fe0fc7 [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;
David Tolnay14cbdeb2016-10-01 12:13:59 -070024 use space::whitespace;
David Tolnayf38cdf62016-09-23 19:07:09 -070025 use ident::parsing::ident;
David Tolnayf38cdf62016-09-23 19:07:09 -070026
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 ) >>
David Tolnay14cbdeb2016-10-01 12:13:59 -070050 option!(whitespace) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070051 (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 Tolnay47a877c2016-10-01 16:50:55 -070059 use data::VariantData;
David Tolnayf38cdf62016-09-23 19:07:09 -070060 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 }
David Tolnay47a877c2016-10-01 16:50:55 -070067 self.vis.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -070068 match self.body {
69 Body::Enum(_) => tokens.append("enum"),
70 Body::Struct(_) => tokens.append("struct"),
71 }
72 self.ident.to_tokens(tokens);
73 self.generics.to_tokens(tokens);
74 self.generics.where_clause.to_tokens(tokens);
75 self.body.to_tokens(tokens);
76 }
77 }
78
79 impl ToTokens for Body {
80 fn to_tokens(&self, tokens: &mut Tokens) {
81 match *self {
82 Body::Enum(ref variants) => {
83 tokens.append("{");
84 for variant in variants {
85 variant.to_tokens(tokens);
86 tokens.append(",");
87 }
88 tokens.append("}");
89 }
90 Body::Struct(ref variant_data) => {
91 variant_data.to_tokens(tokens);
92 match *variant_data {
93 VariantData::Struct(_) => { /* no semicolon */ }
94 VariantData::Tuple(_) |
95 VariantData::Unit => tokens.append(";"),
96 }
97 }
98 }
99 }
100 }
101}