@@ -4,8 +4,6 @@ use std::hash::Hash;
44use std:: vec;
55use std:: slice;
66
7- use rustc_serialize:: json:: { ToJson , Json } ;
8-
97use parser:: Spanning ;
108
119/// A type literal in the syntax tree
@@ -266,26 +264,6 @@ impl InputValue {
266264 InputValue :: Object ( o)
267265 }
268266
269- /// Convert a `Json` structure into an `InputValue`.
270- ///
271- /// This consumes the JSON instance.
272- ///
273- /// Notes:
274- /// * No enums or variables will be produced by this method.
275- /// * All lists and objects will be unlocated
276- pub fn from_json ( json : Json ) -> InputValue {
277- match json {
278- Json :: I64 ( i) => InputValue :: int ( i) ,
279- Json :: U64 ( u) => InputValue :: float ( u as f64 ) ,
280- Json :: F64 ( f) => InputValue :: float ( f) ,
281- Json :: String ( s) => InputValue :: string ( s) ,
282- Json :: Boolean ( b) => InputValue :: boolean ( b) ,
283- Json :: Array ( a) => InputValue :: list ( a. into_iter ( ) . map ( InputValue :: from_json) . collect ( ) ) ,
284- Json :: Object ( o) => InputValue :: object ( o. into_iter ( ) . map ( |( k, v) | ( k, InputValue :: from_json ( v) ) ) . collect ( ) ) ,
285- Json :: Null => InputValue :: null ( ) ,
286- }
287- }
288-
289267 /// Resolve all variables to their values.
290268 pub fn into_const ( self , vars : & HashMap < String , InputValue > ) -> InputValue {
291269 match self {
@@ -403,17 +381,38 @@ impl InputValue {
403381 }
404382}
405383
406- impl ToJson for InputValue {
407- fn to_json ( & self ) -> Json {
384+ impl fmt :: Display for InputValue {
385+ fn fmt ( & self , f : & mut fmt :: Formatter ) -> fmt :: Result {
408386 match * self {
409- InputValue :: Null | InputValue :: Variable ( _) => Json :: Null ,
410- InputValue :: Int ( i) => Json :: I64 ( i) ,
411- InputValue :: Float ( f) => Json :: F64 ( f) ,
412- InputValue :: String ( ref s) | InputValue :: Enum ( ref s) => Json :: String ( s. clone ( ) ) ,
413- InputValue :: Boolean ( b) => Json :: Boolean ( b) ,
414- InputValue :: List ( ref l) => Json :: Array ( l. iter ( ) . map ( |x| x. item . to_json ( ) ) . collect ( ) ) ,
415- InputValue :: Object ( ref o) => Json :: Object ( o. iter ( ) . map ( |& ( ref k, ref v) | ( k. item . clone ( ) , v. item . to_json ( ) ) ) . collect ( ) ) ,
416- }
387+ InputValue :: Null => write ! ( f, "null" ) ,
388+ InputValue :: Int ( v) => write ! ( f, "{}" , v) ,
389+ InputValue :: Float ( v) => write ! ( f, "{}" , v) ,
390+ InputValue :: String ( ref v) => write ! ( f, "\" {}\" " , v) ,
391+ InputValue :: Boolean ( v) => write ! ( f, "{}" , v) ,
392+ InputValue :: Enum ( ref v) => write ! ( f, "{}" , v) ,
393+ InputValue :: Variable ( ref v) => write ! ( f, "${}" , v) ,
394+ InputValue :: List ( ref v) => {
395+ try!( write ! ( f, "[" ) ) ;
396+
397+ for ( i, spanning) in v. iter ( ) . enumerate ( ) {
398+ try!( spanning. item . fmt ( f) ) ;
399+ if i < v. len ( ) - 1 { try!( write ! ( f, ", " ) ) ; }
400+ }
401+
402+ write ! ( f, "]" )
403+ } ,
404+ InputValue :: Object ( ref o) => {
405+ try!( write ! ( f, "{{" ) ) ;
406+
407+ for ( i, & ( ref k, ref v) ) in o. iter ( ) . enumerate ( ) {
408+ try!( write ! ( f, "{}: " , k. item) ) ;
409+ try!( v. item . fmt ( f) ) ;
410+ if i < o. len ( ) - 1 { try!( write ! ( f, ", " ) ) ; }
411+ }
412+
413+ write ! ( f, "}}" )
414+ }
415+ }
417416 }
418417}
419418
@@ -448,3 +447,44 @@ impl<'a> VariableDefinitions<'a> {
448447 self . items . iter ( )
449448 }
450449}
450+
451+ #[ cfg( test) ]
452+ mod tests {
453+ use super :: InputValue ;
454+ use parser:: Spanning ;
455+
456+ #[ test]
457+ fn test_input_value_fmt ( ) {
458+ let value = InputValue :: null ( ) ;
459+ assert_eq ! ( format!( "{}" , value) , "null" ) ;
460+
461+ let value = InputValue :: int ( 123 ) ;
462+ assert_eq ! ( format!( "{}" , value) , "123" ) ;
463+
464+ let value = InputValue :: float ( 12.3 ) ;
465+ assert_eq ! ( format!( "{}" , value) , "12.3" ) ;
466+
467+ let value = InputValue :: string ( "FOO" . to_owned ( ) ) ;
468+ assert_eq ! ( format!( "{}" , value) , "\" FOO\" " ) ;
469+
470+ let value = InputValue :: boolean ( true ) ;
471+ assert_eq ! ( format!( "{}" , value) , "true" ) ;
472+
473+ let value = InputValue :: enum_value ( "BAR" . to_owned ( ) ) ;
474+ assert_eq ! ( format!( "{}" , value) , "BAR" ) ;
475+
476+ let value = InputValue :: variable ( "baz" . to_owned ( ) ) ;
477+ assert_eq ! ( format!( "{}" , value) , "$baz" ) ;
478+
479+ let list = vec ! [ InputValue :: int( 1 ) , InputValue :: int( 2 ) ] ;
480+ let value = InputValue :: list ( list) ;
481+ assert_eq ! ( format!( "{}" , value) , "[1, 2]" ) ;
482+
483+ let object = vec ! [
484+ ( Spanning :: unlocated( "foo" . to_owned( ) ) , Spanning :: unlocated( InputValue :: int( 1 ) ) ) ,
485+ ( Spanning :: unlocated( "bar" . to_owned( ) ) , Spanning :: unlocated( InputValue :: int( 2 ) ) ) ,
486+ ] ;
487+ let value = InputValue :: parsed_object ( object) ;
488+ assert_eq ! ( format!( "{}" , value) , "{foo: 1, bar: 2}" ) ;
489+ }
490+ }
0 commit comments