blob: 3b22b0f21d6f255a4734c15b77d28d13e6a94707 [file] [log] [blame]
David Tolnayf38cdf62016-09-23 19:07:09 -07001use super::*;
2
Alex Crichton62a0a592017-05-22 13:58:53 -07003ast_struct! {
4 /// Struct or enum sent to a `proc_macro_derive` macro.
5 pub struct DeriveInput {
6 /// Name of the struct or enum.
7 pub ident: Ident,
Clar Charrd22b5702017-03-10 15:24:56 -05008
Alex Crichton62a0a592017-05-22 13:58:53 -07009 /// Visibility of the struct or enum.
10 pub vis: Visibility,
Clar Charrd22b5702017-03-10 15:24:56 -050011
Alex Crichton62a0a592017-05-22 13:58:53 -070012 /// Attributes tagged on the whole struct or enum.
13 pub attrs: Vec<Attribute>,
Clar Charrd22b5702017-03-10 15:24:56 -050014
Alex Crichton62a0a592017-05-22 13:58:53 -070015 /// Generics required to complete the definition.
16 pub generics: Generics,
Clar Charrd22b5702017-03-10 15:24:56 -050017
Alex Crichton62a0a592017-05-22 13:58:53 -070018 /// Data within the struct or enum.
19 pub body: Body,
20 }
David Tolnayf38cdf62016-09-23 19:07:09 -070021}
22
Clar Charrd22b5702017-03-10 15:24:56 -050023
Alex Crichton62a0a592017-05-22 13:58:53 -070024ast_enum! {
25 /// Body of a derived struct or enum.
26 pub enum Body {
27 /// It's an enum.
28 Enum(Vec<Variant>),
29
30 /// It's a struct.
31 Struct(VariantData),
32 }
David Tolnayf38cdf62016-09-23 19:07:09 -070033}
34
35#[cfg(feature = "parsing")]
36pub mod parsing {
37 use super::*;
David Tolnay3cf52982016-10-01 17:11:37 -070038 use Generics;
David Tolnay4a51dc72016-10-01 00:40:31 -070039 use attr::parsing::outer_attr;
David Tolnayf38cdf62016-09-23 19:07:09 -070040 use data::parsing::{visibility, struct_body, enum_body};
David Tolnay28c1db62016-10-27 22:48:18 -070041 use generics::parsing::generics;
David Tolnayf38cdf62016-09-23 19:07:09 -070042 use ident::parsing::ident;
David Tolnayf38cdf62016-09-23 19:07:09 -070043
David Tolnay0e837402016-12-22 17:25:55 -050044 named!(pub derive_input -> DeriveInput, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -070045 attrs: many0!(outer_attr) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070046 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -070047 which: alt!(keyword!("struct") | keyword!("enum")) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070048 id: ident >>
49 generics: generics >>
50 item: switch!(value!(which),
David Tolnay0e837402016-12-22 17:25:55 -050051 "struct" => map!(struct_body, move |(wh, body)| DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -070052 ident: id,
53 vis: vis,
54 attrs: attrs,
David Tolnay3cf52982016-10-01 17:11:37 -070055 generics: Generics {
David Tolnay28c1db62016-10-27 22:48:18 -070056 where_clause: wh,
David Tolnay3cf52982016-10-01 17:11:37 -070057 .. generics
58 },
David Tolnayf38cdf62016-09-23 19:07:09 -070059 body: Body::Struct(body),
60 })
61 |
David Tolnay0e837402016-12-22 17:25:55 -050062 "enum" => map!(enum_body, move |(wh, body)| DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -070063 ident: id,
64 vis: vis,
65 attrs: attrs,
David Tolnay3cf52982016-10-01 17:11:37 -070066 generics: Generics {
David Tolnay28c1db62016-10-27 22:48:18 -070067 where_clause: wh,
David Tolnay3cf52982016-10-01 17:11:37 -070068 .. generics
69 },
David Tolnayf38cdf62016-09-23 19:07:09 -070070 body: Body::Enum(body),
71 })
72 ) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070073 (item)
74 ));
75}
76
David Tolnayc2dfbf42016-09-23 23:52:15 -070077#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -070078mod printing {
79 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -070080 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -070081 use data::VariantData;
David Tolnayf38cdf62016-09-23 19:07:09 -070082 use quote::{Tokens, ToTokens};
83
David Tolnay0e837402016-12-22 17:25:55 -050084 impl ToTokens for DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -070085 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -070086 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -070087 attr.to_tokens(tokens);
88 }
David Tolnay47a877c2016-10-01 16:50:55 -070089 self.vis.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -070090 match self.body {
91 Body::Enum(_) => tokens.append("enum"),
92 Body::Struct(_) => tokens.append("struct"),
93 }
94 self.ident.to_tokens(tokens);
95 self.generics.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -070096 match self.body {
David Tolnayf38cdf62016-09-23 19:07:09 -070097 Body::Enum(ref variants) => {
David Tolnay28c1db62016-10-27 22:48:18 -070098 self.generics.where_clause.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -070099 tokens.append("{");
100 for variant in variants {
101 variant.to_tokens(tokens);
102 tokens.append(",");
103 }
104 tokens.append("}");
105 }
106 Body::Struct(ref variant_data) => {
David Tolnayf38cdf62016-09-23 19:07:09 -0700107 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -0700108 VariantData::Struct(_) => {
David Tolnay28c1db62016-10-27 22:48:18 -0700109 self.generics.where_clause.to_tokens(tokens);
110 variant_data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -0700111 // no semicolon
112 }
David Tolnay28c1db62016-10-27 22:48:18 -0700113 VariantData::Tuple(_) => {
114 variant_data.to_tokens(tokens);
115 self.generics.where_clause.to_tokens(tokens);
116 tokens.append(";");
117 }
118 VariantData::Unit => {
119 self.generics.where_clause.to_tokens(tokens);
120 tokens.append(";");
121 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700122 }
123 }
124 }
125 }
126 }
127}