Skip to content

Commit c4c5653

Browse files
committed
x.py: setup: Refactor to centralise list of profiles
Put all()'s otuput in the order we want to print things in, and add a comment about why they are in this order. Provide purpose() and all_for_help(). Use these things everywhere. Move all the abbrev character ("a", "b", etc.) processing into interactive_path. No functional change. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
1 parent 5565241 commit c4c5653

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

src/bootstrap/flags.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,7 @@ Arguments:
551551
profile_string.parse().unwrap_or_else(|err| {
552552
eprintln!("error: {}", err);
553553
eprintln!("help: the available profiles are:");
554-
for choice in Profile::all() {
555-
eprintln!("- {}", choice);
556-
}
554+
eprint!("{}", Profile::all_for_help("- "));
557555
std::process::exit(1);
558556
})
559557
} else {

src/bootstrap/setup.rs

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{t, VERSION};
2+
use std::fmt::Write as _;
23
use std::path::{Path, PathBuf};
34
use std::str::FromStr;
45
use std::{
@@ -20,7 +21,28 @@ impl Profile {
2021
}
2122

2223
pub fn all() -> impl Iterator<Item = Self> {
23-
[Profile::Compiler, Profile::Codegen, Profile::Library, Profile::User].iter().copied()
24+
use Profile::*;
25+
// N.B. these are ordered by how they are displayed, not alphabetically
26+
[Library, Compiler, Codegen, User].iter().copied()
27+
}
28+
29+
pub fn purpose(&self) -> String {
30+
use Profile::*;
31+
match self {
32+
Library => "Contribute to the standard library",
33+
Compiler => "Contribute to the compiler or rustdoc",
34+
Codegen => "Contribute to the compiler, and also modify LLVM or codegen",
35+
User => "Install Rust from source",
36+
}
37+
.to_string()
38+
}
39+
40+
pub fn all_for_help(indent: &str) -> String {
41+
let mut out = String::new();
42+
for choice in Profile::all() {
43+
writeln!(&mut out, "{}{}", indent, choice).unwrap();
44+
}
45+
out
2446
}
2547
}
2648

@@ -29,10 +51,10 @@ impl FromStr for Profile {
2951

3052
fn from_str(s: &str) -> Result<Self, Self::Err> {
3153
match s {
32-
"a" | "lib" | "library" => Ok(Profile::Library),
33-
"b" | "compiler" | "rustdoc" => Ok(Profile::Compiler),
34-
"c" | "llvm" | "codegen" => Ok(Profile::Codegen),
35-
"d" | "maintainer" | "user" => Ok(Profile::User),
54+
"lib" | "library" => Ok(Profile::Library),
55+
"compiler" | "rustdoc" => Ok(Profile::Compiler),
56+
"llvm" | "codegen" => Ok(Profile::Codegen),
57+
"maintainer" | "user" => Ok(Profile::User),
3658
_ => Err(format!("unknown profile: '{}'", s)),
3759
}
3860
}
@@ -104,19 +126,33 @@ pub fn setup(src_path: &Path, profile: Profile) {
104126

105127
// Used to get the path for `Subcommand::Setup`
106128
pub fn interactive_path() -> io::Result<Profile> {
129+
fn abbrev_all() -> impl Iterator<Item = (String, Profile)> {
130+
('a'..).map(|c| c.to_string()).zip(Profile::all())
131+
}
132+
133+
fn parse_with_abbrev(input: &str) -> Result<Profile, String> {
134+
let input = input.trim().to_lowercase();
135+
for (letter, profile) in abbrev_all() {
136+
if input == letter {
137+
return Ok(profile);
138+
}
139+
}
140+
input.parse()
141+
}
142+
107143
let mut input = String::new();
108-
println!(
109-
"Welcome to the Rust project! What do you want to do with x.py?
110-
a) Contribute to the standard library
111-
b) Contribute to the compiler or rustdoc
112-
c) Contribute to the compiler, and also modify LLVM or codegen
113-
d) Install Rust from source"
114-
);
144+
println!("Welcome to the Rust project! What do you want to do with x.py?");
145+
for (letter, profile) in abbrev_all() {
146+
println!("{}) {}", letter, profile.purpose());
147+
}
115148
let template = loop {
116-
print!("Please choose one (a/b/c/d): ");
149+
print!(
150+
"Please choose one ({}): ",
151+
abbrev_all().map(|(l, _)| l).collect::<Vec<_>>().join("/")
152+
);
117153
io::stdout().flush()?;
118154
io::stdin().read_line(&mut input)?;
119-
break match input.trim().to_lowercase().parse() {
155+
break match parse_with_abbrev(&input) {
120156
Ok(profile) => profile,
121157
Err(err) => {
122158
println!("error: {}", err);

0 commit comments

Comments
 (0)