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:
parent
a47a62172a
commit
627cdc07d0
51
info.toml
51
info.toml
|
@ -1,241 +1,292 @@
|
||||||
# VARIABLES
|
# VARIABLES
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "variables1"
|
||||||
path = "exercises/variables/variables1.rs"
|
path = "exercises/variables/variables1.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "variables2"
|
||||||
path = "exercises/variables/variables2.rs"
|
path = "exercises/variables/variables2.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "variables3"
|
||||||
path = "exercises/variables/variables3.rs"
|
path = "exercises/variables/variables3.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "variables4"
|
||||||
path = "exercises/variables/variables4.rs"
|
path = "exercises/variables/variables4.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
# IF
|
# IF
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "if1"
|
||||||
path = "exercises/if/if1.rs"
|
path = "exercises/if/if1.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
# FUNCTIONS
|
# FUNCTIONS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "functions1"
|
||||||
path = "exercises/functions/functions1.rs"
|
path = "exercises/functions/functions1.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "functions2"
|
||||||
path = "exercises/functions/functions2.rs"
|
path = "exercises/functions/functions2.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "functions3"
|
||||||
path = "exercises/functions/functions3.rs"
|
path = "exercises/functions/functions3.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "functions4"
|
||||||
path = "exercises/functions/functions4.rs"
|
path = "exercises/functions/functions4.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "functions5"
|
||||||
path = "exercises/functions/functions5.rs"
|
path = "exercises/functions/functions5.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
# TEST 1
|
# TEST 1
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "test1"
|
||||||
path = "exercises/test1.rs"
|
path = "exercises/test1.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
# PRIMITIVE TYPES
|
# PRIMITIVE TYPES
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "primitive_types1"
|
||||||
path = "exercises/primitive_types/primitive_types1.rs"
|
path = "exercises/primitive_types/primitive_types1.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "primitive_types2"
|
||||||
path = "exercises/primitive_types/primitive_types2.rs"
|
path = "exercises/primitive_types/primitive_types2.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "primitive_types3"
|
||||||
path = "exercises/primitive_types/primitive_types3.rs"
|
path = "exercises/primitive_types/primitive_types3.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "primitive_types4"
|
||||||
path = "exercises/primitive_types/primitive_types4.rs"
|
path = "exercises/primitive_types/primitive_types4.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "primitive_types5"
|
||||||
path = "exercises/primitive_types/primitive_types5.rs"
|
path = "exercises/primitive_types/primitive_types5.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "primitive_types6"
|
||||||
path = "exercises/primitive_types/primitive_types6.rs"
|
path = "exercises/primitive_types/primitive_types6.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
# STRUCTS
|
# STRUCTS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "structs1"
|
||||||
path = "exercises/structs/structs1.rs"
|
path = "exercises/structs/structs1.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "structs2"
|
||||||
path = "exercises/structs/structs2.rs"
|
path = "exercises/structs/structs2.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
# STRINGS
|
# STRINGS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "strings1"
|
||||||
path = "exercises/strings/strings1.rs"
|
path = "exercises/strings/strings1.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "strings2"
|
||||||
path = "exercises/strings/strings2.rs"
|
path = "exercises/strings/strings2.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
# TEST 2
|
# TEST 2
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "test2"
|
||||||
path = "exercises/test2.rs"
|
path = "exercises/test2.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
# ENUMS
|
# ENUMS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "enums1"
|
||||||
path = "exercises/enums/enums1.rs"
|
path = "exercises/enums/enums1.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "enums2"
|
||||||
path = "exercises/enums/enums2.rs"
|
path = "exercises/enums/enums2.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "enums3"
|
||||||
path = "exercises/enums/enums3.rs"
|
path = "exercises/enums/enums3.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
# TESTS
|
# TESTS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "tests1"
|
||||||
path = "exercises/tests/tests1.rs"
|
path = "exercises/tests/tests1.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "tests2"
|
||||||
path = "exercises/tests/tests2.rs"
|
path = "exercises/tests/tests2.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "tests3"
|
||||||
path = "exercises/tests/tests3.rs"
|
path = "exercises/tests/tests3.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
# TEST 3
|
# TEST 3
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "test3"
|
||||||
path = "exercises/test3.rs"
|
path = "exercises/test3.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
# MODULES
|
# MODULES
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "modules1"
|
||||||
path = "exercises/modules/modules1.rs"
|
path = "exercises/modules/modules1.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "modules2"
|
||||||
path = "exercises/modules/modules2.rs"
|
path = "exercises/modules/modules2.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
# MACROS
|
# MACROS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "macros1"
|
||||||
path = "exercises/macros/macros1.rs"
|
path = "exercises/macros/macros1.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "macros2"
|
||||||
path = "exercises/macros/macros2.rs"
|
path = "exercises/macros/macros2.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "macros3"
|
||||||
path = "exercises/macros/macros3.rs"
|
path = "exercises/macros/macros3.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "macros4"
|
||||||
path = "exercises/macros/macros4.rs"
|
path = "exercises/macros/macros4.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
# TEST 4
|
# TEST 4
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "test4"
|
||||||
path = "exercises/test4.rs"
|
path = "exercises/test4.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
# MOVE SEMANTICS
|
# MOVE SEMANTICS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "move_semantics1"
|
||||||
path = "exercises/move_semantics/move_semantics1.rs"
|
path = "exercises/move_semantics/move_semantics1.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "move_semantics2"
|
||||||
path = "exercises/move_semantics/move_semantics2.rs"
|
path = "exercises/move_semantics/move_semantics2.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "move_semantics3"
|
||||||
path = "exercises/move_semantics/move_semantics3.rs"
|
path = "exercises/move_semantics/move_semantics3.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "move_semantics4"
|
||||||
path = "exercises/move_semantics/move_semantics4.rs"
|
path = "exercises/move_semantics/move_semantics4.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
# ERROR HANDLING
|
# ERROR HANDLING
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "errors1"
|
||||||
path = "exercises/error_handling/errors1.rs"
|
path = "exercises/error_handling/errors1.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "errors2"
|
||||||
path = "exercises/error_handling/errors2.rs"
|
path = "exercises/error_handling/errors2.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "errors3"
|
||||||
path = "exercises/error_handling/errors3.rs"
|
path = "exercises/error_handling/errors3.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "errors4"
|
||||||
path = "exercises/error_handling/errorsn.rs"
|
path = "exercises/error_handling/errorsn.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
# OPTIONS / RESULTS
|
# OPTIONS / RESULTS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "option1"
|
||||||
path = "exercises/error_handling/option1.rs"
|
path = "exercises/error_handling/option1.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "option2"
|
||||||
path = "exercises/error_handling/result1.rs"
|
path = "exercises/error_handling/result1.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
# STANDARD LIBRARY TYPES
|
# STANDARD LIBRARY TYPES
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "arc1"
|
||||||
path = "exercises/standard_library_types/arc1.rs"
|
path = "exercises/standard_library_types/arc1.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "iterators2"
|
||||||
path = "exercises/standard_library_types/iterators2.rs"
|
path = "exercises/standard_library_types/iterators2.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "iterators3"
|
||||||
path = "exercises/standard_library_types/iterators3.rs"
|
path = "exercises/standard_library_types/iterators3.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "iterators4"
|
||||||
path = "exercises/standard_library_types/iterators4.rs"
|
path = "exercises/standard_library_types/iterators4.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
||||||
# THREADS
|
# THREADS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "threads1"
|
||||||
path = "exercises/threads/threads1.rs"
|
path = "exercises/threads/threads1.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
|
@ -24,6 +24,7 @@ pub struct ExerciseList {
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct Exercise {
|
pub struct Exercise {
|
||||||
|
pub name: String,
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub mode: Mode,
|
pub mode: Mode,
|
||||||
}
|
}
|
||||||
|
@ -70,6 +71,7 @@ mod test {
|
||||||
fn test_clean() {
|
fn test_clean() {
|
||||||
File::create(&temp_file()).unwrap();
|
File::create(&temp_file()).unwrap();
|
||||||
let exercise = Exercise {
|
let exercise = Exercise {
|
||||||
|
name: String::from("example"),
|
||||||
path: PathBuf::from("example.rs"),
|
path: PathBuf::from("example.rs"),
|
||||||
mode: Mode::Test,
|
mode: Mode::Test,
|
||||||
};
|
};
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -25,8 +25,7 @@ fn main() {
|
||||||
SubCommand::with_name("run")
|
SubCommand::with_name("run")
|
||||||
.alias("r")
|
.alias("r")
|
||||||
.about("Runs/Tests a single exercise")
|
.about("Runs/Tests a single exercise")
|
||||||
.arg(Arg::with_name("file").required(true).index(1))
|
.arg(Arg::with_name("name").required(true).index(1)),
|
||||||
.arg(Arg::with_name("test").short("t").long("test").help("Run the file as a test")),
|
|
||||||
)
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
|
@ -55,20 +54,17 @@ fn main() {
|
||||||
let exercises = toml::from_str::<ExerciseList>(toml_str).unwrap().exercises;
|
let exercises = toml::from_str::<ExerciseList>(toml_str).unwrap().exercises;
|
||||||
|
|
||||||
if let Some(ref matches) = matches.subcommand_matches("run") {
|
if let Some(ref matches) = matches.subcommand_matches("run") {
|
||||||
let filename = matches.value_of("file").unwrap_or_else(|| {
|
let name = matches.value_of("name").unwrap_or_else(|| {
|
||||||
println!("Please supply a file name!");
|
println!("Please supply an exercise name!");
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
let matching_exercise = |e: &&Exercise| {
|
let matching_exercise = |e: &&Exercise| {
|
||||||
Path::new(filename)
|
name == e.name
|
||||||
.canonicalize()
|
|
||||||
.map(|p| p.ends_with(&e.path))
|
|
||||||
.unwrap_or(false)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let exercise = exercises.iter().find(matching_exercise).unwrap_or_else(|| {
|
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)
|
std::process::exit(1)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "compFailure"
|
||||||
path = "compFailure.rs"
|
path = "compFailure.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "testFailure"
|
||||||
path = "testFailure.rs"
|
path = "testFailure.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "compSuccess"
|
||||||
path = "compSuccess.rs"
|
path = "compSuccess.rs"
|
||||||
mode = "compile"
|
mode = "compile"
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
name = "testSuccess"
|
||||||
path = "testSuccess.rs"
|
path = "testSuccess.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
|
|
|
@ -40,7 +40,7 @@ fn verify_all_failure() {
|
||||||
fn run_single_compile_success() {
|
fn run_single_compile_success() {
|
||||||
Command::cargo_bin("rustlings")
|
Command::cargo_bin("rustlings")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.args(&["r", "compSuccess.rs"])
|
.args(&["r", "compSuccess"])
|
||||||
.current_dir("tests/fixture/success/")
|
.current_dir("tests/fixture/success/")
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
|
@ -50,7 +50,7 @@ fn run_single_compile_success() {
|
||||||
fn run_single_compile_failure() {
|
fn run_single_compile_failure() {
|
||||||
Command::cargo_bin("rustlings")
|
Command::cargo_bin("rustlings")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.args(&["r", "compFailure.rs"])
|
.args(&["r", "compFailure"])
|
||||||
.current_dir("tests/fixture/failure/")
|
.current_dir("tests/fixture/failure/")
|
||||||
.assert()
|
.assert()
|
||||||
.code(1);
|
.code(1);
|
||||||
|
@ -60,7 +60,7 @@ fn run_single_compile_failure() {
|
||||||
fn run_single_test_success() {
|
fn run_single_test_success() {
|
||||||
Command::cargo_bin("rustlings")
|
Command::cargo_bin("rustlings")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.args(&["r", "testSuccess.rs"])
|
.args(&["r", "testSuccess"])
|
||||||
.current_dir("tests/fixture/success/")
|
.current_dir("tests/fixture/success/")
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
|
@ -70,7 +70,7 @@ fn run_single_test_success() {
|
||||||
fn run_single_test_failure() {
|
fn run_single_test_failure() {
|
||||||
Command::cargo_bin("rustlings")
|
Command::cargo_bin("rustlings")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.args(&["r", "testFailure.rs"])
|
.args(&["r", "testFailure"])
|
||||||
.current_dir("tests/fixture/failure/")
|
.current_dir("tests/fixture/failure/")
|
||||||
.assert()
|
.assert()
|
||||||
.code(1);
|
.code(1);
|
||||||
|
|
Loading…
Reference in New Issue