summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-11-11 16:51:12 +0000
committerbors <bors@rust-lang.org>2019-11-11 16:51:12 +0000
commit9544ba10294d4959b0a761c04d86ef8ceeba3838 (patch)
treedbdd23fad66b796a9fefc04174b50566a7d269a1 /src
parent9a9007abae86c3b1b1c09778a6544ced54ea4453 (diff)
parent1a7bb5a4005e66665d2618b20bea132f009b79f9 (diff)
Auto merge of #231 - rust-lang:refactor-hints, r=jrvidal
Refactor hints Breaking change. This removes hints from the end of files, and puts them into `info.toml`. You can now access hints using: ``` rustlings hint <exerciseName> ``` ALSO this changes the exercise system to index by name for `run` and `hint`, so: ``` rustlings run exercises/if/if1.rs ``` becomes ``` rustlings run if1 ```
Diffstat (limited to 'src')
-rw-r--r--src/exercise.rs8
-rw-r--r--src/main.rs37
2 files changed, 32 insertions, 13 deletions
diff --git a/src/exercise.rs b/src/exercise.rs
index f27b545..b6c28da 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -28,8 +28,10 @@ pub struct ExerciseList {
#[derive(Deserialize)]
pub struct Exercise {
+ pub name: String,
pub path: PathBuf,
pub mode: Mode,
+ pub hint: String,
}
#[derive(PartialEq, Debug)]
@@ -128,8 +130,10 @@ 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,
+ hint: String::from(""),
};
exercise.clean();
assert!(!Path::new(&temp_file()).exists());
@@ -138,8 +142,10 @@ mod test {
#[test]
fn test_pending_state() {
let exercise = Exercise {
+ name: "pending_exercise".into(),
path: PathBuf::from("tests/fixture/state/pending_exercise.rs"),
mode: Mode::Compile,
+ hint: String::new(),
};
let state = exercise.state();
@@ -177,8 +183,10 @@ mod test {
#[test]
fn test_finished_exercise() {
let exercise = Exercise {
+ name: "finished_exercise".into(),
path: PathBuf::from("tests/fixture/state/finished_exercise.rs"),
mode: Mode::Compile,
+ hint: String::new(),
};
assert_eq!(exercise.state(), State::Done);
diff --git a/src/main.rs b/src/main.rs
index e42dff4..d1d0d6d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -25,8 +25,13 @@ 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)),
+ )
+ .subcommand(
+ SubCommand::with_name("hint")
+ .alias("h")
+ .about("Returns a hint for the current exercise")
+ .arg(Arg::with_name("name").required(true).index(1)),
)
.get_matches();
@@ -55,26 +60,32 @@ 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!");
- std::process::exit(1);
- });
+ let name = matches.value_of("name").unwrap();
- let matching_exercise = |e: &&Exercise| {
- Path::new(filename)
- .canonicalize()
- .map(|p| p.ends_with(&e.path))
- .unwrap_or(false)
- };
+ let matching_exercise = |e: &&Exercise| 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)
});
run(&exercise).unwrap_or_else(|_| std::process::exit(1));
}
+ if let Some(ref matches) = matches.subcommand_matches("hint") {
+ let name = matches.value_of("name").unwrap();
+
+ let exercise = exercises
+ .iter()
+ .find(|e| name == e.name)
+ .unwrap_or_else(|| {
+ println!("No exercise found for your given name!");
+ std::process::exit(1)
+ });
+
+ println!("{}", exercise.hint);
+ }
+
if matches.subcommand_matches("verify").is_some() {
verify(&exercises).unwrap_or_else(|_| std::process::exit(1));
}