@@ -10,6 +10,7 @@ pub fn generate_code(
1010 selected_operation : String ,
1111 additional_derives : Option < String > ,
1212 deprecation_strategy : Option < String > ,
13+ no_formatting : bool ,
1314 output : PathBuf ,
1415) -> Result < ( ) , failure:: Error > {
1516 let deprecation_strategy = deprecation_strategy. as_ref ( ) . map ( |s| s. as_str ( ) ) ;
@@ -25,8 +26,43 @@ pub fn generate_code(
2526 additional_derives,
2627 deprecation_strategy,
2728 } ;
29+
2830 let gen = generate_module_token_stream ( query_path, schema_path, Some ( options) ) ?;
29- let mut file = File :: create ( output) ?;
30- write ! ( file, "{}" , gen . to_string( ) ) ;
31+
32+ let mut file = File :: create ( output. clone ( ) ) ?;
33+
34+ let codes = gen. to_string ( ) ;
35+
36+ if cfg ! ( feature = "rustfmt" ) && !no_formatting {
37+ let codes = format ( & codes) ;
38+ write ! ( file, "{}" , codes) ;
39+ } else {
40+ write ! ( file, "{}" , codes) ;
41+ }
42+
3143 Ok ( ( ) )
3244}
45+
46+ #[ allow( unused_variables) ]
47+ fn format ( codes : & str ) -> String {
48+ #[ cfg( feature = "rustfmt" ) ]
49+ {
50+ use rustfmt:: { Config , Input , Session } ;
51+ use std:: default:: Default ;
52+
53+ let mut config = Config :: default ( ) ;
54+
55+ config. set ( ) . emit_mode ( rustfmt_nightly:: EmitMode :: Stdout ) ;
56+ config. set ( ) . verbose ( rustfmt_nightly:: Verbosity :: Quiet ) ;
57+
58+ let mut out = Vec :: with_capacity ( codes. len ( ) * 2 ) ;
59+
60+ Session :: new ( config, Some ( & mut out) )
61+ . format ( Input :: Text ( codes. to_string ( ) ) )
62+ . unwrap_or_else ( |err| panic ! ( "rustfmt error: {}" , err) ) ;
63+
64+ return String :: from_utf8 ( out) . unwrap ( ) ;
65+ }
66+ #[ cfg( not( feature = "rustfmt" ) ) ]
67+ unreachable ! ( )
68+ }
0 commit comments