@@ -3,6 +3,7 @@ use std::fmt;
3
3
use std:: hash:: Hash ;
4
4
use std:: slice;
5
5
use std:: vec;
6
+ use ScalarValue ;
6
7
7
8
use indexmap:: IndexMap ;
8
9
@@ -37,54 +38,55 @@ pub enum Type<'a> {
37
38
/// their position in the source file, if available.
38
39
#[ derive( Clone , PartialEq , Debug ) ]
39
40
#[ allow( missing_docs) ]
40
- pub enum InputValue {
41
+ pub enum InputValue < S : fmt :: Debug > {
41
42
Null ,
43
+ Scalar ( S ) ,
42
44
Int ( i32 ) ,
43
45
Float ( f64 ) ,
44
46
String ( String ) ,
45
47
Boolean ( bool ) ,
46
48
Enum ( String ) ,
47
49
Variable ( String ) ,
48
- List ( Vec < Spanning < InputValue > > ) ,
49
- Object ( Vec < ( Spanning < String > , Spanning < InputValue > ) > ) ,
50
+ List ( Vec < Spanning < InputValue < S > > > ) ,
51
+ Object ( Vec < ( Spanning < String > , Spanning < InputValue < S > > ) > ) ,
50
52
}
51
53
52
54
#[ derive( Clone , PartialEq , Debug ) ]
53
- pub struct VariableDefinition < ' a > {
55
+ pub struct VariableDefinition < ' a , S : fmt :: Debug > {
54
56
pub var_type : Spanning < Type < ' a > > ,
55
- pub default_value : Option < Spanning < InputValue > > ,
57
+ pub default_value : Option < Spanning < InputValue < S > > > ,
56
58
}
57
59
58
60
#[ derive( Clone , PartialEq , Debug ) ]
59
- pub struct Arguments < ' a > {
60
- pub items : Vec < ( Spanning < & ' a str > , Spanning < InputValue > ) > ,
61
+ pub struct Arguments < ' a , S : fmt :: Debug > {
62
+ pub items : Vec < ( Spanning < & ' a str > , Spanning < InputValue < S > > ) > ,
61
63
}
62
64
63
65
#[ derive( Clone , PartialEq , Debug ) ]
64
- pub struct VariableDefinitions < ' a > {
65
- pub items : Vec < ( Spanning < & ' a str > , VariableDefinition < ' a > ) > ,
66
+ pub struct VariableDefinitions < ' a , S : fmt :: Debug > {
67
+ pub items : Vec < ( Spanning < & ' a str > , VariableDefinition < ' a , S > ) > ,
66
68
}
67
69
68
70
#[ derive( Clone , PartialEq , Debug ) ]
69
- pub struct Field < ' a > {
71
+ pub struct Field < ' a , S : fmt :: Debug > {
70
72
pub alias : Option < Spanning < & ' a str > > ,
71
73
pub name : Spanning < & ' a str > ,
72
- pub arguments : Option < Spanning < Arguments < ' a > > > ,
73
- pub directives : Option < Vec < Spanning < Directive < ' a > > > > ,
74
- pub selection_set : Option < Vec < Selection < ' a > > > ,
74
+ pub arguments : Option < Spanning < Arguments < ' a , S > > > ,
75
+ pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
76
+ pub selection_set : Option < Vec < Selection < ' a , S > > > ,
75
77
}
76
78
77
79
#[ derive( Clone , PartialEq , Debug ) ]
78
- pub struct FragmentSpread < ' a > {
80
+ pub struct FragmentSpread < ' a , S : fmt :: Debug > {
79
81
pub name : Spanning < & ' a str > ,
80
- pub directives : Option < Vec < Spanning < Directive < ' a > > > > ,
82
+ pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
81
83
}
82
84
83
85
#[ derive( Clone , PartialEq , Debug ) ]
84
- pub struct InlineFragment < ' a > {
86
+ pub struct InlineFragment < ' a , S : fmt :: Debug > {
85
87
pub type_condition : Option < Spanning < & ' a str > > ,
86
- pub directives : Option < Vec < Spanning < Directive < ' a > > > > ,
87
- pub selection_set : Vec < Selection < ' a > > ,
88
+ pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
89
+ pub selection_set : Vec < Selection < ' a , S > > ,
88
90
}
89
91
90
92
/// Entry in a GraphQL selection set
@@ -104,16 +106,16 @@ pub struct InlineFragment<'a> {
104
106
/// ```
105
107
#[ derive( Clone , PartialEq , Debug ) ]
106
108
#[ allow( missing_docs) ]
107
- pub enum Selection < ' a > {
108
- Field ( Spanning < Field < ' a > > ) ,
109
- FragmentSpread ( Spanning < FragmentSpread < ' a > > ) ,
110
- InlineFragment ( Spanning < InlineFragment < ' a > > ) ,
109
+ pub enum Selection < ' a , S : fmt :: Debug > {
110
+ Field ( Spanning < Field < ' a , S > > ) ,
111
+ FragmentSpread ( Spanning < FragmentSpread < ' a , S > > ) ,
112
+ InlineFragment ( Spanning < InlineFragment < ' a , S > > ) ,
111
113
}
112
114
113
115
#[ derive( Clone , PartialEq , Debug ) ]
114
- pub struct Directive < ' a > {
116
+ pub struct Directive < ' a , S : fmt :: Debug > {
115
117
pub name : Spanning < & ' a str > ,
116
- pub arguments : Option < Spanning < Arguments < ' a > > > ,
118
+ pub arguments : Option < Spanning < Arguments < ' a , S > > > ,
117
119
}
118
120
119
121
#[ derive( Clone , PartialEq , Debug ) ]
@@ -123,45 +125,45 @@ pub enum OperationType {
123
125
}
124
126
125
127
#[ derive( Clone , PartialEq , Debug ) ]
126
- pub struct Operation < ' a > {
128
+ pub struct Operation < ' a , S : fmt :: Debug > {
127
129
pub operation_type : OperationType ,
128
130
pub name : Option < Spanning < & ' a str > > ,
129
- pub variable_definitions : Option < Spanning < VariableDefinitions < ' a > > > ,
130
- pub directives : Option < Vec < Spanning < Directive < ' a > > > > ,
131
- pub selection_set : Vec < Selection < ' a > > ,
131
+ pub variable_definitions : Option < Spanning < VariableDefinitions < ' a , S > > > ,
132
+ pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
133
+ pub selection_set : Vec < Selection < ' a , S > > ,
132
134
}
133
135
134
136
#[ derive( Clone , PartialEq , Debug ) ]
135
- pub struct Fragment < ' a > {
137
+ pub struct Fragment < ' a , S : fmt :: Debug > {
136
138
pub name : Spanning < & ' a str > ,
137
139
pub type_condition : Spanning < & ' a str > ,
138
- pub directives : Option < Vec < Spanning < Directive < ' a > > > > ,
139
- pub selection_set : Vec < Selection < ' a > > ,
140
+ pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
141
+ pub selection_set : Vec < Selection < ' a , S > > ,
140
142
}
141
143
142
144
#[ derive( Clone , PartialEq , Debug ) ]
143
- pub enum Definition < ' a > {
144
- Operation ( Spanning < Operation < ' a > > ) ,
145
- Fragment ( Spanning < Fragment < ' a > > ) ,
145
+ pub enum Definition < ' a , S : fmt :: Debug > {
146
+ Operation ( Spanning < Operation < ' a , S > > ) ,
147
+ Fragment ( Spanning < Fragment < ' a , S > > ) ,
146
148
}
147
149
148
- pub type Document < ' a > = Vec < Definition < ' a > > ;
150
+ pub type Document < ' a , S > = Vec < Definition < ' a , S > > ;
149
151
150
152
/// Parse an unstructured input value into a Rust data type.
151
153
///
152
154
/// The conversion _can_ fail, and must in that case return None. Implemented
153
155
/// automatically by the convenience macro `graphql_scalar!` or by deriving GraphQLEnum.
154
156
///
155
157
/// Must be implemented manually when manually exposing new enums or scalars.
156
- pub trait FromInputValue : Sized {
158
+ pub trait FromInputValue < S : fmt :: Debug > : Sized {
157
159
/// Performs the conversion.
158
- fn from_input_value ( v : & InputValue ) -> Option < Self > ;
160
+ fn from_input_value ( v : & InputValue < S > ) -> Option < Self > ;
159
161
}
160
162
161
163
/// Losslessly clones a Rust data type into an InputValue.
162
- pub trait ToInputValue : Sized {
164
+ pub trait ToInputValue < S : fmt :: Debug > : Sized {
163
165
/// Performs the conversion.
164
- fn to_input_value ( & self ) -> InputValue ;
166
+ fn to_input_value ( & self ) -> InputValue < S > ;
165
167
}
166
168
167
169
impl < ' a > Type < ' a > {
@@ -205,39 +207,49 @@ impl<'a> fmt::Display for Type<'a> {
205
207
}
206
208
}
207
209
208
- impl InputValue {
210
+ impl < S > InputValue < S >
211
+ where
212
+ S : ScalarValue ,
213
+ {
209
214
/// Construct a null value.
210
- pub fn null ( ) -> InputValue {
215
+ pub fn null ( ) -> Self {
211
216
InputValue :: Null
212
217
}
213
218
214
219
/// Construct an integer value.
215
- pub fn int ( i : i32 ) -> InputValue {
216
- InputValue :: Int ( i)
220
+ pub fn int ( i : i32 ) -> Self {
221
+ Self :: scalar ( i)
217
222
}
218
223
219
224
/// Construct a floating point value.
220
- pub fn float ( f : f64 ) -> InputValue {
221
- InputValue :: Float ( f)
225
+ pub fn float ( f : f64 ) -> Self {
226
+ Self :: scalar ( f)
222
227
}
223
228
224
229
/// Construct a boolean value.
225
- pub fn boolean ( b : bool ) -> InputValue {
226
- InputValue :: Boolean ( b)
230
+ pub fn boolean ( b : bool ) -> Self {
231
+ Self :: scalar ( b)
227
232
}
228
233
229
234
/// Construct a string value.
230
- pub fn string < T : AsRef < str > > ( s : T ) -> InputValue {
235
+ pub fn string < T : AsRef < str > > ( s : T ) -> Self {
231
236
InputValue :: String ( s. as_ref ( ) . to_owned ( ) )
232
237
}
233
238
239
+ pub fn scalar < T > ( v : T ) -> Self
240
+ where
241
+ T : Into < S > ,
242
+ {
243
+ InputValue :: Scalar ( v. into ( ) )
244
+ }
245
+
234
246
/// Construct an enum value.
235
- pub fn enum_value < T : AsRef < str > > ( s : T ) -> InputValue {
247
+ pub fn enum_value < T : AsRef < str > > ( s : T ) -> Self {
236
248
InputValue :: Enum ( s. as_ref ( ) . to_owned ( ) )
237
249
}
238
250
239
251
/// Construct a variable value.
240
- pub fn variable < T : AsRef < str > > ( v : T ) -> InputValue {
252
+ pub fn variable < T : AsRef < str > > ( v : T ) -> Self {
241
253
InputValue :: Variable ( v. as_ref ( ) . to_owned ( ) )
242
254
}
243
255
@@ -246,20 +258,20 @@ impl InputValue {
246
258
/// Convenience function to make each `InputValue` in the input vector
247
259
/// not contain any location information. Can be used from `ToInputValue`
248
260
/// implementations, where no source code position information is available.
249
- pub fn list ( l : Vec < InputValue > ) -> InputValue {
261
+ pub fn list ( l : Vec < Self > ) -> Self {
250
262
InputValue :: List ( l. into_iter ( ) . map ( Spanning :: unlocated) . collect ( ) )
251
263
}
252
264
253
265
/// Construct a located list.
254
- pub fn parsed_list ( l : Vec < Spanning < InputValue > > ) -> InputValue {
266
+ pub fn parsed_list ( l : Vec < Spanning < Self > > ) -> Self {
255
267
InputValue :: List ( l)
256
268
}
257
269
258
270
/// Construct an unlocated object.
259
271
///
260
272
/// Similar to `InputValue::list`, it makes each key and value in the given
261
273
/// hash map not contain any location information.
262
- pub fn object < K > ( o : IndexMap < K , InputValue > ) -> InputValue
274
+ pub fn object < K > ( o : IndexMap < K , Self > ) -> Self
263
275
where
264
276
K : AsRef < str > + Eq + Hash ,
265
277
{
@@ -276,12 +288,12 @@ impl InputValue {
276
288
}
277
289
278
290
/// Construct a located object.
279
- pub fn parsed_object ( o : Vec < ( Spanning < String > , Spanning < InputValue > ) > ) -> InputValue {
291
+ pub fn parsed_object ( o : Vec < ( Spanning < String > , Spanning < Self > ) > ) -> Self {
280
292
InputValue :: Object ( o)
281
293
}
282
294
283
295
/// Resolve all variables to their values.
284
- pub fn into_const ( self , vars : & Variables ) -> InputValue {
296
+ pub fn into_const ( self , vars : & Variables < S > ) -> Self {
285
297
match self {
286
298
InputValue :: Variable ( v) => vars. get ( & v) . map_or_else ( InputValue :: null, Clone :: clone) ,
287
299
InputValue :: List ( l) => InputValue :: List (
@@ -301,9 +313,9 @@ impl InputValue {
301
313
/// Shorthand form of invoking `FromInputValue::from()`.
302
314
pub fn convert < T > ( & self ) -> Option < T >
303
315
where
304
- T : FromInputValue ,
316
+ T : FromInputValue < S > ,
305
317
{
306
- <T as FromInputValue >:: from_input_value ( self )
318
+ <T as FromInputValue < S > >:: from_input_value ( self )
307
319
}
308
320
309
321
/// Does the value represent null?
@@ -358,7 +370,7 @@ impl InputValue {
358
370
///
359
371
/// This constructs a new IndexMap that contain references to the keys
360
372
/// and values in `self`.
361
- pub fn to_object_value ( & self ) -> Option < IndexMap < & str , & InputValue > > {
373
+ pub fn to_object_value ( & self ) -> Option < IndexMap < & str , & Self > > {
362
374
match * self {
363
375
InputValue :: Object ( ref o) => Some (
364
376
o. iter ( )
@@ -373,7 +385,7 @@ impl InputValue {
373
385
///
374
386
/// This constructs a new vector that contain references to the values
375
387
/// in `self`.
376
- pub fn to_list_value ( & self ) -> Option < Vec < & InputValue > > {
388
+ pub fn to_list_value ( & self ) -> Option < Vec < & Self > > {
377
389
match * self {
378
390
InputValue :: List ( ref l) => Some ( l. iter ( ) . map ( |s| & s. item ) . collect ( ) ) ,
379
391
_ => None ,
@@ -384,18 +396,20 @@ impl InputValue {
384
396
pub fn referenced_variables ( & self ) -> Vec < & str > {
385
397
match * self {
386
398
InputValue :: Variable ( ref name) => vec ! [ name] ,
387
- InputValue :: List ( ref l) => l. iter ( )
399
+ InputValue :: List ( ref l) => l
400
+ . iter ( )
388
401
. flat_map ( |v| v. item . referenced_variables ( ) )
389
402
. collect ( ) ,
390
- InputValue :: Object ( ref obj) => obj. iter ( )
403
+ InputValue :: Object ( ref obj) => obj
404
+ . iter ( )
391
405
. flat_map ( |& ( _, ref v) | v. item . referenced_variables ( ) )
392
406
. collect ( ) ,
393
407
_ => vec ! [ ] ,
394
408
}
395
409
}
396
410
397
411
/// Compare equality with another `InputValue` ignoring any source position information.
398
- pub fn unlocated_eq ( & self , other : & InputValue ) -> bool {
412
+ pub fn unlocated_eq ( & self , other : & Self ) -> bool {
399
413
use InputValue :: * ;
400
414
401
415
match ( self , other) {
@@ -406,7 +420,8 @@ impl InputValue {
406
420
| ( & Enum ( ref s1) , & Enum ( ref s2) )
407
421
| ( & Variable ( ref s1) , & Variable ( ref s2) ) => s1 == s2,
408
422
( & Boolean ( b1) , & Boolean ( b2) ) => b1 == b2,
409
- ( & List ( ref l1) , & List ( ref l2) ) => l1. iter ( )
423
+ ( & List ( ref l1) , & List ( ref l2) ) => l1
424
+ . iter ( )
410
425
. zip ( l2. iter ( ) )
411
426
. all ( |( v1, v2) | v1. item . unlocated_eq ( & v2. item ) ) ,
412
427
( & Object ( ref o1) , & Object ( ref o2) ) => {
@@ -421,7 +436,7 @@ impl InputValue {
421
436
}
422
437
}
423
438
424
- impl fmt:: Display for InputValue {
439
+ impl < S : fmt:: Debug > fmt :: Display for InputValue < S > {
425
440
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
426
441
match * self {
427
442
InputValue :: Null => write ! ( f, "null" ) ,
@@ -460,24 +475,24 @@ impl fmt::Display for InputValue {
460
475
}
461
476
}
462
477
463
- impl < ' a > Arguments < ' a > {
464
- pub fn into_iter ( self ) -> vec:: IntoIter < ( Spanning < & ' a str > , Spanning < InputValue > ) > {
478
+ impl < ' a , S : fmt :: Debug > Arguments < ' a , S > {
479
+ pub fn into_iter ( self ) -> vec:: IntoIter < ( Spanning < & ' a str > , Spanning < InputValue < S > > ) > {
465
480
self . items . into_iter ( )
466
481
}
467
482
468
- pub fn iter ( & self ) -> slice:: Iter < ( Spanning < & ' a str > , Spanning < InputValue > ) > {
483
+ pub fn iter ( & self ) -> slice:: Iter < ( Spanning < & ' a str > , Spanning < InputValue < S > > ) > {
469
484
self . items . iter ( )
470
485
}
471
486
472
- pub fn iter_mut ( & mut self ) -> slice:: IterMut < ( Spanning < & ' a str > , Spanning < InputValue > ) > {
487
+ pub fn iter_mut ( & mut self ) -> slice:: IterMut < ( Spanning < & ' a str > , Spanning < InputValue < S > > ) > {
473
488
self . items . iter_mut ( )
474
489
}
475
490
476
491
pub fn len ( & self ) -> usize {
477
492
self . items . len ( )
478
493
}
479
494
480
- pub fn get ( & self , key : & str ) -> Option < & Spanning < InputValue > > {
495
+ pub fn get ( & self , key : & str ) -> Option < & Spanning < InputValue < S > > > {
481
496
self . items
482
497
. iter ( )
483
498
. filter ( |& & ( ref k, _) | k. item == key)
@@ -486,8 +501,8 @@ impl<'a> Arguments<'a> {
486
501
}
487
502
}
488
503
489
- impl < ' a > VariableDefinitions < ' a > {
490
- pub fn iter ( & self ) -> slice:: Iter < ( Spanning < & ' a str > , VariableDefinition ) > {
504
+ impl < ' a , S : fmt :: Debug > VariableDefinitions < ' a , S > {
505
+ pub fn iter ( & self ) -> slice:: Iter < ( Spanning < & ' a str > , VariableDefinition < S > ) > {
491
506
self . items . iter ( )
492
507
}
493
508
}
0 commit comments