1
1
use std:: { borrow:: Cow , fmt, hash:: Hash , slice, vec} ;
2
2
3
+ use arcstr:: ArcStr ;
4
+
3
5
use indexmap:: IndexMap ;
4
6
5
7
use crate :: {
@@ -8,24 +10,70 @@ use crate::{
8
10
value:: { DefaultScalarValue , ScalarValue } ,
9
11
} ;
10
12
11
- /// A type literal in the syntax tree
13
+ /// Type literal in a syntax tree.
12
14
///
13
- /// This enum carries no semantic information and might refer to types that do
14
- /// not exist.
15
- # [ derive ( Clone , Eq , PartialEq , Debug ) ]
16
- pub enum Type < ' a > {
17
- /// A nullable named type, e.g. `String`
18
- Named ( Cow < ' a , str > ) ,
19
- /// A nullable list type, e.g. `[String]`
15
+ /// This enum carries no semantic information and might refer to types that do not exist.
16
+ # [ derive ( Clone , Debug , Eq , PartialEq ) ]
17
+ pub enum Type < N = ArcStr > {
18
+ /// `null`able named type, e.g. `String`.
19
+ Named ( N ) ,
20
+
21
+ /// `null`able list type, e.g. `[String]`.
20
22
///
21
- /// The list itself is what's nullable, the containing type might be non-null.
22
- List ( Box < Type < ' a > > , Option < usize > ) ,
23
- /// A non-null named type, e.g. `String!`
24
- NonNullNamed ( Cow < ' a , str > ) ,
25
- /// A non-null list type, e.g. `[String]!`.
23
+ /// The list itself is `null`able, the containing [`Type`] might be non-`null`.
24
+ List ( Box < Type < N > > , Option < usize > ) ,
25
+
26
+ /// Non-`null` named type, e.g. `String!`.
27
+ NonNullNamed ( N ) ,
28
+
29
+ /// Non-`null` list type, e.g. `[String]!`.
26
30
///
27
- /// The list itself is what's non-null, the containing type might be null.
28
- NonNullList ( Box < Type < ' a > > , Option < usize > ) ,
31
+ /// The list itself is non-`null`, the containing [`Type`] might be `null`able.
32
+ NonNullList ( Box < Type < N > > , Option < usize > ) ,
33
+ }
34
+
35
+ impl < N : fmt:: Display > fmt:: Display for Type < N > {
36
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
37
+ match self {
38
+ Self :: Named ( n) => write ! ( f, "{n}" ) ,
39
+ Self :: NonNullNamed ( n) => write ! ( f, "{n}!" ) ,
40
+ Self :: List ( t, _) => write ! ( f, "[{t}]" ) ,
41
+ Self :: NonNullList ( t, _) => write ! ( f, "[{t}]!" ) ,
42
+ }
43
+ }
44
+ }
45
+
46
+ impl < N : AsRef < str > > Type < N > {
47
+ /// Returns the name of this named [`Type`].
48
+ ///
49
+ /// Only applies to named [`Type`]s. Lists will return [`None`].
50
+ #[ must_use]
51
+ pub fn name ( & self ) -> Option < & str > {
52
+ match self {
53
+ Self :: Named ( n) | Self :: NonNullNamed ( n) => Some ( n. as_ref ( ) ) ,
54
+ Self :: List ( ..) | Self :: NonNullList ( ..) => None ,
55
+ }
56
+ }
57
+
58
+ /// Returns the innermost name of this [`Type`] by unpacking lists.
59
+ ///
60
+ /// All [`Type`] literals contain exactly one named type.
61
+ #[ must_use]
62
+ pub fn innermost_name ( & self ) -> & str {
63
+ match self {
64
+ Self :: Named ( n) | Self :: NonNullNamed ( n) => n. as_ref ( ) ,
65
+ Self :: List ( l, ..) | Self :: NonNullList ( l, ..) => l. innermost_name ( ) ,
66
+ }
67
+ }
68
+
69
+ /// Indicates whether this [`Type`] can only represent non-`null` values.
70
+ #[ must_use]
71
+ pub fn is_non_null ( & self ) -> bool {
72
+ match self {
73
+ Self :: NonNullList ( ..) | Self :: NonNullNamed ( ..) => true ,
74
+ Self :: List ( ..) | Self :: Named ( ..) => false ,
75
+ }
76
+ }
29
77
}
30
78
31
79
/// A JSON-like value that can be passed into the query execution, either
@@ -34,8 +82,8 @@ pub enum Type<'a> {
34
82
///
35
83
/// Lists and objects variants are _spanned_, i.e. they contain a reference to
36
84
/// their position in the source file, if available.
37
- #[ derive( Clone , Debug , PartialEq ) ]
38
85
#[ expect( missing_docs, reason = "self-explanatory" ) ]
86
+ #[ derive( Clone , Debug , PartialEq ) ]
39
87
pub enum InputValue < S = DefaultScalarValue > {
40
88
Null ,
41
89
Scalar ( S ) ,
@@ -45,24 +93,24 @@ pub enum InputValue<S = DefaultScalarValue> {
45
93
Object ( Vec < ( Spanning < String > , Spanning < InputValue < S > > ) > ) ,
46
94
}
47
95
48
- #[ derive( Clone , PartialEq , Debug ) ]
96
+ #[ derive( Clone , Debug , PartialEq ) ]
49
97
pub struct VariableDefinition < ' a , S > {
50
- pub var_type : Spanning < Type < ' a > > ,
98
+ pub var_type : Spanning < Type < & ' a str > > ,
51
99
pub default_value : Option < Spanning < InputValue < S > > > ,
52
100
pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
53
101
}
54
102
55
- #[ derive( Clone , PartialEq , Debug ) ]
103
+ #[ derive( Clone , Debug , PartialEq ) ]
56
104
pub struct Arguments < ' a , S > {
57
105
pub items : Vec < ( Spanning < & ' a str > , Spanning < InputValue < S > > ) > ,
58
106
}
59
107
60
- #[ derive( Clone , PartialEq , Debug ) ]
108
+ #[ derive( Clone , Debug , PartialEq ) ]
61
109
pub struct VariableDefinitions < ' a , S > {
62
110
pub items : Vec < ( Spanning < & ' a str > , VariableDefinition < ' a , S > ) > ,
63
111
}
64
112
65
- #[ derive( Clone , PartialEq , Debug ) ]
113
+ #[ derive( Clone , Debug , PartialEq ) ]
66
114
pub struct Field < ' a , S > {
67
115
pub alias : Option < Spanning < & ' a str > > ,
68
116
pub name : Spanning < & ' a str > ,
@@ -71,13 +119,13 @@ pub struct Field<'a, S> {
71
119
pub selection_set : Option < Vec < Selection < ' a , S > > > ,
72
120
}
73
121
74
- #[ derive( Clone , PartialEq , Debug ) ]
122
+ #[ derive( Clone , Debug , PartialEq ) ]
75
123
pub struct FragmentSpread < ' a , S > {
76
124
pub name : Spanning < & ' a str > ,
77
125
pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
78
126
}
79
127
80
- #[ derive( Clone , PartialEq , Debug ) ]
128
+ #[ derive( Clone , Debug , PartialEq ) ]
81
129
pub struct InlineFragment < ' a , S > {
82
130
pub type_condition : Option < Spanning < & ' a str > > ,
83
131
pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
@@ -99,15 +147,15 @@ pub struct InlineFragment<'a, S> {
99
147
/// }
100
148
/// }
101
149
/// ```
102
- #[ derive( Clone , PartialEq , Debug ) ]
103
150
#[ expect( missing_docs, reason = "self-explanatory" ) ]
151
+ #[ derive( Clone , Debug , PartialEq ) ]
104
152
pub enum Selection < ' a , S = DefaultScalarValue > {
105
153
Field ( Spanning < Field < ' a , S > > ) ,
106
154
FragmentSpread ( Spanning < FragmentSpread < ' a , S > > ) ,
107
155
InlineFragment ( Spanning < InlineFragment < ' a , S > > ) ,
108
156
}
109
157
110
- #[ derive( Clone , PartialEq , Debug ) ]
158
+ #[ derive( Clone , Debug , PartialEq ) ]
111
159
pub struct Directive < ' a , S > {
112
160
pub name : Spanning < & ' a str > ,
113
161
pub arguments : Option < Spanning < Arguments < ' a , S > > > ,
@@ -122,7 +170,7 @@ pub enum OperationType {
122
170
}
123
171
124
172
#[ expect( missing_docs, reason = "self-explanatory" ) ]
125
- #[ derive( Clone , PartialEq , Debug ) ]
173
+ #[ derive( Clone , Debug , PartialEq ) ]
126
174
pub struct Operation < ' a , S > {
127
175
pub operation_type : OperationType ,
128
176
pub name : Option < Spanning < & ' a str > > ,
@@ -131,7 +179,7 @@ pub struct Operation<'a, S> {
131
179
pub selection_set : Vec < Selection < ' a , S > > ,
132
180
}
133
181
134
- #[ derive( Clone , PartialEq , Debug ) ]
182
+ #[ derive( Clone , Debug , PartialEq ) ]
135
183
pub struct Fragment < ' a , S > {
136
184
pub name : Spanning < & ' a str > ,
137
185
pub type_condition : Spanning < & ' a str > ,
@@ -140,7 +188,7 @@ pub struct Fragment<'a, S> {
140
188
}
141
189
142
190
#[ doc( hidden) ]
143
- #[ derive( Clone , PartialEq , Debug ) ]
191
+ #[ derive( Clone , Debug , PartialEq ) ]
144
192
pub enum Definition < ' a , S > {
145
193
Operation ( Spanning < Operation < ' a , S > > ) ,
146
194
Fragment ( Spanning < Fragment < ' a , S > > ) ,
@@ -194,44 +242,6 @@ pub trait ToInputValue<S = DefaultScalarValue>: Sized {
194
242
fn to_input_value ( & self ) -> InputValue < S > ;
195
243
}
196
244
197
- impl Type < ' _ > {
198
- /// Get the name of a named type.
199
- ///
200
- /// Only applies to named types; lists will return `None`.
201
- pub fn name ( & self ) -> Option < & str > {
202
- match * self {
203
- Type :: Named ( ref n) | Type :: NonNullNamed ( ref n) => Some ( n) ,
204
- _ => None ,
205
- }
206
- }
207
-
208
- /// Get the innermost name by unpacking lists
209
- ///
210
- /// All type literals contain exactly one named type.
211
- pub fn innermost_name ( & self ) -> & str {
212
- match * self {
213
- Type :: Named ( ref n) | Type :: NonNullNamed ( ref n) => n,
214
- Type :: List ( ref l, _) | Type :: NonNullList ( ref l, _) => l. innermost_name ( ) ,
215
- }
216
- }
217
-
218
- /// Determines if a type only can represent non-null values.
219
- pub fn is_non_null ( & self ) -> bool {
220
- matches ! ( * self , Type :: NonNullNamed ( _) | Type :: NonNullList ( ..) )
221
- }
222
- }
223
-
224
- impl fmt:: Display for Type < ' _ > {
225
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
226
- match self {
227
- Self :: Named ( n) => write ! ( f, "{n}" ) ,
228
- Self :: NonNullNamed ( n) => write ! ( f, "{n}!" ) ,
229
- Self :: List ( t, _) => write ! ( f, "[{t}]" ) ,
230
- Self :: NonNullList ( t, _) => write ! ( f, "[{t}]!" ) ,
231
- }
232
- }
233
- }
234
-
235
245
impl < S > InputValue < S > {
236
246
/// Construct a `null` value.
237
247
pub fn null ( ) -> Self {
@@ -574,7 +584,7 @@ impl<'a, S> Arguments<'a, S> {
574
584
}
575
585
576
586
impl < ' a , S > VariableDefinitions < ' a , S > {
577
- pub fn iter ( & self ) -> slice:: Iter < ( Spanning < & ' a str > , VariableDefinition < S > ) > {
587
+ pub fn iter ( & self ) -> slice:: Iter < ( Spanning < & ' a str > , VariableDefinition < ' a , S > ) > {
578
588
self . items . iter ( )
579
589
}
580
590
}
0 commit comments