1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
Expose simple enums

GraphQL enums are similar to enums classes C++ - more like grouped constants
with type safety than what Rust enums offer. This macro can be used to export
non-data carrying Rust enums to GraphQL:

```rust
# #[macro_use] extern crate juniper;
enum Color {
    Red,
    Orange,
    Green,
    Blue,
    Black,
}

graphql_enum!(Color {
    Color::Red => "RED" as "The color red",
    Color::Orange => "ORANGE",
    Color::Green => "GREEN",
    Color::Blue => "BLUE",
    Color::Black => "BLACK" deprecated "Superseded by ORANGE",
});

# fn main() { }
```

The macro expands to a `match` statement which will result in a compilation
error if not all enum variants are covered. It also creates an implementation
for `FromInputValue` and `ToInputValue`, making it usable in arguments and
default values.

If you want to expose the enum under a different name than the Rust type,
you can write `graphql_enum!(Color as "MyColor" { ...`.

*/
#[macro_export]
macro_rules! graphql_enum {
    ( @as_expr, $e:expr) => { $e };
    ( @as_pattern, $p:pat) => { $p };
    ( @as_path, $p:path) => { $p };

    // Calls $val.$func($arg) if $arg is not None
    ( @maybe_apply, None, $func:ident, $val:expr ) => { $val };
    ( @maybe_apply, $arg:tt, $func:ident, $val:expr ) => { $val.$func($arg) };

    // Each of the @parse match arms accumulates data up to a call to @generate.
    //
    // ( $name, $outname, $descr ): the name of the Rust enum, the name of the
    // GraphQL enum (as a string), and the description of the enum (as a string or None)
    //
    // [ ( $eval, $ename, $edescr, $edepr ) , ] the value of the Rust enum,
    // the value of the GraphQL enum (as a string), the description of the enum
    // value (as a string or None), and the deprecation reason of the enum value
    // (as a string or None).
    (
        @generate,
        ( $name:path, $outname:tt, $descr:tt ),
        [ $( ( $eval:tt, $ename:tt, $edescr:tt, $edepr:tt ) , )* ]
    ) => {
        impl<CtxT> $crate::GraphQLType<CtxT> for $name {
            fn name() -> Option<&'static str> {
                Some(graphql_enum!(@as_expr, $outname))
            }

            fn meta(registry: &mut $crate::Registry<CtxT>) -> $crate::meta::MetaType {
                graphql_enum!(
                    @maybe_apply, $descr, description,
                    registry.build_enum_type::<$name>()(&[
                        $(
                            graphql_enum!(
                                @maybe_apply,
                                $edepr, deprecated,
                                graphql_enum!(
                                    @maybe_apply,
                                    $edescr, description,
                                    $crate::meta::EnumValue::new(graphql_enum!(@as_expr, $ename))))
                        ),*
                    ]))
                    .into_meta()
            }

            fn resolve(&self, _: Option<Vec<$crate::Selection>>, _: &mut $crate::Executor<CtxT>) -> $crate::Value {
                match self {
                    $(
                        &graphql_enum!(@as_pattern, $eval) =>
                            $crate::Value::string(graphql_enum!(@as_expr, $ename)) ),*
                }
            }
        }

        impl $crate::FromInputValue for $name {
            fn from(v: &$crate::InputValue) -> Option<$name> {
                match v.as_enum_value() {
                    $(
                        Some(graphql_enum!(@as_pattern, $ename))
                            => Some(graphql_enum!(@as_expr, $eval)), )*
                    _ => None,
                }
            }
        }

        impl $crate::ToInputValue for $name {
            fn to(&self) -> $crate::InputValue {
                match self {
                    $(
                        &graphql_enum!(@as_pattern, $eval) =>
                            $crate::InputValue::string(graphql_enum!(@as_expr, $ename)) ),*
                }
            }
        }

        impl $crate::IntoFieldResult<$name> for $name {
            fn into(self) -> $crate::FieldResult<$name> {
                Ok(self)
            }
        }
    };

    // No more items to parse
    ( @parse, $meta:tt, $acc:tt, ) => {
        graphql_enum!( @generate, $meta, $acc );
    };

    // Remove extraneous commas
    ( @parse, $meta:tt, $acc:tt, , $($rest:tt)* ) => {
        graphql_enum!( @parse, $meta, $acc, $($rest)* );
    };

    // description: <description>
    (
        @parse,
        ( $name:tt, $outname:tt, $_ignore:tt ),
        $acc:tt,
        description: $descr:tt $($items:tt)*
    ) => {
        graphql_enum!( @parse, ( $name, $outname, $descr ), $acc, $($items)* );
    };

    // RustEnumValue => "GraphQL enum value" deprecated <reason>
    (
        @parse,
        $meta:tt,
        [ $($acc:tt ,)* ],
        $eval:path => $ename:tt deprecated $depr:tt $($rest:tt)*
    ) => {
        graphql_enum!( @parse, $meta, [ $($acc ,)* ( $eval, $ename, None, $depr ), ], $($rest)* );
    };

    // RustEnumValue => "GraphQL enum value" as <description> deprecated <reason>
    (
        @parse,
        $meta:tt,
        [ $($acc:tt ,)* ],
        $eval:path => $ename:tt as $descr:tt deprecated $depr:tt $($rest:tt)*
    ) => {
        graphql_enum!( @parse, $meta, [ $($acc ,)* ( $eval, $ename, $descr, $depr ), ], $($rest)* );
    };

    // RustEnumValue => "GraphQL enum value" as <description>
    (
        @parse,
        $meta:tt,
        [ $($acc:tt ,)* ],
        $eval:path => $ename:tt as $descr:tt $($rest:tt)*
    ) => {
        graphql_enum!( @parse, $meta, [ $($acc ,)* ( $eval, $ename, $descr, None ), ], $($rest)* );
    };

    // RustEnumValue => "GraphQL enum value"
    (
        @parse,
        $meta:tt,
        [ $($acc:tt ,)* ],
        $eval:path => $ename:tt $($rest:tt)*
    ) => {
        graphql_enum!( @parse, $meta, [ $($acc ,)* ( $eval , $ename , None , None ), ], $($rest)* );
    };

    // Entry point:
    // RustEnumName as "GraphQLEnumName" { ... }
    (
        $name:path as $outname:tt { $($items:tt)* }
    ) => {
        graphql_enum!( @parse, ( $name, $outname, None ), [ ], $($items)* );
    };

    // Entry point
    // RustEnumName { ... }
    (
        $name:path { $($items:tt)* }
    ) => {
        graphql_enum!( @parse, ( $name, (stringify!($name)), None ), [ ], $($items)* );
    };
}