File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ use std:: convert:: From ;
1
2
use std:: marker:: PhantomData ;
3
+ use std:: ops:: Deref ;
2
4
3
5
use ast:: { InputValue , Selection , FromInputValue , ToInputValue } ;
4
6
use value:: Value ;
@@ -11,8 +13,23 @@ use types::base::GraphQLType;
11
13
/// An ID as defined by the GraphQL specification
12
14
///
13
15
/// Represented as a string, but can be converted _to_ from an integer as well.
16
+ #[ derive( Clone , Debug , Eq , PartialEq ) ]
14
17
pub struct ID ( String ) ;
15
18
19
+ impl From < String > for ID {
20
+ fn from ( s : String ) -> ID {
21
+ ID ( s)
22
+ }
23
+ }
24
+
25
+ impl Deref for ID {
26
+ type Target = str ;
27
+
28
+ fn deref ( & self ) -> & str {
29
+ & self . 0
30
+ }
31
+ }
32
+
16
33
graphql_scalar ! ( ID as "ID" {
17
34
resolve( & self ) -> Value {
18
35
Value :: string( & self . 0 )
@@ -156,3 +173,21 @@ impl<T> GraphQLType for EmptyMutation<T> {
156
173
registry. build_object_type :: < Self > ( & [ ] ) . into_meta ( )
157
174
}
158
175
}
176
+
177
+ #[ cfg( test) ]
178
+ mod tests {
179
+ use super :: ID ;
180
+
181
+ #[ test]
182
+ fn test_id_from_string ( ) {
183
+ let actual = ID :: from ( String :: from ( "foo" ) ) ;
184
+ let expected = ID ( String :: from ( "foo" ) ) ;
185
+ assert_eq ! ( actual, expected) ;
186
+ }
187
+
188
+ #[ test]
189
+ fn test_id_deref ( ) {
190
+ let id = ID ( String :: from ( "foo" ) ) ;
191
+ assert_eq ! ( id. len( ) , 3 ) ;
192
+ }
193
+ }
You can’t perform that action at this time.
0 commit comments