Skip to content

Commit 1af9504

Browse files
committed
Add support for declaring input objects as public structs
Fixes #5.
1 parent fce45ec commit 1af9504

File tree

3 files changed

+88
-14
lines changed

3 files changed

+88
-14
lines changed

src/macros/input_object.rs

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ macro_rules! graphql_input_object {
6464
// Generate the struct declaration, including (Rust) meta attributes
6565
(
6666
@generate_struct_fields,
67-
( $($meta:tt)* ), $name:tt,
67+
( $($meta:tt)* ), ( $($pubmod:tt)* ), $name:tt,
6868
( $($field_name:ident : $field_type:ty $(as $descr:tt)* $(,)* ),* )
6969
) => {
70-
$($meta)* struct $name {
70+
$($meta)* $($pubmod)* struct $name {
7171
$( $field_name: $field_type, )*
7272
}
7373
};
@@ -93,12 +93,26 @@ macro_rules! graphql_input_object {
9393
// struct $name { ... }
9494
(
9595
@parse,
96-
( $_ignore1:tt, $_ignore2:tt, $_ignore3:tt, $_ignore4:tt, $descr:tt ),
96+
( $_ignore1:tt, $_ignore2:tt, $_ignore3:tt, $_ignore4:tt, $_ignore5:tt, $descr:tt ),
9797
$(#[$meta:meta])* struct $name:ident { $($fields:tt)* } $($rest:tt)*
9898
) => {
9999
graphql_input_object!(
100100
@parse,
101-
( ( $(#[$meta])* ), $name, (stringify!($name)), ($($fields)*), $descr ),
101+
( ( $(#[$meta])* ), ( ), $name, (stringify!($name)), ($($fields)*), $descr ),
102+
$($rest)*
103+
);
104+
};
105+
106+
// #[...] pub struct $name { ... }
107+
// pub struct $name { ... }
108+
(
109+
@parse,
110+
( $_ignore1:tt, $_ignore2:tt, $_ignore3:tt, $_ignore4:tt, $_ignore5:tt, $descr:tt ),
111+
$(#[$meta:meta])* pub struct $name:ident { $($fields:tt)* } $($rest:tt)*
112+
) => {
113+
graphql_input_object!(
114+
@parse,
115+
( ( $(#[$meta])* ), ( pub ), $name, (stringify!($name)), ($($fields)*), $descr ),
102116
$($rest)*
103117
);
104118
};
@@ -107,35 +121,49 @@ macro_rules! graphql_input_object {
107121
// struct $name as "GraphQLName" { ... }
108122
(
109123
@parse,
110-
( $_ignore1:tt, $_ignore2:tt, $_ignore3:tt, $_ignore4:tt, $descr:tt ),
124+
( $_ignore1:tt, $_ignore2:tt, $_ignore3:tt, $_ignore4:tt, $_ignore5:tt, $descr:tt ),
111125
$(#[$meta:meta])* struct $name:ident as $outname:tt { $($fields:tt)* } $($rest:tt)*
112126
) => {
113127
graphql_input_object!(
114128
@parse,
115-
( ( $($meta)* ), $name, $outname, ($($fields)*), $descr ),
129+
( ( $($meta)* ), ( ), $name, $outname, ($($fields)*), $descr ),
130+
$($rest)*
131+
);
132+
};
133+
134+
// #[...] pub struct $name as "GraphQLName" { ... }
135+
// pub struct $name as "GraphQLName" { ... }
136+
(
137+
@parse,
138+
( $_ignore1:tt, $_ignore2:tt, $_ignore3:tt, $_ignore4:tt, $_ignore5:tt, $descr:tt ),
139+
$(#[$meta:meta])* pub struct $name:ident as $outname:tt { $($fields:tt)* } $($rest:tt)*
140+
) => {
141+
graphql_input_object!(
142+
@parse,
143+
( ( $($meta)* ), ( pub ), $name, $outname, ($($fields)*), $descr ),
116144
$($rest)*
117145
);
118146
};
119147

120148
// description: <description>
121149
(
122150
@parse,
123-
( $meta:tt, $name:tt, $outname:tt, $fields:tt, $_ignore:tt ),
151+
( $meta:tt, $pubmod:tt, $name:tt, $outname:tt, $fields:tt, $_ignore:tt ),
124152
description: $descr:tt $($rest:tt)*
125153
) => {
126154
graphql_input_object!(
127155
@parse,
128-
( $meta, $name, $outname, $fields, $descr ),
156+
( $meta, $pubmod, $name, $outname, $fields, $descr ),
129157
$($rest)*
130158
);
131159
};
132160

133161
// No more data to parse, generate the struct and impls
134162
(
135163
@parse,
136-
( $meta:tt, $name:tt, $outname:tt, $fields:tt, $descr:tt ),
164+
( $meta:tt, $pubmod:tt, $name:tt, $outname:tt, $fields:tt, $descr:tt ),
137165
) => {
138-
graphql_input_object!(@generate_struct_fields, $meta, $name, $fields);
166+
graphql_input_object!(@generate_struct_fields, $meta, $pubmod, $name, $fields);
139167

140168
impl $crate::FromInputValue for $name {
141169
fn from(value: &$crate::InputValue) -> Option<$name> {
@@ -164,20 +192,29 @@ macro_rules! graphql_input_object {
164192
}
165193
};
166194

167-
// Entry point: parse calls starting with the struct declaration
195+
// Entry point: parse calls starting with a struct declaration
168196
( $(#[$meta:meta])* struct $($items:tt)* ) => {
169197
graphql_input_object!(
170198
@parse,
171-
( ( ), None, None, None, None ),
199+
( ( ), ( ), None, None, None, None ),
172200
$(#[$meta])* struct $($items)*
173201
);
174202
};
175203

204+
// Entry point: parse calls starting with a public struct declaration
205+
( $(#[$meta:meta])* pub struct $($items:tt)* ) => {
206+
graphql_input_object!(
207+
@parse,
208+
( ( ), ( ), None, None, None, None ),
209+
$(#[$meta])* pub struct $($items)*
210+
);
211+
};
212+
176213
// Entry point: parse calls starting with the description
177214
( description: $($items:tt)* ) => {
178215
graphql_input_object!(
179216
@parse,
180-
( ( ), None, None, None, None ),
217+
( ( ), ( ), None, None, None, None ),
181218
description: $($items)*
182219
);
183220
};

src/macros/tests/input_object.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,34 @@ graphql_input_object!(
4242
}
4343
);
4444

45+
graphql_input_object!(
46+
pub struct Public {
47+
field_one: String,
48+
}
49+
);
50+
51+
graphql_input_object!(
52+
description: "Description for the input object"
53+
54+
pub struct PublicWithDescription {
55+
field_one: String,
56+
}
57+
);
58+
59+
graphql_input_object!(
60+
description: "Description for the input object"
61+
62+
pub struct NamedPublicWithDescription as "APublicNamedInputObjectWithDescription" {
63+
field_one: String,
64+
}
65+
);
66+
67+
graphql_input_object!(
68+
pub struct NamedPublic as "APublicNamedInputObject" {
69+
field_one: String,
70+
}
71+
);
72+
4573
graphql_input_object!(
4674
struct FieldDescription {
4775
field_one: String as "The first field",
@@ -56,7 +84,11 @@ graphql_object!(Root: () |&self| {
5684
a3: Derive,
5785
a4: Named,
5886
a5: Description,
59-
a6: FieldDescription
87+
a6: FieldDescription,
88+
a7: Public,
89+
a8: PublicWithDescription,
90+
a9: NamedPublicWithDescription,
91+
a10: NamedPublic,
6092
) -> i64 {
6193
0
6294
}

src/macros/tests/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ mod field;
66
mod object;
77
mod interface;
88
mod union;
9+
10+
11+
// This asserts that the input objects defined public actually became public
12+
#[allow(unused_imports)]
13+
use self::input_object::{NamedPublic, NamedPublicWithDescription};

0 commit comments

Comments
 (0)