feat: Index exercises by name

BREAKING CHANGE: This changes the way you use `rustlings run` by now
requiring an abridged form of the previous filename, e.g:

`rustlings run exercises/if/if1.rs` becomes
`rustlings run if1`
This commit is contained in:
marisa
2019-11-11 15:46:32 +01:00
parent a47a62172a
commit 627cdc07d0
6 changed files with 68 additions and 15 deletions

View File

@@ -25,8 +25,7 @@ fn main() {
SubCommand::with_name("run")
.alias("r")
.about("Runs/Tests a single exercise")
.arg(Arg::with_name("file").required(true).index(1))
.arg(Arg::with_name("test").short("t").long("test").help("Run the file as a test")),
.arg(Arg::with_name("name").required(true).index(1)),
)
.get_matches();
@@ -55,20 +54,17 @@ fn main() {
let exercises = toml::from_str::<ExerciseList>(toml_str).unwrap().exercises;
if let Some(ref matches) = matches.subcommand_matches("run") {
let filename = matches.value_of("file").unwrap_or_else(|| {
println!("Please supply a file name!");
let name = matches.value_of("name").unwrap_or_else(|| {
println!("Please supply an exercise name!");
std::process::exit(1);
});
let matching_exercise = |e: &&Exercise| {
Path::new(filename)
.canonicalize()
.map(|p| p.ends_with(&e.path))
.unwrap_or(false)
name == e.name
};
let exercise = exercises.iter().find(matching_exercise).unwrap_or_else(|| {
println!("No exercise found for your file name!");
println!("No exercise found for your given name!");
std::process::exit(1)
});