Skip to content

Commit 21ed215

Browse files
committed
WIP
1 parent ec59766 commit 21ed215

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2911
-1855
lines changed

juniper/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fnv = "1.0.3"
3838
indexmap = { version = "1.0.0", features = ["serde-1"] }
3939
serde = { version = "1.0.8" }
4040
serde_derive = { version = "1.0.2" }
41+
erased-serde = "0.3"
4142

4243
chrono = { version = "0.4.0", optional = true }
4344
serde_json = { version="1.0.2", optional = true }

juniper/src/ast.rs

Lines changed: 86 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fmt;
33
use std::hash::Hash;
44
use std::slice;
55
use std::vec;
6+
use ScalarValue;
67

78
use indexmap::IndexMap;
89

@@ -37,54 +38,55 @@ pub enum Type<'a> {
3738
/// their position in the source file, if available.
3839
#[derive(Clone, PartialEq, Debug)]
3940
#[allow(missing_docs)]
40-
pub enum InputValue {
41+
pub enum InputValue<S: fmt::Debug> {
4142
Null,
43+
Scalar(S),
4244
Int(i32),
4345
Float(f64),
4446
String(String),
4547
Boolean(bool),
4648
Enum(String),
4749
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>>)>),
5052
}
5153

5254
#[derive(Clone, PartialEq, Debug)]
53-
pub struct VariableDefinition<'a> {
55+
pub struct VariableDefinition<'a, S: fmt::Debug> {
5456
pub var_type: Spanning<Type<'a>>,
55-
pub default_value: Option<Spanning<InputValue>>,
57+
pub default_value: Option<Spanning<InputValue<S>>>,
5658
}
5759

5860
#[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>>)>,
6163
}
6264

6365
#[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>)>,
6668
}
6769

6870
#[derive(Clone, PartialEq, Debug)]
69-
pub struct Field<'a> {
71+
pub struct Field<'a, S: fmt::Debug> {
7072
pub alias: Option<Spanning<&'a str>>,
7173
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>>>,
7577
}
7678

7779
#[derive(Clone, PartialEq, Debug)]
78-
pub struct FragmentSpread<'a> {
80+
pub struct FragmentSpread<'a, S: fmt::Debug> {
7981
pub name: Spanning<&'a str>,
80-
pub directives: Option<Vec<Spanning<Directive<'a>>>>,
82+
pub directives: Option<Vec<Spanning<Directive<'a, S>>>>,
8183
}
8284

8385
#[derive(Clone, PartialEq, Debug)]
84-
pub struct InlineFragment<'a> {
86+
pub struct InlineFragment<'a, S: fmt::Debug> {
8587
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>>,
8890
}
8991

9092
/// Entry in a GraphQL selection set
@@ -104,16 +106,16 @@ pub struct InlineFragment<'a> {
104106
/// ```
105107
#[derive(Clone, PartialEq, Debug)]
106108
#[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>>),
111113
}
112114

113115
#[derive(Clone, PartialEq, Debug)]
114-
pub struct Directive<'a> {
116+
pub struct Directive<'a, S: fmt::Debug> {
115117
pub name: Spanning<&'a str>,
116-
pub arguments: Option<Spanning<Arguments<'a>>>,
118+
pub arguments: Option<Spanning<Arguments<'a, S>>>,
117119
}
118120

119121
#[derive(Clone, PartialEq, Debug)]
@@ -123,45 +125,45 @@ pub enum OperationType {
123125
}
124126

125127
#[derive(Clone, PartialEq, Debug)]
126-
pub struct Operation<'a> {
128+
pub struct Operation<'a, S: fmt::Debug> {
127129
pub operation_type: OperationType,
128130
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>>,
132134
}
133135

134136
#[derive(Clone, PartialEq, Debug)]
135-
pub struct Fragment<'a> {
137+
pub struct Fragment<'a, S: fmt::Debug> {
136138
pub name: Spanning<&'a str>,
137139
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>>,
140142
}
141143

142144
#[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>>),
146148
}
147149

148-
pub type Document<'a> = Vec<Definition<'a>>;
150+
pub type Document<'a, S> = Vec<Definition<'a, S>>;
149151

