Skip to content

Commit

Permalink
feat: Index exercises by name
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
marisa committed Nov 11, 2019
1 parent a47a621 commit 627cdc0
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
51 changes: 51 additions & 0 deletions info.toml
Original file line number Diff line number Diff line change
@@ -1,241 +1,292 @@
# VARIABLES

[[exercises]]
name = "variables1"
path = "exercises/variables/variables1.rs"
mode = "compile"

[[exercises]]
name = "variables2"
path = "exercises/variables/variables2.rs"
mode = "compile"

[[exercises]]
name = "variables3"
path = "exercises/variables/variables3.rs"
mode = "compile"

[[exercises]]
name = "variables4"
path = "exercises/variables/variables4.rs"
mode = "compile"

# IF

[[exercises]]
name = "if1"
path = "exercises/if/if1.rs"
mode = "test"

# FUNCTIONS

[[exercises]]
name = "functions1"
path = "exercises/functions/functions1.rs"
mode = "compile"

[[exercises]]
name = "functions2"
path = "exercises/functions/functions2.rs"
mode = "compile"

[[exercises]]
name = "functions3"
path = "exercises/functions/functions3.rs"
mode = "compile"

[[exercises]]
name = "functions4"
path = "exercises/functions/functions4.rs"
mode = "compile"

[[exercises]]
name = "functions5"
path = "exercises/functions/functions5.rs"
mode = "compile"

# TEST 1

[[exercises]]
name = "test1"
path = "exercises/test1.rs"
mode = "test"

# PRIMITIVE TYPES

[[exercises]]
name = "primitive_types1"
path = "exercises/primitive_types/primitive_types1.rs"
mode = "compile"

[[exercises]]
name = "primitive_types2"
path = "exercises/primitive_types/primitive_types2.rs"
mode = "compile"

[[exercises]]
name = "primitive_types3"
path = "exercises/primitive_types/primitive_types3.rs"
mode = "compile"

[[exercises]]
name = "primitive_types4"
path = "exercises/primitive_types/primitive_types4.rs"
mode = "test"

[[exercises]]
name = "primitive_types5"
path = "exercises/primitive_types/primitive_types5.rs"
mode = "compile"

[[exercises]]
name = "primitive_types6"
path = "exercises/primitive_types/primitive_types6.rs"
mode = "compile"

# STRUCTS

[[exercises]]
name = "structs1"
path = "exercises/structs/structs1.rs"
mode = "test"

[[exercises]]
name = "structs2"
path = "exercises/structs/structs2.rs"
mode = "test"

# STRINGS

[[exercises]]
name = "strings1"
path = "exercises/strings/strings1.rs"
mode = "compile"

[[exercises]]
name = "strings2"
path = "exercises/strings/strings2.rs"
mode = "compile"

# TEST 2

[[exercises]]
name = "test2"
path = "exercises/test2.rs"
mode = "compile"

# ENUMS

[[exercises]]
name = "enums1"
path = "exercises/enums/enums1.rs"
mode = "compile"

[[exercises]]
name = "enums2"
path = "exercises/enums/enums2.rs"
mode = "compile"

[[exercises]]
name = "enums3"
path = "exercises/enums/enums3.rs"
mode = "test"

# TESTS

[[exercises]]
name = "tests1"
path = "exercises/tests/tests1.rs"
mode = "test"

[[exercises]]
name = "tests2"
path = "exercises/tests/tests2.rs"
mode = "test"

[[exercises]]
name = "tests3"
path = "exercises/tests/tests3.rs"
mode = "test"

# TEST 3

[[exercises]]
name = "test3"
path = "exercises/test3.rs"
mode = "test"

# MODULES

[[exercises]]
name = "modules1"
path = "exercises/modules/modules1.rs"
mode = "compile"

[[exercises]]
name = "modules2"
path = "exercises/modules/modules2.rs"
mode = "compile"

# MACROS

[[exercises]]
name = "macros1"
path = "exercises/macros/macros1.rs"
mode = "compile"

[[exercises]]
name = "macros2"
path = "exercises/macros/macros2.rs"
mode = "compile"

[[exercises]]
name = "macros3"
path = "exercises/macros/macros3.rs"
mode = "compile"

[[exercises]]
name = "macros4"
path = "exercises/macros/macros4.rs"
mode = "compile"

# TEST 4

[[exercises]]
name = "test4"
path = "exercises/test4.rs"
mode = "compile"

# MOVE SEMANTICS

[[exercises]]
name = "move_semantics1"
path = "exercises/move_semantics/move_semantics1.rs"
mode = "compile"

[[exercises]]
name = "move_semantics2"
path = "exercises/move_semantics/move_semantics2.rs"
mode = "compile"

[[exercises]]
name = "move_semantics3"
path = "exercises/move_semantics/move_semantics3.rs"
mode = "compile"

[[exercises]]
name = "move_semantics4"
path = "exercises/move_semantics/move_semantics4.rs"
mode = "compile"

# ERROR HANDLING

[[exercises]]
name = "errors1"
path = "exercises/error_handling/errors1.rs"
mode = "test"

[[exercises]]
name = "errors2"
path = "exercises/error_handling/errors2.rs"
mode = "test"

[[exercises]]
name = "errors3"
path = "exercises/error_handling/errors3.rs"
mode = "test"

[[exercises]]
name = "errors4"
path = "exercises/error_handling/errorsn.rs"
mode = "test"

# OPTIONS / RESULTS

[[exercises]]
name = "option1"
path = "exercises/error_handling/option1.rs"
mode = "test"

[[exercises]]
name = "option2"
path = "exercises/error_handling/result1.rs"
mode = "test"

# STANDARD LIBRARY TYPES

[[exercises]]
name = "arc1"
path = "exercises/standard_library_types/arc1.rs"
mode = "compile"

[[exercises]]
name = "iterators2"
path = "exercises/standard_library_types/iterators2.rs"
mode = "test"

[[exercises]]
name = "iterators3"
path = "exercises/standard_library_types/iterators3.rs"
mode = "test"

[[exercises]]
name = "iterators4"
path = "exercises/standard_library_types/iterators4.rs"
mode = "test"

# THREADS

[[exercises]]
name = "threads1"
path = "exercises/threads/threads1.rs"
mode = "compile"
2 changes: 2 additions & 0 deletions src/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct ExerciseList {

#[derive(Deserialize)]
pub struct Exercise {
pub name: String,
pub path: PathBuf,
pub mode: Mode,
}
Expand Down Expand Up @@ -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,
};
Expand Down
14 changes: 5 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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)
});

Expand Down
4 changes: 3 additions & 1 deletion tests/fixture/failure/info.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[[exercises]]
name = "compFailure"
path = "compFailure.rs"
mode = "compile"

[[exercises]]
name = "testFailure"
path = "testFailure.rs"
mode = "test"
mode = "test"
4 changes: 3 additions & 1 deletion tests/fixture/success/info.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[[exercises]]
name = "compSuccess"
path = "compSuccess.rs"
mode = "compile"

[[exercises]]
name = "testSuccess"
path = "testSuccess.rs"
mode = "test"
mode = "test"
Loading

0 comments on commit 627cdc0

Please sign in to comment.