1
1
use super :: configuration:: Configuration ;
2
2
3
- use anyhow:: Result ;
3
+ use anyhow:: { bail , Result } ;
4
4
use rustfmt_nightly:: { Input , Session } ;
5
5
6
6
pub fn format_text ( file_text : & str , config : & Configuration ) -> Result < String > {
@@ -9,16 +9,18 @@ pub fn format_text(file_text: &str, config: &Configuration) -> Result<String> {
9
9
let input = Input :: Text ( String :: from ( file_text) ) ;
10
10
let mut session = Session :: new ( config. rustfmt_config . clone ( ) , Some ( & mut out) ) ;
11
11
session. format ( input) ?;
12
+
13
+ if !session. has_no_errors ( ) {
14
+ bail ! ( "Rustfmt had errors." ) ;
15
+ }
12
16
}
13
17
14
18
let text = std:: str:: from_utf8 ( & out) ?;
15
19
// rustfmt adds this prefix, so just ignore it
16
- Ok (
17
- text
18
- . trim_start_matches ( "<stdin>:\n \n " )
19
- . trim_start_matches ( "stdin:\n \n " )
20
- . to_string ( ) ,
21
- )
20
+ let text = text
21
+ . trim_start_matches ( "<stdin>:\n \n " )
22
+ . trim_start_matches ( "stdin:\n \n " ) ;
23
+ Ok ( text. to_string ( ) )
22
24
}
23
25
24
26
#[ cfg( test) ]
@@ -35,5 +37,12 @@ mod test {
35
37
let config = resolve_config ( Default :: default ( ) , & global_config) . config ;
36
38
assert_eq ! ( format_text( "use test;" , & config) . unwrap( ) , "use test;\n " ) ;
37
39
assert_eq ! ( format_text( "use test;\n " , & config) . unwrap( ) , "use test;\n " ) ;
40
+ assert_eq ! (
41
+ format_text( "let test = ...;" , & config)
42
+ . err( )
43
+ . unwrap( )
44
+ . to_string( ) ,
45
+ "Rustfmt had errors."
46
+ ) ;
38
47
}
39
48
}
0 commit comments