blob: 00a31e10882a9fd51d71e0156ebfa9004b21a9e2 [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 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 >>
David Tolnay3cf52982016-10-01 17:11:37 -070033 where_clause: where_clause >>
David Tolnayf38cdf62016-09-23 19:07:09 -070034 item: switch!(value!(which),
35 "struct" => map!(struct_body, move |body| MacroInput {
36 ident: id,
37 vis: vis,
38 attrs: attrs,
David Tolnay3cf52982016-10-01 17:11:37 -070039 generics: Generics {
40 where_clause: where_clause,
41 .. generics
42 },
David Tolnayf38cdf62016-09-23 19:07:09 -070043 body: Body::Struct(body),
44 })
45 |
46 "enum" => map!(enum_body, move |body| MacroInput {
47 ident: id,
48 vis: vis,
49 attrs: attrs,
David Tolnay3cf52982016-10-01 17:11:37 -070050 generics: Generics {
51 where_clause: where_clause,
52 .. generics
53 },
David Tolnayf38cdf62016-09-23 19:07:09 -070054 body: Body::Enum(body),
55 })
56 ) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070057 (item)
58 ));
59}
60
David Tolnayc2dfbf42016-09-23 23:52:15 -070061#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -070062mod printing {
63 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -070064 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -070065 use data::VariantData;
David Tolnayf38cdf62016-09-23 19:07:09 -070066 use quote::{Tokens, ToTokens};
67
68 impl ToTokens for MacroInput {
69 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -070070 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -070071 attr.to_tokens(tokens);
72 }
David Tolnay47a877c2016-10-01 16:50:55 -070073 self.vis.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -070074 match self.body {
75 Body::Enum(_) => tokens.append("enum"),
76 Body::Struct(_) => tokens.append("struct"),
77 }
78 self.ident.to_tokens(tokens);
79 self.generics.to_tokens(tokens);
80 self.generics.where_clause.to_tokens(tokens);
81 self.body.to_tokens(tokens);
82 }
83 }
84
85 impl ToTokens for Body {
86 fn to_tokens(&self, tokens: &mut Tokens) {
87 match *self {
88 Body::Enum(ref variants) => {
89 tokens.append("{");
90 for variant in variants {
91 variant.to_tokens(tokens);
92 tokens.append(",");
93 }
94 tokens.append("}");
95 }
96 Body::Struct(ref variant_data) => {
97 variant_data.to_tokens(tokens);
98 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -070099 VariantData::Struct(_) => {
100 // no semicolon
101 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700102 VariantData::Tuple(_) |
103 VariantData::Unit => tokens.append(";"),
104 }
105 }
106 }
107 }
108 }
109}