1
1
use crate :: t;
2
2
use std:: path:: { Path , PathBuf } ;
3
+ use std:: str:: FromStr ;
3
4
use std:: {
4
- env, fs,
5
+ env, fmt , fs,
5
6
io:: { self , Write } ,
6
7
} ;
7
8
8
- pub fn setup ( src_path : & Path , include_name : & str ) {
9
+ #[ derive( Clone , Copy , Eq , PartialEq ) ]
10
+ pub enum Profile {
11
+ Compiler ,
12
+ Codegen ,
13
+ Library ,
14
+ User ,
15
+ }
16
+
17
+ impl Profile {
18
+ fn include_path ( & self , src_path : & Path ) -> PathBuf {
19
+ PathBuf :: from ( format ! ( "{}/src/bootstrap/defaults/config.{}.toml" , src_path. display( ) , self ) )
20
+ }
21
+ }
22
+
23
+ #[ derive( Debug ) ]
24
+ pub struct ProfileErr {
25
+ pub name : String ,
26
+ }
27
+
28
+ impl FromStr for Profile {
29
+ type Err = ProfileErr ;
30
+
31
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
32
+ match s {
33
+ "a" | "lib" | "library" => Ok ( Profile :: Library ) ,
34
+ "b" | "compiler" => Ok ( Profile :: Compiler ) ,
35
+ "c" | "llvm" | "codegen" => Ok ( Profile :: Codegen ) ,
36
+ "d" | "maintainer" | "user" => Ok ( Profile :: User ) ,
37
+ _ => Err ( ProfileErr { name : s. to_string ( ) } ) ,
38
+ }
39
+ }
40
+ }
41
+
42
+ impl fmt:: Display for Profile {
43
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
44
+ match self {
45
+ Profile :: Compiler => write ! ( f, "compiler" ) ,
46
+ Profile :: Codegen => write ! ( f, "codegen" ) ,
47
+ Profile :: Library => write ! ( f, "library" ) ,
48
+ Profile :: User => write ! ( f, "user" ) ,
49
+ }
50
+ }
51
+ }
52
+
53
+ pub fn setup ( src_path : & Path , profile : Profile ) {
9
54
let cfg_file = env:: var_os ( "BOOTSTRAP_CONFIG" ) . map ( PathBuf :: from) ;
10
55
11
56
if cfg_file. as_ref ( ) . map_or ( false , |f| f. exists ( ) ) {
@@ -14,15 +59,10 @@ pub fn setup(src_path: &Path, include_name: &str) {
14
59
"error: you asked `x.py` to setup a new config file, but one already exists at `{}`" ,
15
60
file. display( )
16
61
) ;
62
+ println ! ( "help: try adding `profile = \" {}\" ` at the top of {}" , profile, file. display( ) ) ;
17
63
println ! (
18
- "help: try adding `profile = \" {}\" ` at the top of {}" ,
19
- include_name,
20
- file. display( )
21
- ) ;
22
- println ! (
23
- "note: this will use the configuration in {}/src/bootstrap/defaults/config.{}.toml" ,
24
- src_path. display( ) ,
25
- include_name
64
+ "note: this will use the configuration in {}" ,
65
+ profile. include_path( src_path) . display( )
26
66
) ;
27
67
std:: process:: exit ( 1 ) ;
28
68
}
@@ -31,35 +71,33 @@ pub fn setup(src_path: &Path, include_name: &str) {
31
71
let settings = format ! (
32
72
"# Includes one of the default files in src/bootstrap/defaults\n \
33
73
profile = \" {}\" \n ",
34
- include_name
74
+ profile
35
75
) ;
36
76
t ! ( fs:: write( path, settings) ) ;
37
77
38
- let include_path =
39
- format ! ( "{}/src/bootstrap/defaults/config.{}.toml" , src_path. display( ) , include_name) ;
40
- println ! ( "`x.py` will now use the configuration at {}" , include_path) ;
78
+ let include_path = profile. include_path ( src_path) ;
79
+ println ! ( "`x.py` will now use the configuration at {}" , include_path. display( ) ) ;
41
80
42
- let suggestions = match include_name {
43
- "llvm" | "codegen" | "compiler" => & [ "check" , "build" , "test" ] [ ..] ,
44
- "library" => & [ "check" , "build" , "test library/std" , "doc" ] ,
45
- "maintainer" | "user" => & [ "dist" , "build" ] ,
46
- _ => return ,
81
+ let suggestions = match profile {
82
+ Profile :: Codegen | Profile :: Compiler => & [ "check" , "build" , "test" ] [ ..] ,
83
+ Profile :: Library => & [ "check" , "build" , "test library/std" , "doc" ] ,
84
+ Profile :: User => & [ "dist" , "build" ] ,
47
85
} ;
48
86
49
87
println ! ( "To get started, try one of the following commands:" ) ;
50
88
for cmd in suggestions {
51
89
println ! ( "- `x.py {}`" , cmd) ;
52
90
}
53
91
54
- if include_name != "user" {
92
+ if profile != Profile :: User {
55
93
println ! (
56
94
"For more suggestions, see https://rustc-dev-guide.rust-lang.org/building/suggested.html"
57
95
) ;
58
96
}
59
97
}
60
98
61
99
// Used to get the path for `Subcommand::Setup`
62
- pub fn interactive_path ( ) -> io:: Result < String > {
100
+ pub fn interactive_path ( ) -> io:: Result < Profile > {
63
101
let mut input = String :: new ( ) ;
64
102
println ! (
65
103
"Welcome to the Rust project! What do you want to do with x.py?
@@ -72,17 +110,14 @@ d) Install Rust from source"
72
110
print ! ( "Please choose one (a/b/c/d): " ) ;
73
111
io:: stdout ( ) . flush ( ) ?;
74
112
io:: stdin ( ) . read_line ( & mut input) ?;
75
- break match input. trim ( ) . to_lowercase ( ) . as_str ( ) {
76
- "a" | "lib" | "library" => "library" ,
77
- "b" | "compiler" => "compiler" ,
78
- "c" | "llvm" => "llvm" ,
79
- "d" | "user" | "maintainer" => "maintainer" ,
80
- _ => {
81
- println ! ( "error: unrecognized option '{}'" , input. trim( ) ) ;
113
+ break match input. trim ( ) . to_lowercase ( ) . parse ( ) {
114
+ Ok ( profile) => profile,
115
+ Err ( ProfileErr { name } ) => {
116
+ println ! ( "error: unrecognized option '{}'" , name) ;
82
117
println ! ( "note: press Ctrl+C to exit" ) ;
83
118
continue ;
84
119
}
85
120
} ;
86
121
} ;
87
- Ok ( template. to_owned ( ) )
122
+ Ok ( template)
88
123
}
0 commit comments