150152
/// Parse an unstructured input value into a Rust data type.
151153
///
152154
/// The conversion _can_ fail, and must in that case return None. Implemented
153155
/// automatically by the convenience macro `graphql_scalar!` or by deriving GraphQLEnum.
154156
///
155157
/// Must be implemented manually when manually exposing new enums or scalars.
156-
pub trait FromInputValue: Sized {
158+
pub trait FromInputValue<S: fmt::Debug>: Sized {
157159
/// Performs the conversion.
158-
fn from_input_value(v: &InputValue) -> Option<Self>;
160+
fn from_input_value(v: &InputValue<S>) -> Option<Self>;
159161
}
160162

161163
/// Losslessly clones a Rust data type into an InputValue.
162-
pub trait ToInputValue: Sized {
164+
pub trait ToInputValue<S: fmt::Debug>: Sized {
163165
/// Performs the conversion.
164-
fn to_input_value(&self) -> InputValue;
166+
fn to_input_value(&self) -> InputValue<S>;
165167
}
166168

167169
impl<'a> Type<'a> {
@@ -205,39 +207,49 @@ impl<'a> fmt::Display for Type<'a> {
205207
}
206208
}
207209

208-
impl InputValue {
210+
impl<S> InputValue<S>
211+
where
212+
S: ScalarValue,
213+
{
209214
/// Construct a null value.
210-
pub fn null() -> InputValue {
215+
pub fn null() -> Self {
211216
InputValue::Null
212217
}
213218

214219
/// 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)
217222
}
218223

219224
/// 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)
222227
}
223228

224229
/// 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)
227232
}
228233

229234
/// Construct a string value.
230-
pub fn string<T: AsRef<str>>(s: T) -> InputValue {
235+
pub fn string<T: AsRef<str>>(s: T) -> Self {
231236
InputValue::String(s.as_ref().to_owned())
232237
}
233238

239+
pub fn scalar<T>(v: T) -> Self
240+
where
241+
T: Into<S>,
242+
{
243+
InputValue::Scalar(v.into())
244+
}
245+
234246
/// 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 {
236248
InputValue::Enum(s.as_ref().to_owned())
237249
}
238250

239251
/// Construct a variable value.
240-
pub fn variable<T: AsRef<str>>(v: T) -> InputValue {
252+
pub fn variable<T: AsRef<str>>(v: T) -> Self {
241253
InputValue::Variable(v.as_ref().to_owned())
242254
}
243255

@@ -246,20 +258,20 @@ impl InputValue {
246258
/// Convenience function to make each `InputValue` in the input vector
247259
/// not contain any location information. Can be used from `ToInputValue`
248260
/// 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 {
250262
InputValue::List(l.into_iter().map(Spanning::unlocated).collect())
251263
}
252264

253265
/// Construct a located list.
254-
pub fn parsed_list(l: Vec<Spanning<InputValue>>) -> InputValue {
266+
pub fn parsed_list(l: Vec<Spanning<Self>>) -> Self {
255267
InputValue::List(l)
256268
}
257269

258270
/// Construct an unlocated object.
259271
///
260272
/// Similar to `InputValue::list`, it makes each key and value in the given
261273
/// 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
263275
where
264276
K: AsRef<str> + Eq + Hash,
265277
{
@@ -276,12 +288,12 @@ impl InputValue {
276288
}
277289

278290
/// 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 {
280292
InputValue::Object(o)
281293
}
282294

283295
/// 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 {
285297
match self {
286298
InputValue::Variable(v) => vars.get(&v).map_or_else(InputValue::null, Clone::clone),
287299
InputValue::List(l) => InputValue::List(
@@ -301,9 +313,9 @@ impl InputValue {
301313
/// Shorthand form of invoking `FromInputValue::from()`.
302314
pub fn convert<T>(&self) -> Option<T>
303315
where
304-
T: FromInputValue,
316+
T: FromInputValue<S>,
305317
{
306-
<T as FromInputValue>::from_input_value(self)
318+
<T as FromInputValue<S>>::from_input_value(self)
307319
}
308320

309321
/// Does the value represent null?
@@ -358,7 +370,7 @@ impl InputValue {
358370
///
359371
/// This constructs a new IndexMap that contain references to the keys
360372
/// 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>> {
362374
match *self {
363375
InputValue::Object(ref o) => Some(
364376
o.iter()
@@ -373,7 +385,7 @@ impl InputValue {
373385
///
374386
/// This constructs a new vector that contain references to the values
375387
/// in `self`.
376-
pub fn to_list_value(&self) -> Option<Vec<&InputValue>> {
388+
pub fn to_list_value(&self) -> Option<Vec<&Self>> {
377389
match *self {
378390
InputValue::List(ref l) => Some(l.iter().map(|s| &s.item).collect()),
379391
_ => None,
@@ -384,18 +396,20 @@ impl InputValue {
384396
pub fn referenced_variables(&self) -> Vec<&str> {
385397
match *self {
386398
InputValue::Variable(ref name) => vec![name],
387-
InputValue::List(ref l) => l.iter()
399+
InputValue::List(ref l) => l
400+
.iter()
388401
.flat_map(|v| v.item.referenced_variables())
389402
.collect(),
390-
InputValue::Object(ref obj) => obj.iter()
403+
InputValue::Object(ref obj) => obj
404+
.iter()
391405
.flat_map(|&(_, ref v)| v.item.referenced_variables())
392406
.collect(),
393407
_ => vec![],
394408
}
395409
}
396410

397411
/// 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 {
399413
use InputValue::*;
400414

401415
match (self, other) {
@@ -406,7 +420,8 @@ impl InputValue {
406420
| (&Enum(ref s1), &Enum(ref s2))
407421
| (&Variable(ref s1), &Variable(ref s2)) => s1 == s2,
408422
(&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()
410425
.zip(l2.iter())
411426
.all(|(v1, v2)| v1.item.unlocated_eq(&v2.item)),
412427
(&Object(ref o1), &Object(ref o2)) => {
@@ -421,7 +436,7 @@ impl InputValue {
421436
}
422437
}
423438

424-
impl fmt::Display for InputValue {
439+
impl<S: fmt::Debug> fmt::Display for InputValue<S> {
425440
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
426441
match *self {
427442
InputValue::Null => write!(f, "null"),
@@ -460,24 +475,24 @@ impl fmt::Display for InputValue {
460475
}
461476
}
462477

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>>)> {
465480
self.items.into_iter()
466481
}
467482

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>>)> {
469484
self.items.iter()
470485
}
471486

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>>)> {
473488
self.items.iter_mut()
474489
}
475490

476491
pub fn len(&self) -> usize {
477492
self.items.len()
478493
}
479494

480-
pub fn get(&self, key: &str) -> Option<&Spanning<InputValue>> {
495+
pub fn get(&self, key: &str) -> Option<&Spanning<InputValue<S>>> {
481496
self.items
482497
.iter()
483498
.filter(|&&(ref k, _)| k.item == key)
@@ -486,8 +501,8 @@ impl<'a> Arguments<'a> {
486501
}
487502
}
488503

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>)> {
491506
self.items.iter()
492507
}
493508
}

0 commit comments

Comments
 (0)