From 627cdc07d07dfe6a740e885e0ddf6900e7ec336b Mon Sep 17 00:00:00 2001 From: marisa Date: Mon, 11 Nov 2019 15:46:32 +0100 Subject: 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` --- src/exercise.rs | 2 ++ src/main.rs | 14 +++++--------- 2 files changed, 7 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/exercise.rs b/src/exercise.rs index 6f526e7..9cd88a0 100644 --- a/src/exercise.rs +++ b/src/exercise.rs @@ -24,6 +24,7 @@ pub struct ExerciseList { #[derive(Deserialize)] pub struct Exercise { + pub name: String, pub path: PathBuf, pub mode: Mode, } @@ -70,6 +71,7 @@ mod test { fn test_clean() { File::create(&temp_file()).unwrap(); let exercise = Exercise { + name: String::from("example"), path: PathBuf::from("example.rs"), mode: Mode::Test, }; diff --git a/src/main.rs b/src/main.rs index aad4cff..100186b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::(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) }); -- cgit v1.2.3