@@ -6,19 +6,19 @@ use query::QueryContext;
66use schema:: DEFAULT_SCALARS ;
77
88#[ 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 > > ) ,
1313}
1414
15- impl FieldType {
15+ impl < ' a > FieldType < ' a > {
1616 /// Takes a field type with its name
1717 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 ( )
2020 } else {
21- prefix. to_string ( )
21+ prefix
2222 } ;
2323 match & self {
2424 FieldType :: Named ( ref name) => {
@@ -30,7 +30,7 @@ impl FieldType {
3030 . is_some ( )
3131 || DEFAULT_SCALARS . iter ( ) . any ( |elem| elem == name)
3232 {
33- name. clone ( )
33+ name. to_string ( )
3434 } else if context
3535 . schema
3636 . enums
@@ -43,7 +43,7 @@ impl FieldType {
4343 if prefix. is_empty ( ) {
4444 panic ! ( "Empty prefix for {:?}" , self ) ;
4545 }
46- prefix
46+ prefix. to_string ( )
4747 } ;
4848 let full_name = Ident :: new ( & full_name, Span :: call_site ( ) ) ;
4949
@@ -61,11 +61,11 @@ impl FieldType {
6161 }
6262
6363 /// 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 {
6565 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 ( ) ,
6969 }
7070 }
7171
@@ -77,16 +77,19 @@ impl FieldType {
7777 }
7878}
7979
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 > {
8282 from_schema_type_inner ( schema_type, false )
8383 }
8484}
8585
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 > {
8790 match inner {
8891 graphql_parser:: schema:: Type :: ListType ( inner) => {
89- let inner = from_schema_type_inner ( * inner, false ) ;
92+ let inner = from_schema_type_inner ( & * inner, false ) ;
9093 let f = FieldType :: Vector ( Box :: new ( inner) ) ;
9194 if non_null {
9295 f
@@ -102,21 +105,21 @@ fn from_schema_type_inner(inner: graphql_parser::schema::Type, non_null: bool) -
102105 FieldType :: Optional ( Box :: new ( f) )
103106 }
104107 }
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 ) ,
106109 }
107110}
108111
109112fn from_json_type_inner ( inner : & introspection_response:: TypeRef , non_null : bool ) -> FieldType {
110113 use introspection_response:: * ;
111- let inner = inner. clone ( ) ;
112114
113115 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+ ) ,
117120 Some ( __TypeKind:: LIST ) => {
118121 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" ) ,
120123 false ,
121124 ) ) ) ;
122125 if non_null {
@@ -126,7 +129,7 @@ fn from_json_type_inner(inner: &introspection_response::TypeRef, non_null: bool)
126129 }
127130 }
128131 Some ( _) => {
129- let f = FieldType :: Named ( inner. name . expect ( "type name" ) . clone ( ) ) ;
132+ let f = FieldType :: Named ( & inner. name . as_ref ( ) . expect ( "type name" ) ) ;
130133 if non_null {
131134 f
132135 } else {
@@ -137,14 +140,18 @@ fn from_json_type_inner(inner: &introspection_response::TypeRef, non_null: bool)
137140 }
138141}
139142
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 > {
142149 from_json_type_inner ( & schema_type. type_ref , false )
143150 }
144151}
145152
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 > {
148155 from_json_type_inner ( & schema_type. type_ref , false )
149156 }
150157}
@@ -157,29 +164,29 @@ mod tests {
157164
158165 #[ test]
159166 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 ( ) ) ;
161168 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" ) ) )
164171 ) ;
165172
166- let ty = GqlParserType :: NonNullType ( Box :: new ( GqlParserType :: NamedType ( "Cat" . to_string ( ) ) ) ) ;
173+ let ty = GqlParserType :: NonNullType ( Box :: new ( GqlParserType :: NamedType ( "Cat" . to_owned ( ) ) ) ) ;
167174
168- assert_eq ! ( FieldType :: from( ty) , FieldType :: Named ( "Cat" . to_string ( ) ) ) ;
175+ assert_eq ! ( FieldType :: from( & ty) , FieldType :: Named ( "Cat" ) ) ;
169176 }
170177
171178 #[ test]
172179 fn field_type_from_introspection_response_works ( ) {
173180 let ty = FullTypeFieldsType {
174181 type_ref : TypeRef {
175182 kind : Some ( __TypeKind:: OBJECT ) ,
176- name : Some ( "Cat" . to_string ( ) ) ,
183+ name : Some ( "Cat" . into ( ) ) ,
177184 of_type : None ,
178185 } ,
179186 } ;
180187 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" ) ) )
183190 ) ;
184191
185192 let ty = FullTypeFieldsType {
@@ -188,11 +195,11 @@ mod tests {
188195 name : None ,
189196 of_type : Some ( Box :: new ( TypeRef {
190197 kind : Some ( __TypeKind:: OBJECT ) ,
191- name : Some ( "Cat" . to_string ( ) ) ,
198+ name : Some ( "Cat" . into ( ) ) ,
192199 of_type : None ,
193200 } ) ) ,
194201 } ,
195202 } ;
196- assert_eq ! ( FieldType :: from( ty) , FieldType :: Named ( "Cat" . to_string ( ) ) ) ;
203+ assert_eq ! ( FieldType :: from( & ty) , FieldType :: Named ( "Cat" ) ) ;
197204 }
198205}
0 commit comments