@@ -291,14 +291,8 @@ impl Config {
291
291
let mut file = File :: open ( & file_path) ?;
292
292
let mut toml = String :: new ( ) ;
293
293
file. read_to_string ( & mut toml) ?;
294
- Config :: from_toml_for_style_edition (
295
- & toml,
296
- file_path. parent ( ) . unwrap ( ) ,
297
- edition,
298
- style_edition,
299
- version,
300
- )
301
- . map_err ( |err| Error :: new ( ErrorKind :: InvalidData , err) )
294
+ Config :: from_toml_for_style_edition ( & toml, file_path, edition, style_edition, version)
295
+ . map_err ( |err| Error :: new ( ErrorKind :: InvalidData , err) )
302
296
}
303
297
304
298
/// Resolves the config for input in `dir`.
@@ -370,13 +364,13 @@ impl Config {
370
364
}
371
365
372
366
#[ allow( dead_code) ]
373
- pub ( super ) fn from_toml ( toml : & str , dir : & Path ) -> Result < Config , String > {
374
- Self :: from_toml_for_style_edition ( toml, dir , None , None , None )
367
+ pub ( super ) fn from_toml ( toml : & str , file_path : & Path ) -> Result < Config , String > {
368
+ Self :: from_toml_for_style_edition ( toml, file_path , None , None , None )
375
369
}
376
370
377
371
pub ( crate ) fn from_toml_for_style_edition (
378
372
toml : & str ,
379
- dir : & Path ,
373
+ file_path : & Path ,
380
374
edition : Option < Edition > ,
381
375
style_edition : Option < StyleEdition > ,
382
376
version : Option < Version > ,
@@ -400,13 +394,19 @@ impl Config {
400
394
if !err. is_empty ( ) {
401
395
eprint ! ( "{err}" ) ;
402
396
}
397
+ let dir = file_path. parent ( ) . ok_or_else ( || {
398
+ format ! ( "failed to get parent directory for {}" , file_path. display( ) )
399
+ } ) ?;
400
+
403
401
Ok ( parsed_config. to_parsed_config ( style_edition, edition, version, dir) )
404
402
}
405
403
Err ( e) => {
406
- err. push_str ( "Error: Decoding config file failed:\n " ) ;
407
- err. push_str ( format ! ( "{e}\n " ) . as_str ( ) ) ;
408
- err. push_str ( "Please check your config file." ) ;
409
- Err ( err)
404
+ let err_msg = format ! (
405
+ "The file `{}` failed to parse.\n Error details: {e}" ,
406
+ file_path. display( )
407
+ ) ;
408
+ err. push_str ( & err_msg) ;
409
+ Err ( err_msg)
410
410
}
411
411
}
412
412
}
@@ -674,7 +674,7 @@ mod test {
674
674
675
675
#[ test]
676
676
fn test_was_set ( ) {
677
- let config = Config :: from_toml ( "hard_tabs = true" , Path :: new ( "" ) ) . unwrap ( ) ;
677
+ let config = Config :: from_toml ( "hard_tabs = true" , Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
678
678
679
679
assert_eq ! ( config. was_set( ) . hard_tabs( ) , true ) ;
680
680
assert_eq ! ( config. was_set( ) . verbose( ) , false ) ;
@@ -933,7 +933,8 @@ make_backup = false
933
933
#[ nightly_only_test]
934
934
#[ test]
935
935
fn test_unstable_from_toml ( ) {
936
- let config = Config :: from_toml ( "unstable_features = true" , Path :: new ( "" ) ) . unwrap ( ) ;
936
+ let config =
937
+ Config :: from_toml ( "unstable_features = true" , Path :: new ( "./rustfmt.toml" ) ) . unwrap ( ) ;
937
938
assert_eq ! ( config. was_set( ) . unstable_features( ) , true ) ;
938
939
assert_eq ! ( config. unstable_features( ) , true ) ;
939
940
}
@@ -963,7 +964,7 @@ make_backup = false
963
964
unstable_features = true
964
965
merge_imports = true
965
966
"# ;
966
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
967
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
967
968
assert_eq ! ( config. imports_granularity( ) , ImportGranularity :: Crate ) ;
968
969
}
969
970
@@ -975,7 +976,7 @@ make_backup = false
975
976
merge_imports = true
976
977
imports_granularity = "Preserve"
977
978
"# ;
978
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
979
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
979
980
assert_eq ! ( config. imports_granularity( ) , ImportGranularity :: Preserve ) ;
980
981
}
981
982
@@ -986,7 +987,7 @@ make_backup = false
986
987
unstable_features = true
987
988
merge_imports = true
988
989
"# ;
989
- let mut config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
990
+ let mut config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
990
991
config. override_value ( "imports_granularity" , "Preserve" ) ;
991
992
assert_eq ! ( config. imports_granularity( ) , ImportGranularity :: Preserve ) ;
992
993
}
@@ -998,7 +999,7 @@ make_backup = false
998
999
unstable_features = true
999
1000
imports_granularity = "Module"
1000
1001
"# ;
1001
- let mut config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1002
+ let mut config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1002
1003
config. override_value ( "merge_imports" , "true" ) ;
1003
1004
// no effect: the new option always takes precedence
1004
1005
assert_eq ! ( config. imports_granularity( ) , ImportGranularity :: Module ) ;
@@ -1015,7 +1016,7 @@ make_backup = false
1015
1016
use_small_heuristics = "Default"
1016
1017
max_width = 200
1017
1018
"# ;
1018
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1019
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1019
1020
assert_eq ! ( config. array_width( ) , 120 ) ;
1020
1021
assert_eq ! ( config. attr_fn_like_width( ) , 140 ) ;
1021
1022
assert_eq ! ( config. chain_width( ) , 120 ) ;
@@ -1031,7 +1032,7 @@ make_backup = false
1031
1032
use_small_heuristics = "Max"
1032
1033
max_width = 120
1033
1034
"# ;
1034
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1035
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1035
1036
assert_eq ! ( config. array_width( ) , 120 ) ;
1036
1037
assert_eq ! ( config. attr_fn_like_width( ) , 120 ) ;
1037
1038
assert_eq ! ( config. chain_width( ) , 120 ) ;
@@ -1047,7 +1048,7 @@ make_backup = false
1047
1048
use_small_heuristics = "Off"
1048
1049
max_width = 100
1049
1050
"# ;
1050
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1051
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1051
1052
assert_eq ! ( config. array_width( ) , usize :: max_value( ) ) ;
1052
1053
assert_eq ! ( config. attr_fn_like_width( ) , usize :: max_value( ) ) ;
1053
1054
assert_eq ! ( config. chain_width( ) , usize :: max_value( ) ) ;
@@ -1069,7 +1070,7 @@ make_backup = false
1069
1070
struct_lit_width = 30
1070
1071
struct_variant_width = 34
1071
1072
"# ;
1072
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1073
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1073
1074
assert_eq ! ( config. array_width( ) , 20 ) ;
1074
1075
assert_eq ! ( config. attr_fn_like_width( ) , 40 ) ;
1075
1076
assert_eq ! ( config. chain_width( ) , 20 ) ;
@@ -1091,7 +1092,7 @@ make_backup = false
1091
1092
struct_lit_width = 30
1092
1093
struct_variant_width = 34
1093
1094
"# ;
1094
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1095
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1095
1096
assert_eq ! ( config. array_width( ) , 20 ) ;
1096
1097
assert_eq ! ( config. attr_fn_like_width( ) , 40 ) ;
1097
1098
assert_eq ! ( config. chain_width( ) , 20 ) ;
@@ -1113,7 +1114,7 @@ make_backup = false
1113
1114
struct_lit_width = 30
1114
1115
struct_variant_width = 34
1115
1116
"# ;
1116
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1117
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1117
1118
assert_eq ! ( config. array_width( ) , 20 ) ;
1118
1119
assert_eq ! ( config. attr_fn_like_width( ) , 40 ) ;
1119
1120
assert_eq ! ( config. chain_width( ) , 20 ) ;
@@ -1129,7 +1130,7 @@ make_backup = false
1129
1130
max_width = 90
1130
1131
fn_call_width = 95
1131
1132
"# ;
1132
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1133
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1133
1134
assert_eq ! ( config. fn_call_width( ) , 90 ) ;
1134
1135
}
1135
1136
@@ -1139,7 +1140,7 @@ make_backup = false
1139
1140
max_width = 80
1140
1141
attr_fn_like_width = 90
1141
1142
"# ;
1142
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1143
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1143
1144
assert_eq ! ( config. attr_fn_like_width( ) , 80 ) ;
1144
1145
}
1145
1146
@@ -1149,7 +1150,7 @@ make_backup = false
1149
1150
max_width = 78
1150
1151
struct_lit_width = 90
1151
1152
"# ;
1152
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1153
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1153
1154
assert_eq ! ( config. struct_lit_width( ) , 78 ) ;
1154
1155
}
1155
1156
@@ -1159,7 +1160,7 @@ make_backup = false
1159
1160
max_width = 80
1160
1161
struct_variant_width = 90
1161
1162
"# ;
1162
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1163
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1163
1164
assert_eq ! ( config. struct_variant_width( ) , 80 ) ;
1164
1165
}
1165
1166
@@ -1169,7 +1170,7 @@ make_backup = false
1169
1170
max_width = 60
1170
1171
array_width = 80
1171
1172
"# ;
1172
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1173
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1173
1174
assert_eq ! ( config. array_width( ) , 60 ) ;
1174
1175
}
1175
1176
@@ -1179,7 +1180,7 @@ make_backup = false
1179
1180
max_width = 80
1180
1181
chain_width = 90
1181
1182
"# ;
1182
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1183
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1183
1184
assert_eq ! ( config. chain_width( ) , 80 ) ;
1184
1185
}
1185
1186
@@ -1189,7 +1190,7 @@ make_backup = false
1189
1190
max_width = 70
1190
1191
single_line_if_else_max_width = 90
1191
1192
"# ;
1192
- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1193
+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
1193
1194
assert_eq ! ( config. single_line_if_else_max_width( ) , 70 ) ;
1194
1195
}
1195
1196
0 commit comments