@@ -555,32 +555,24 @@ impl Cfg {
555
555
// Then look for 'rust-toolchain'
556
556
let toolchain_file = d. join ( "rust-toolchain" ) ;
557
557
if let Ok ( contents) = utils:: read_file ( "toolchain file" , & toolchain_file) {
558
- if let ( Some ( override_file) , toml_err) = Cfg :: parse_override_file ( contents) {
559
- if let Some ( toolchain_name) = & override_file. toolchain . channel {
560
- let all_toolchains = self . list_toolchains ( ) ?;
561
- if !all_toolchains. iter ( ) . any ( |s| s == toolchain_name) {
562
- // The given name is not resolvable as a toolchain, so
563
- // instead check it's plausible for installation later
564
- let mut validation = dist:: validate_channel_name ( & toolchain_name)
565
- . chain_err ( || {
566
- format ! (
567
- "error parsing override file as legacy format: invalid channel name '{}' in '{}'" ,
568
- toolchain_name,
569
- toolchain_file. display( )
570
- )
571
- } ) ;
572
- // If there was an error parsing the toolchain file as TOML, report it now.
573
- // This can help if the toolchain file is actually TOML but there was a syntax error.
574
- if let Some ( err) = toml_err {
575
- validation = validation. chain_err ( || err) ;
576
- }
577
- validation?;
578
- }
558
+ let override_file = Cfg :: parse_override_file ( contents) ?;
559
+ if let Some ( toolchain_name) = & override_file. toolchain . channel {
560
+ let all_toolchains = self . list_toolchains ( ) ?;
561
+ if !all_toolchains. iter ( ) . any ( |s| s == toolchain_name) {
562
+ // The given name is not resolvable as a toolchain, so
563
+ // instead check it's plausible for installation later
564
+ dist:: validate_channel_name ( & toolchain_name) . chain_err ( || {
565
+ format ! (
566
+ "invalid channel name '{}' in '{}'" ,
567
+ toolchain_name,
568
+ toolchain_file. display( )
569
+ )
570
+ } ) ?;
579
571
}
580
-
581
- let reason = OverrideReason :: ToolchainFile ( toolchain_file) ;
582
- return Ok ( Some ( ( override_file, reason) ) ) ;
583
572
}
573
+
574
+ let reason = OverrideReason :: ToolchainFile ( toolchain_file) ;
575
+ return Ok ( Some ( ( override_file, reason) ) ) ;
584
576
}
585
577
586
578
dir = d. parent ( ) ;
@@ -589,27 +581,30 @@ impl Cfg {
589
581
Ok ( None )
590
582
}
591
583
592
- fn parse_override_file < S : AsRef < str > > (
593
- contents : S ,
594
- ) -> ( Option < OverrideFile > , Option < ErrorKind > ) {
584
+ fn parse_override_file < S : AsRef < str > > ( contents : S ) -> Result < OverrideFile > {
595
585
let contents = contents. as_ref ( ) ;
596
586
597
- // Avoid TOML parsing error for backwards compatibility with the legacy format
598
- if contents . is_empty ( ) {
599
- return ( None , None ) ;
600
- }
587
+ match contents . lines ( ) . count ( ) {
588
+ 0 => return Err ( ErrorKind :: EmptyOverrideFile . into ( ) ) ,
589
+ 1 => {
590
+ let channel = contents . trim ( ) ;
601
591
602
- // Try to parse as TOML ...
603
- match toml:: from_str :: < OverrideFile > ( contents)
604
- . map ( |file| if file. is_empty ( ) { None } else { Some ( file) } )
605
- . map_err ( ErrorKind :: ParsingOverride )
606
- {
607
- Ok ( override_file) => ( override_file, None ) ,
608
- // ... in case of error, try to parse as the legacy format
609
- Err ( toml_err) => (
610
- contents. lines ( ) . next ( ) . map ( |line| line. trim ( ) . into ( ) ) ,
611
- Some ( toml_err) ,
612
- ) ,
592
+ if channel. is_empty ( ) {
593
+ Err ( ErrorKind :: EmptyOverrideFile . into ( ) )
594
+ } else {
595
+ Ok ( channel. into ( ) )
596
+ }
597
+ }
598
+ _ => {
599
+ let override_file = toml:: from_str :: < OverrideFile > ( contents)
600
+ . map_err ( ErrorKind :: ParsingOverrideFile ) ?;
601
+
602
+ if override_file. is_empty ( ) {
603
+ Err ( ErrorKind :: InvalidOverrideFile . into ( ) )
604
+ } else {
605
+ Ok ( override_file)
606
+ }
607
+ }
613
608
}
614
609
}
615
610
@@ -881,32 +876,31 @@ mod tests {
881
876
fn parse_legacy_toolchain_file ( ) {
882
877
let contents = "nightly-2020-07-10" ;
883
878
884
- let ( override_file , toml_err ) = Cfg :: parse_override_file ( contents) ;
879
+ let result = Cfg :: parse_override_file ( contents) ;
885
880
assert_eq ! (
886
- override_file ,
887
- Some ( OverrideFile {
881
+ result . unwrap ( ) ,
882
+ OverrideFile {
888
883
toolchain: ToolchainSection {
889
884
channel: Some ( contents. into( ) ) ,
890
- ..Default :: default ( )
885
+ components: None ,
886
+ targets: None ,
891
887
}
892
- } )
888
+ }
893
889
) ;
894
- assert ! ( toml_err. is_some( ) ) ;
895
890
}
896
891
897
892
#[ test]
898
893
fn parse_toml_toolchain_file ( ) {
899
- let contents = r#"
900
- [toolchain]
894
+ let contents = r#"[toolchain]
901
895
channel = "nightly-2020-07-10"
902
896
components = [ "rustfmt", "rustc-dev" ]
903
897
targets = [ "wasm32-unknown-unknown", "thumbv2-none-eabi" ]
904
898
"# ;
905
899
906
- let ( override_file , toml_err ) = Cfg :: parse_override_file ( contents) ;
900
+ let result = Cfg :: parse_override_file ( contents) ;
907
901
assert_eq ! (
908
- override_file ,
909
- Some ( OverrideFile {
902
+ result . unwrap ( ) ,
903
+ OverrideFile {
910
904
toolchain: ToolchainSection {
911
905
channel: Some ( "nightly-2020-07-10" . into( ) ) ,
912
906
components: Some ( vec![ "rustfmt" . into( ) , "rustc-dev" . into( ) ] ) ,
@@ -915,95 +909,86 @@ targets = [ "wasm32-unknown-unknown", "thumbv2-none-eabi" ]
915
909
"thumbv2-none-eabi" . into( )
916
910
] ) ,
917
911
}
918
- } )
912
+ }
919
913
) ;
920
- assert ! ( toml_err. is_none( ) ) ;
921
914
}
922
915
923
916
#[ test]
924
917
fn parse_toml_toolchain_file_only_channel ( ) {
925
- let contents = r#"
926
- [toolchain]
918
+ let contents = r#"[toolchain]
927
919
channel = "nightly-2020-07-10"
928
920
"# ;
929
921
930
- let ( override_file , toml_err ) = Cfg :: parse_override_file ( contents) ;
922
+ let result = Cfg :: parse_override_file ( contents) ;
931
923
assert_eq ! (
932
- override_file ,
933
- Some ( OverrideFile {
924
+ result . unwrap ( ) ,
925
+ OverrideFile {
934
926
toolchain: ToolchainSection {
935
927
channel: Some ( "nightly-2020-07-10" . into( ) ) ,
936
928
components: None ,
937
929
targets: None ,
938
930
}
939
- } )
931
+ }
940
932
) ;
941
- assert ! ( toml_err. is_none( ) ) ;
942
933
}
943
934
944
935
#[ test]
945
936
fn parse_toml_toolchain_file_empty_components ( ) {
946
- let contents = r#"
947
- [toolchain]
937
+ let contents = r#"[toolchain]
948
938
channel = "nightly-2020-07-10"
949
939
components = []
950
940
"# ;
951
941
952
- let ( override_file , toml_err ) = Cfg :: parse_override_file ( contents) ;
942
+ let result = Cfg :: parse_override_file ( contents) ;
953
943
assert_eq ! (
954
- override_file ,
955
- Some ( OverrideFile {
944
+ result . unwrap ( ) ,
945
+ OverrideFile {
956
946
toolchain: ToolchainSection {
957
947
channel: Some ( "nightly-2020-07-10" . into( ) ) ,
958
948
components: Some ( vec![ ] ) ,
959
949
targets: None ,
960
950
}
961
- } )
951
+ }
962
952
) ;
963
- assert ! ( toml_err. is_none( ) ) ;
964
953
}
965
954
966
955
#[ test]
967
956
fn parse_toml_toolchain_file_empty_targets ( ) {
968
- let contents = r#"
969
- [toolchain]
957
+ let contents = r#"[toolchain]
970
958
channel = "nightly-2020-07-10"
971
959
targets = []
972
960
"# ;
973
961
974
- let ( override_file , toml_err ) = Cfg :: parse_override_file ( contents) ;
962
+ let result = Cfg :: parse_override_file ( contents) ;
975
963
assert_eq ! (
976
- override_file ,
977
- Some ( OverrideFile {
964
+ result . unwrap ( ) ,
965
+ OverrideFile {
978
966
toolchain: ToolchainSection {
979
967
channel: Some ( "nightly-2020-07-10" . into( ) ) ,
980
968
components: None ,
981
969
targets: Some ( vec![ ] ) ,
982
970
}
983
- } )
971
+ }
984
972
) ;
985
- assert ! ( toml_err. is_none( ) ) ;
986
973
}
987
974
988
975
#[ test]
989
976
fn parse_toml_toolchain_file_no_channel ( ) {
990
- let contents = r#"
991
- [toolchain]
977
+ let contents = r#"[toolchain]
992
978
components = [ "rustfmt" ]
993
979
"# ;
994
980
995
- let ( override_file , toml_err ) = Cfg :: parse_override_file ( contents) ;
981
+ let result = Cfg :: parse_override_file ( contents) ;
996
982
assert_eq ! (
997
- override_file ,
998
- Some ( OverrideFile {
983
+ result . unwrap ( ) ,
984
+ OverrideFile {
999
985
toolchain: ToolchainSection {
1000
986
channel: None ,
1001
987
components: Some ( vec![ "rustfmt" . into( ) ] ) ,
1002
988
targets: None ,
1003
989
}
1004
- } )
990
+ }
1005
991
) ;
1006
- assert ! ( toml_err. is_none( ) ) ;
1007
992
}
1008
993
1009
994
#[ test]
@@ -1012,18 +997,33 @@ components = [ "rustfmt" ]
1012
997
[toolchain]
1013
998
"# ;
1014
999
1015
- let ( override_file, toml_err) = Cfg :: parse_override_file ( contents) ;
1016
- assert ! ( override_file. is_none( ) ) ;
1017
- assert ! ( toml_err. is_none( ) ) ;
1000
+ let result = Cfg :: parse_override_file ( contents) ;
1001
+ assert ! ( matches!(
1002
+ result. unwrap_err( ) . kind( ) ,
1003
+ ErrorKind :: InvalidOverrideFile
1004
+ ) ) ;
1018
1005
}
1019
1006
1020
1007
#[ test]
1021
1008
fn parse_empty_toolchain_file ( ) {
1022
1009
let contents = "" ;
1023
1010
1024
- let ( override_file, toml_err) = Cfg :: parse_override_file ( contents) ;
1025
- assert ! ( override_file. is_none( ) ) ;
1026
- assert ! ( toml_err. is_none( ) ) ;
1011
+ let result = Cfg :: parse_override_file ( contents) ;
1012
+ assert ! ( matches!(
1013
+ result. unwrap_err( ) . kind( ) ,
1014
+ ErrorKind :: EmptyOverrideFile
1015
+ ) ) ;
1016
+ }
1017
+
1018
+ #[ test]
1019
+ fn parse_whitespace_toolchain_file ( ) {
1020
+ let contents = " " ;
1021
+
1022
+ let result = Cfg :: parse_override_file ( contents) ;
1023
+ assert ! ( matches!(
1024
+ result. unwrap_err( ) . kind( ) ,
1025
+ ErrorKind :: EmptyOverrideFile
1026
+ ) ) ;
1027
1027
}
1028
1028
1029
1029
#[ test]
@@ -1032,16 +1032,10 @@ components = [ "rustfmt" ]
1032
1032
channel = nightly
1033
1033
"# ;
1034
1034
1035
- let ( override_file, toml_err) = Cfg :: parse_override_file ( contents) ;
1036
- assert_eq ! (
1037
- override_file,
1038
- Some ( OverrideFile {
1039
- toolchain: ToolchainSection {
1040
- channel: Some ( "[toolchain]" . into( ) ) ,
1041
- ..Default :: default ( )
1042
- }
1043
- } )
1044
- ) ;
1045
- assert ! ( toml_err. is_some( ) ) ;
1035
+ let result = Cfg :: parse_override_file ( contents) ;
1036
+ assert ! ( matches!(
1037
+ result. unwrap_err( ) . kind( ) ,
1038
+ ErrorKind :: ParsingOverrideFile ( ..)
1039
+ ) ) ;
1046
1040
}
1047
1041
}
0 commit comments