blob: de99f27067dbcdb352f6fe295106568f507f7dfe [file] [log] [blame]
David Tolnayf38cdf62016-09-23 19:07:09 -07001use super::*;
2
David Tolnay9bf4af82017-01-07 11:17:46 -08003#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayf38cdf62016-09-23 19:07:09 -07004pub 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
David Tolnay9bf4af82017-01-07 11:17:46 -080012#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayf38cdf62016-09-23 19:07:09 -070013pub enum Body {
14 Enum(Vec<Variant>),
15 Struct(VariantData),
16}
17
18#[cfg(feature = "parsing")]
19pub mod parsing {
20 use super::*;
David Tolnay3cf52982016-10-01 17:11:37 -070021 use Generics;
David Tolnay4a51dc72016-10-01 00:40:31 -070022 use attr::parsing::outer_attr;
David Tolnayf38cdf62016-09-23 19:07:09 -070023 use data::parsing::{visibility, struct_body, enum_body};
David Tolnay28c1db62016-10-27 22:48:18 -070024 use generics::parsing::generics;
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),
David Tolnay28c1db62016-10-27 22:48:18 -070034 "struct" => map!(struct_body, move |(wh, body)| MacroInput {
David Tolnayf38cdf62016-09-23 19:07:09 -070035 ident: id,
36 vis: vis,
37 attrs: attrs,
David Tolnay3cf52982016-10-01 17:11:37 -070038 generics: Generics {
David Tolnay28c1db62016-10-27 22:48:18 -070039 where_clause: wh,
David Tolnay3cf52982016-10-01 17:11:37 -070040 .. generics
41 },
David Tolnayf38cdf62016-09-23 19:07:09 -070042 body: Body::Struct(body),
43 })
44 |
David Tolnay28c1db62016-10-27 22:48:18 -070045 "enum" => map!(enum_body, move |(wh, body)| MacroInput {
David Tolnayf38cdf62016-09-23 19:07:09 -070046 ident: id,
47 vis: vis,
48 attrs: attrs,
David Tolnay3cf52982016-10-01 17:11:37 -070049 generics: Generics {
David Tolnay28c1db62016-10-27 22:48:18 -070050 where_clause: wh,
David Tolnay3cf52982016-10-01 17:11:37 -070051 .. generics
52 },
David Tolnayf38cdf62016-09-23 19:07:09 -070053 body: Body::Enum(body),
54 })
55 ) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070056 (item)
57 ));
58}
59
David Tolnayc2dfbf42016-09-23 23:52:15 -070060#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -070061mod printing {
62 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -070063 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -070064 use data::VariantData;
David Tolnayf38cdf62016-09-23 19:07:09 -070065 use quote::{Tokens, ToTokens};
66
67 impl ToTokens for MacroInput {
68 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -070069 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -070070 attr.to_tokens(tokens);
71 }
David Tolnay47a877c2016-10-01 16:50:55 -070072 self.vis.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -070073 match self.body {
74 Body::Enum(_) => tokens.append("enum"),
75 Body::Struct(_) => tokens.append("struct"),
76 }
77 self.ident.to_tokens(tokens);
78 self.generics.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -070079 match self.body {
David Tolnayf38cdf62016-09-23 19:07:09 -070080 Body::Enum(ref variants) => {
David Tolnay28c1db62016-10-27 22:48:18 -070081 self.generics.where_clause.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -070082 tokens.append("{");
83 for variant in variants {
84 variant.to_tokens(tokens);
85 tokens.append(",");
86 }
87 tokens.append("}");
88 }
89 Body::Struct(ref variant_data) => {
David Tolnayf38cdf62016-09-23 19:07:09 -070090 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -070091 VariantData::Struct(_) => {
David Tolnay28c1db62016-10-27 22:48:18 -070092 self.generics.where_clause.to_tokens(tokens);
93 variant_data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -070094 // no semicolon
95 }
David Tolnay28c1db62016-10-27 22:48:18 -070096 VariantData::Tuple(_) => {
97 variant_data.to_tokens(tokens);
98 self.generics.where_clause.to_tokens(tokens);
99 tokens.append(";");
100 }
101 VariantData::Unit => {
102 self.generics.where_clause.to_tokens(tokens);
103 tokens.append(";");
104 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700105 }
106 }
107 }
108 }
109 }
110}