11use std:: { borrow:: Cow , fmt, hash:: Hash , slice, vec} ;
22
3+ use arcstr:: ArcStr ;
4+
35use indexmap:: IndexMap ;
46
57use crate :: {
@@ -8,24 +10,70 @@ use crate::{
810 value:: { DefaultScalarValue , ScalarValue } ,
911} ;
1012
11- /// A type literal in the syntax tree
13+ /// Type literal in a syntax tree.
1214///
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]`.
2022 ///
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]!`.
2630 ///
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+ }
2977}
3078
3179/// A JSON-like value that can be passed into the query execution, either
@@ -34,8 +82,8 @@ pub enum Type<'a> {
3482///
3583/// Lists and objects variants are _spanned_, i.e. they contain a reference to
3684/// their position in the source file, if available.
37- #[ derive( Clone , Debug , PartialEq ) ]
3885#[ expect( missing_docs, reason = "self-explanatory" ) ]
86+ #[ derive( Clone , Debug , PartialEq ) ]
3987pub enum InputValue < S = DefaultScalarValue > {
4088 Null ,
4189 Scalar ( S ) ,
@@ -45,24 +93,24 @@ pub enum InputValue<S = DefaultScalarValue> {
4593 Object ( Vec < ( Spanning < String > , Spanning < InputValue < S > > ) > ) ,
4694}
4795
48- #[ derive( Clone , PartialEq , Debug ) ]
96+ #[ derive( Clone , Debug , PartialEq ) ]
4997pub struct VariableDefinition < ' a , S > {
50- pub var_type : Spanning < Type < ' a > > ,
98+ pub var_type : Spanning < Type < & ' a str > > ,
5199 pub default_value : Option < Spanning < InputValue < S > > > ,
52100 pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
53101}
54102
55- #[ derive( Clone , PartialEq , Debug ) ]
103+ #[ derive( Clone , Debug , PartialEq ) ]
56104pub struct Arguments < ' a , S > {
57105 pub items : Vec < ( Spanning < & ' a str > , Spanning < InputValue < S > > ) > ,
58106}
59107
60- #[ derive( Clone , PartialEq , Debug ) ]
108+ #[ derive( Clone , Debug , PartialEq ) ]
61109pub struct VariableDefinitions < ' a , S > {
62110 pub items : Vec < ( Spanning < & ' a str > , VariableDefinition < ' a , S > ) > ,
63111}
64112
65- #[ derive( Clone , PartialEq , Debug ) ]
113+ #[ derive( Clone , Debug , PartialEq ) ]
66114pub struct Field < ' a , S > {
67115 pub alias : Option < Spanning < & ' a str > > ,
68116 pub name : Spanning < & ' a str > ,
@@ -71,13 +119,13 @@ pub struct Field<'a, S> {
71119 pub selection_set : Option < Vec < Selection < ' a , S > > > ,
72120}
73121
74- #[ derive( Clone , PartialEq , Debug ) ]
122+ #[ derive( Clone , Debug , PartialEq ) ]
75123pub struct FragmentSpread < ' a , S > {
76124 pub name : Spanning < & ' a str > ,
77125 pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
78126}
79127
80- #[ derive( Clone , PartialEq , Debug ) ]
128+ #[ derive( Clone , Debug , PartialEq ) ]
81129pub struct InlineFragment < ' a , S > {
82130 pub type_condition : Option < Spanning < & ' a str > > ,
83131 pub directives : Option < Vec < Spanning < Directive < ' a , S > > > > ,
@@ -99,15 +147,15 @@ pub struct InlineFragment<'a, S> {
99147/// }
100148/// }
101149/// ```
102- #[ derive( Clone , PartialEq , Debug ) ]
103150#[ expect( missing_docs, reason = "self-explanatory" ) ]
151+ #[ derive( Clone , Debug , PartialEq ) ]
104152pub enum Selection < ' a , S = DefaultScalarValue > {
105153 Field ( Spanning < Field < ' a , S > > ) ,
106154 FragmentSpread ( Spanning < FragmentSpread < ' a , S > > ) ,
107155 InlineFragment ( Spanning < InlineFragment < ' a , S > > ) ,
108156}
109157
110- #[ derive( Clone , PartialEq , Debug ) ]
158+ #[ derive( Clone , Debug , PartialEq ) ]
111159pub struct Directive < ' a , S > {
112160 pub name : Spanning < & ' a str > ,
113161 pub arguments : Option < Spanning < Arguments < ' a , S > > > ,
@@ -122,7 +170,7 @@ pub enum OperationType {
122170}
123171
124172#[ expect( missing_docs, reason = "self-explanatory" ) ]
125- #[ derive( Clone , PartialEq , Debug ) ]
173+ #[ derive( Clone , Debug , PartialEq ) ]
126174pub struct Operation < ' a , S > {
127175 pub operation_type : OperationType ,
128176 pub name : Option < Spanning < & ' a str > > ,
@@ -131,7 +179,7 @@ pub struct Operation<'a, S> {
131179 pub selection_set : Vec < Selection < ' a , S > > ,
132180}
133181
134- #[ derive( Clone , PartialEq , Debug ) ]
182+ #[ derive( Clone , Debug , PartialEq ) ]
135183pub struct Fragment < ' a , S > {
136184 pub name : Spanning < & ' a str > ,
137185 pub type_condition : Spanning < & ' a str > ,
@@ -140,7 +188,7 @@ pub struct Fragment<'a, S> {
140188}
141189
142190#[ doc( hidden) ]
143- #[ derive( Clone , PartialEq , Debug ) ]
191+ #[ derive( Clone , Debug , PartialEq ) ]
144192pub enum Definition < ' a , S > {
145193 Operation ( Spanning < Operation < ' a , S > > ) ,
146194 Fragment ( Spanning < Fragment < ' a , S > > ) ,
@@ -194,44 +242,6 @@ pub trait ToInputValue<S = DefaultScalarValue>: Sized {
194242 fn to_input_value ( & self ) -> InputValue < S > ;
195243}
196244
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-
235245impl < S > InputValue < S > {
236246 /// Construct a `null` value.
237247 pub fn null ( ) -> Self {
@@ -574,7 +584,7 @@ impl<'a, S> Arguments<'a, S> {
574584}
575585
576586impl < ' 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 > ) > {
578588 self . items . iter ( )
579589 }
580590}
0 commit comments