@@ -6,19 +6,19 @@ use query::QueryContext;
6
6
use schema:: DEFAULT_SCALARS ;
7
7
8
8
#[ derive( Clone , Debug , PartialEq , Hash ) ]
9
- pub enum FieldType {
10
- Named ( String ) ,
11
- Optional ( Box < FieldType > ) ,
12
- Vector ( Box < FieldType > ) ,
9
+ pub enum FieldType < ' a > {
10
+ Named ( & ' a str ) ,
11
+ Optional ( Box < FieldType < ' a > > ) ,
12
+ Vector ( Box < FieldType < ' a > > ) ,
13
13
}
14
14
15
- impl FieldType {
15
+ impl < ' a > FieldType < ' a > {
16
16
/// Takes a field type with its name
17
17
pub ( crate ) fn to_rust ( & self , context : & QueryContext , prefix : & str ) -> TokenStream {
18
- let prefix: String = if prefix. is_empty ( ) {
19
- self . inner_name_string ( )
18
+ let prefix: & str = if prefix. is_empty ( ) {
19
+ self . inner_name_str ( )
20
20
} else {
21
- prefix. to_string ( )
21
+ prefix
22
22
} ;
23
23
match & self {
24
24
FieldType :: Named ( ref name) => {
@@ -30,7 +30,7 @@ impl FieldType {
30
30
. is_some ( )
31
31
|| DEFAULT_SCALARS . iter ( ) . any ( |elem| elem == name)
32
32
{
33
- name. clone ( )
33
+ name. to_string ( )
34
34
} else if context
35
35
. schema
36
36
. enums
@@ -43,7 +43,7 @@ impl FieldType {
43
43
if prefix. is_empty ( ) {
44
44
panic ! ( "Empty prefix for {:?}" , self ) ;
45
45
}
46
- prefix
46
+ prefix. to_string ( )
47
47
} ;
48
48
let full_name = Ident :: new ( & full_name, Span :: call_site ( ) ) ;
49
49
@@ -61,11 +61,11 @@ impl FieldType {
61
61
}
62
62
63
63
/// Return the innermost name - we mostly use this for looking types up in our Schema struct.
64
- pub fn inner_name_string ( & self ) -> String {
64
+ pub fn inner_name_str ( & self ) -> & str {
65
65
match & self {
66
- FieldType :: Named ( name) => name. to_string ( ) ,
67
- FieldType :: Optional ( inner) => inner. inner_name_string ( ) ,
68
- FieldType :: Vector ( inner) => inner. inner_name_string ( ) ,
66
+ FieldType :: Named ( name) => name,
67
+ FieldType :: Optional ( inner) => inner. inner_name_str ( ) ,
68
+ FieldType :: Vector ( inner) => inner. inner_name_str ( ) ,
69
69
}
70
70
}
71
71
@@ -77,16 +77,19 @@ impl FieldType {
77
77
}
78
78
}
79
79
80
- impl :: std:: convert:: From < graphql_parser:: schema:: Type > for FieldType {
81
- fn from ( schema_type : graphql_parser:: schema:: Type ) -> FieldType {
80
+ impl < ' schema > :: std:: convert:: From < & ' schema graphql_parser:: schema:: Type > for FieldType < ' schema > {
81
+ fn from ( schema_type : & ' schema graphql_parser:: schema:: Type ) -> FieldType < ' schema > {
82
82
from_schema_type_inner ( schema_type, false )
83
83
}
84
84
}
85
85
86
- fn from_schema_type_inner ( inner : graphql_parser:: schema:: Type , non_null : bool ) -> FieldType {
86
+ fn from_schema_type_inner < ' schema > (
87
+ inner : & ' schema graphql_parser:: schema:: Type ,
88
+ non_null : bool ,
89
+ ) -> FieldType < ' schema > {
87
90
match inner {
88
91
graphql_parser:: schema:: Type :: ListType ( inner) => {
89
- let inner = from_schema_type_inner ( * inner, false ) ;
92
+ let inner = from_schema_type_inner ( & * inner, false ) ;
90
93
let f = FieldType :: Vector ( Box :: new ( inner) ) ;
91
94
if non_null {
92
95
f
@@ -102,21 +105,21 @@ fn from_schema_type_inner(inner: graphql_parser::schema::Type, non_null: bool) -
102
105
FieldType :: Optional ( Box :: new ( f) )
103
106
}
104
107
}
105
- graphql_parser:: schema:: Type :: NonNullType ( inner) => from_schema_type_inner ( * inner, true ) ,
108
+ graphql_parser:: schema:: Type :: NonNullType ( inner) => from_schema_type_inner ( & * inner, true ) ,
106
109
}
107
110
}
108
111
109
112
fn from_json_type_inner ( inner : & introspection_response:: TypeRef , non_null : bool ) -> FieldType {
110
113
use introspection_response:: * ;
111
- let inner = inner. clone ( ) ;
112
114
113
115
match inner. kind {
114
- Some ( __TypeKind:: NON_NULL ) => {
115
- from_json_type_inner ( & inner. of_type . expect ( "inner type is missing" ) , true )
116
- }
116
+ Some ( __TypeKind:: NON_NULL ) => from_json_type_inner (
117
+ & inner. of_type . as_ref ( ) . expect ( "inner type is missing" ) ,
118
+ true ,
119
+ ) ,
117
120
Some ( __TypeKind:: LIST ) => {
118
121
let f = FieldType :: Vector ( Box :: new ( from_json_type_inner (
119
- & inner. of_type . expect ( "inner type is missing" ) ,
122
+ & inner. of_type . as_ref ( ) . expect ( "inner type is missing" ) ,
120
123
false ,
121
124
) ) ) ;
122
125
if non_null {
@@ -126,7 +129,7 @@ fn from_json_type_inner(inner: &introspection_response::TypeRef, non_null: bool)
126
129
}
127
130
}
128
131
Some ( _) => {
129
- let f = FieldType :: Named ( inner. name . expect ( "type name" ) . clone ( ) ) ;
132
+ let f = FieldType :: Named ( & inner. name . as_ref ( ) . expect ( "type name" ) ) ;
130
133
if non_null {
131
134
f
132
135
} else {
@@ -137,14 +140,18 @@ fn from_json_type_inner(inner: &introspection_response::TypeRef, non_null: bool)
137
140
}
138
141
}
139
142
140
- impl :: std:: convert:: From < introspection_response:: FullTypeFieldsType > for FieldType {
141
- fn from ( schema_type : introspection_response:: FullTypeFieldsType ) -> FieldType {
143
+ impl < ' schema > :: std:: convert:: From < & ' schema introspection_response:: FullTypeFieldsType >
144
+ for FieldType < ' schema >
145
+ {
146
+ fn from (
147
+ schema_type : & ' schema introspection_response:: FullTypeFieldsType ,
148
+ ) -> FieldType < ' schema > {
142
149
from_json_type_inner ( & schema_type. type_ref , false )
143
150
}
144
151
}
145
152
146
- impl :: std:: convert:: From < introspection_response:: InputValueType > for FieldType {
147
- fn from ( schema_type : introspection_response:: InputValueType ) -> FieldType {
153
+ impl < ' a > :: std:: convert:: From < & ' a introspection_response:: InputValueType > for FieldType < ' a > {
154
+ fn from ( schema_type : & ' a introspection_response:: InputValueType ) -> FieldType < ' a > {
148
155
from_json_type_inner ( & schema_type. type_ref , false )
149
156
}
150
157
}
@@ -157,29 +164,29 @@ mod tests {
157
164
158
165
#[ test]
159
166
fn field_type_from_graphql_parser_schema_type_works ( ) {
160
- let ty = GqlParserType :: NamedType ( "Cat" . to_string ( ) ) ;
167
+ let ty = GqlParserType :: NamedType ( "Cat" . to_owned ( ) ) ;
161
168
assert_eq ! (
162
- FieldType :: from( ty) ,
163
- FieldType :: Optional ( Box :: new( FieldType :: Named ( "Cat" . to_string ( ) ) ) )
169
+ FieldType :: from( & ty) ,
170
+ FieldType :: Optional ( Box :: new( FieldType :: Named ( "Cat" ) ) )
164
171
) ;
165
172
166
- let ty = GqlParserType :: NonNullType ( Box :: new ( GqlParserType :: NamedType ( "Cat" . to_string ( ) ) ) ) ;
173
+ let ty = GqlParserType :: NonNullType ( Box :: new ( GqlParserType :: NamedType ( "Cat" . to_owned ( ) ) ) ) ;
167
174
168
- assert_eq ! ( FieldType :: from( ty) , FieldType :: Named ( "Cat" . to_string ( ) ) ) ;
175
+ assert_eq ! ( FieldType :: from( & ty) , FieldType :: Named ( "Cat" ) ) ;
169
176
}
170
177
171
178
#[ test]
172
179
fn field_type_from_introspection_response_works ( ) {
173
180
let ty = FullTypeFieldsType {
174
181
type_ref : TypeRef {
175
182
kind : Some ( __TypeKind:: OBJECT ) ,
176
- name : Some ( "Cat" . to_string ( ) ) ,
183
+ name : Some ( "Cat" . into ( ) ) ,
177
184
of_type : None ,
178
185
} ,
179
186
} ;
180
187
assert_eq ! (
181
- FieldType :: from( ty) ,
182
- FieldType :: Optional ( Box :: new( FieldType :: Named ( "Cat" . to_string ( ) ) ) )
188
+ FieldType :: from( & ty) ,
189
+ FieldType :: Optional ( Box :: new( FieldType :: Named ( "Cat" ) ) )
183
190
) ;
184
191
185
192
let ty = FullTypeFieldsType {
@@ -188,11 +195,11 @@ mod tests {
188
195
name : None ,
189
196
of_type : Some ( Box :: new ( TypeRef {
190
197
kind : Some ( __TypeKind:: OBJECT ) ,
191
- name : Some ( "Cat" . to_string ( ) ) ,
198
+ name : Some ( "Cat" . into ( ) ) ,
192
199
of_type : None ,
193
200
} ) ) ,
194
201
} ,
195
202
} ;
196
- assert_eq ! ( FieldType :: from( ty) , FieldType :: Named ( "Cat" . to_string ( ) ) ) ;
203
+ assert_eq ! ( FieldType :: from( & ty) , FieldType :: Named ( "Cat" ) ) ;
197
204
}
198
205
}
0 commit comments