@@ -325,29 +325,62 @@ impl Config {
325
325
326
326
/// When using nightly options on stable channel decide whether to abort or
327
327
/// log out warnings to the user.
328
- pub fn abort_or_warn_on_unstable_options ( & self ) -> Option < HandleUnstableOptions > {
328
+ pub fn abort_or_warn_on_unstable_options ( & self ) -> HandleUnstableOptions {
329
329
if !self . using_unstable_options_on_stable_channel ( ) {
330
- return None ;
330
+ return HandleUnstableOptions :: Continue ;
331
331
}
332
332
333
333
if self . abort_on_unrecognised_options ( ) {
334
- Some ( HandleUnstableOptions :: Abort (
335
- self . unstable_options_abort_message ( ) . unwrap ( ) ,
336
- ) )
334
+ HandleUnstableOptions :: Abort ( self . unstable_options_abort_message ( ) . unwrap ( ) )
337
335
} else {
338
- Some ( HandleUnstableOptions :: Warn (
339
- self . unstable_options_warning_message ( ) . unwrap ( ) ,
340
- ) )
336
+ HandleUnstableOptions :: Warn ( self . unstable_options_warning_message ( ) . unwrap ( ) )
341
337
}
342
338
}
343
339
}
344
340
341
+ /// Errors that can occur when loading configuration
342
+ #[ derive( Debug ) ]
343
+ pub enum LoadConfigurationError {
344
+ /// An io error when reading configuration.
345
+ IoError ( std:: io:: Error ) ,
346
+ /// Used unstable options on the stable channel
347
+ UsedUnstableOptions ( String ) ,
348
+ }
349
+
350
+ impl From < std:: io:: Error > for LoadConfigurationError {
351
+ fn from ( e : std:: io:: Error ) -> LoadConfigurationError {
352
+ LoadConfigurationError :: IoError ( e)
353
+ }
354
+ }
355
+
356
+ impl std:: error:: Error for LoadConfigurationError { }
357
+
358
+ impl std:: fmt:: Display for LoadConfigurationError {
359
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
360
+ match self {
361
+ Self :: IoError ( e) => e. fmt ( f) ,
362
+ Self :: UsedUnstableOptions ( msg) => {
363
+ write ! ( f, "{}" , msg)
364
+ }
365
+ }
366
+ }
367
+ }
368
+
369
+ pub enum HandleUnstableOptions {
370
+ // Exit early. Do not allow rustfmt to format any files
371
+ Abort ( String ) ,
372
+ // Warn the user that they are using unstable options and format files
373
+ Warn ( String ) ,
374
+ // Proceed as normal. No need to warn or abort.
375
+ Continue ,
376
+ }
377
+
345
378
/// Loads a config by checking the client-supplied options and if appropriate, the
346
379
/// file system (including searching the file system for overrides).
347
380
pub fn load_config < O : CliOptions > (
348
381
file_path : Option < & Path > ,
349
382
options : Option < O > ,
350
- ) -> Result < ( Config , Option < PathBuf > ) , Error > {
383
+ ) -> Result < ( Config , Option < PathBuf > ) , LoadConfigurationError > {
351
384
let over_ride = match options {
352
385
Some ( ref opts) => config_path ( opts) ?,
353
386
None => None ,
@@ -361,12 +394,23 @@ pub fn load_config<O: CliOptions>(
361
394
Ok ( ( Config :: default ( ) , None ) )
362
395
} ;
363
396
364
- result. map ( |( mut c, p) | {
397
+ let ( config , options ) = result. map ( |( mut c, p) | {
365
398
if let Some ( options) = options {
366
399
options. apply_to ( & mut c) ;
400
+ c. collect_unstable_options ( ) ;
367
401
}
368
402
( c, p)
369
- } )
403
+ } ) ?;
404
+
405
+ use HandleUnstableOptions :: { Abort , Continue , Warn } ;
406
+ match config. abort_or_warn_on_unstable_options ( ) {
407
+ Abort ( s) => Err ( LoadConfigurationError :: UsedUnstableOptions ( s) ) ,
408
+ Warn ( warning) => {
409
+ eprint ! ( "{}" , warning) ;
410
+ Ok ( ( config, options) )
411
+ }
412
+ Continue => Ok ( ( config, options) ) ,
413
+ }
370
414
}
371
415
372
416
// Check for the presence of known config file names (`rustfmt.toml, `.rustfmt.toml`) in `dir`
@@ -420,29 +464,6 @@ fn config_path(options: &dyn CliOptions) -> Result<Option<PathBuf>, Error> {
420
464
}
421
465
}
422
466
423
- #[ macro_export]
424
- macro_rules! abort_or_warn_on_unstable_options {
425
- ( $config: expr) => {
426
- match $config. abort_or_warn_on_unstable_options( ) {
427
- Some ( $crate:: HandleUnstableOptions :: Abort ( message) ) => {
428
- eprint!( "{}" , message) ;
429
- return Ok ( 1 ) ;
430
- }
431
- Some ( $crate:: HandleUnstableOptions :: Warn ( message) ) => {
432
- eprint!( "{}" , message) ;
433
- }
434
- None => { }
435
- }
436
- } ;
437
- }
438
-
439
- #[ allow( unreachable_pub) ]
440
- pub use abort_or_warn_on_unstable_options;
441
- pub enum HandleUnstableOptions {
442
- Abort ( String ) ,
443
- Warn ( String ) ,
444
- }
445
-
446
467
#[ cfg( test) ]
447
468
mod test {
448
469
use super :: * ;
@@ -701,7 +722,7 @@ Set `abort_on_unrecognised_options = false` to convert this error into a warning
701
722
let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
702
723
assert ! ( matches!(
703
724
config. abort_or_warn_on_unstable_options( ) ,
704
- Some ( HandleUnstableOptions :: Warn ( _) )
725
+ HandleUnstableOptions :: Warn ( _)
705
726
) )
706
727
}
707
728
@@ -718,7 +739,7 @@ Set `abort_on_unrecognised_options = false` to convert this error into a warning
718
739
let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
719
740
assert ! ( matches!(
720
741
config. abort_or_warn_on_unstable_options( ) ,
721
- Some ( HandleUnstableOptions :: Abort ( _) )
742
+ HandleUnstableOptions :: Abort ( _)
722
743
) )
723
744
}
724
745
@@ -733,7 +754,10 @@ Set `abort_on_unrecognised_options = false` to convert this error into a warning
733
754
array_width = 50
734
755
"# ;
735
756
let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
736
- assert ! ( matches!( config. abort_or_warn_on_unstable_options( ) , None ) )
757
+ assert ! ( matches!(
758
+ config. abort_or_warn_on_unstable_options( ) ,
759
+ HandleUnstableOptions :: Continue
760
+ ) )
737
761
}
738
762
739
763
#[ test]
@@ -747,7 +771,10 @@ Set `abort_on_unrecognised_options = false` to convert this error into a warning
747
771
array_width = 50
748
772
"# ;
749
773
let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
750
- assert ! ( matches!( config. abort_or_warn_on_unstable_options( ) , None ) )
774
+ assert ! ( matches!(
775
+ config. abort_or_warn_on_unstable_options( ) ,
776
+ HandleUnstableOptions :: Continue
777
+ ) )
751
778
}
752
779
753
780
#[ test]
0 commit comments