blob: 77f1c55a3d2cf9a2fcb2d179c22c51a3c078b0ed [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 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 Tolnay3cf52982016-10-01 17:11:37 -070024 use generics::parsing::{generics, where_clause};
David Tolnay14cbdeb2016-10-01 12:13:59 -070025 use space::whitespace;
David Tolnayf38cdf62016-09-23 19:07:09 -070026 use ident::parsing::ident;
David Tolnayf38cdf62016-09-23 19:07:09 -070027
28 named!(pub macro_input -> MacroInput, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -070029 attrs: many0!(outer_attr) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070030 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -070031 which: alt!(keyword!("struct") | keyword!("enum")) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070032 id: ident >>
33 generics: generics >>
David Tolnay3cf52982016-10-01 17:11:37 -070034 where_clause: where_clause >>
David Tolnayf38cdf62016-09-23 19:07:09 -070035 item: switch!(value!(which),
36 "struct" => map!(struct_body, move |body| MacroInput {
37 ident: id,
38 vis: vis,
39 attrs: attrs,
David Tolnay3cf52982016-10-01 17:11:37 -070040 generics: Generics {
41 where_clause: where_clause,
42 .. generics
43 },
David Tolnayf38cdf62016-09-23 19:07:09 -070044 body: Body::Struct(body),
45 })
46 |
47 "enum" => map!(enum_body, move |body| MacroInput {
48 ident: id,
49 vis: vis,
50 attrs: attrs,
David Tolnay3cf52982016-10-01 17:11:37 -070051 generics: Generics {
52 where_clause: where_clause,
53 .. generics
54 },
David Tolnayf38cdf62016-09-23 19:07:09 -070055 body: Body::Enum(body),
56 })
57 ) >>
David Tolnay14cbdeb2016-10-01 12:13:59 -070058 option!(whitespace) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070059 (item)
60 ));
61}
62
David Tolnayc2dfbf42016-09-23 23:52:15 -070063#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -070064mod printing {
65 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -070066 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -070067 use data::VariantData;
David Tolnayf38cdf62016-09-23 19:07:09 -070068 use quote::{Tokens, ToTokens};
69
70 impl ToTokens for MacroInput {
71 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -070072 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -070073 attr.to_tokens(tokens);
74 }
David Tolnay47a877c2016-10-01 16:50:55 -070075 self.vis.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -070076 match self.body {
77 Body::Enum(_) => tokens.append("enum"),
78 Body::Struct(_) => tokens.append("struct"),
79 }
80 self.ident.to_tokens(tokens);
81 self.generics.to_tokens(tokens);
82 self.generics.where_clause.to_tokens(tokens);
83 self.body.to_tokens(tokens);
84 }
85 }
86
87 impl ToTokens for Body {
88 fn to_tokens(&self, tokens: &mut Tokens) {
89 match *self {
90 Body::Enum(ref variants) => {
91 tokens.append("{");
92 for variant in variants {
93 variant.to_tokens(tokens);
94 tokens.append(",");
95 }
96 tokens.append("}");
97 }
98 Body::Struct(ref variant_data) => {
99 variant_data.to_tokens(tokens);
100 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -0700101 VariantData::Struct(_) => {
102 // no semicolon
103 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700104 VariantData::Tuple(_) |
105 VariantData::Unit => tokens.append(";"),
106 }
107 }
108 }
109 }
110 }
111}