commit
b9720d92e6
|
@ -673,6 +673,16 @@
|
||||||
"contributions": [
|
"contributions": [
|
||||||
"content"
|
"content"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"login": "jfchevrette",
|
||||||
|
"name": "Jean-Francois Chevrette",
|
||||||
|
"avatar_url": "https://avatars.githubusercontent.com/u/3001?v=4",
|
||||||
|
"profile": "https://github.com/jfchevrette",
|
||||||
|
"contributions": [
|
||||||
|
"content",
|
||||||
|
"code"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"contributorsPerLine": 8,
|
"contributorsPerLine": 8,
|
||||||
|
|
12
README.md
12
README.md
|
@ -1,5 +1,5 @@
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||||
[](#contributors-)
|
[](#contributors-)
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||||
|
|
||||||
# rustlings 🦀❤️
|
# rustlings 🦀❤️
|
||||||
|
@ -112,13 +112,6 @@ After every couple of sections, there will be a quiz that'll test your knowledge
|
||||||
|
|
||||||
Once you've completed Rustlings, put your new knowledge to good use! Continue practicing your Rust skills by building your own projects, contributing to Rustlings, or finding other open-source projects to contribute to.
|
Once you've completed Rustlings, put your new knowledge to good use! Continue practicing your Rust skills by building your own projects, contributing to Rustlings, or finding other open-source projects to contribute to.
|
||||||
|
|
||||||
If you'd like to uninstall Rustlings, you can do so by invoking cargo and removing the rustlings directory:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cargo uninstall rustlings
|
|
||||||
rm -r rustlings/ # or on Windows: rmdir /s rustlings
|
|
||||||
```
|
|
||||||
|
|
||||||
## Uninstalling Rustlings
|
## Uninstalling Rustlings
|
||||||
|
|
||||||
If you want to remove Rustlings from your system, there's two steps. First, you'll need to remove the exercises folder that the install script created
|
If you want to remove Rustlings from your system, there's two steps. First, you'll need to remove the exercises folder that the install script created
|
||||||
|
@ -257,6 +250,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
||||||
<td align="center"><a href="http://willhayworth.com"><img src="https://avatars3.githubusercontent.com/u/181174?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Will Hayworth</b></sub></a><br /><a href="#content-wsh" title="Content">🖋</a></td>
|
<td align="center"><a href="http://willhayworth.com"><img src="https://avatars3.githubusercontent.com/u/181174?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Will Hayworth</b></sub></a><br /><a href="#content-wsh" title="Content">🖋</a></td>
|
||||||
<td align="center"><a href="https://github.com/chrizel"><img src="https://avatars3.githubusercontent.com/u/20802?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Christian Zeller</b></sub></a><br /><a href="#content-chrizel" title="Content">🖋</a></td>
|
<td align="center"><a href="https://github.com/chrizel"><img src="https://avatars3.githubusercontent.com/u/20802?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Christian Zeller</b></sub></a><br /><a href="#content-chrizel" title="Content">🖋</a></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td align="center"><a href="https://github.com/jfchevrette"><img src="https://avatars.githubusercontent.com/u/3001?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jean-Francois Chevrette</b></sub></a><br /><a href="#content-jfchevrette" title="Content">🖋</a> <a href="https://github.com/rust-lang/rustlings/commits?author=jfchevrette" title="Code">💻</a></td>
|
||||||
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<!-- markdownlint-restore -->
|
<!-- markdownlint-restore -->
|
||||||
|
|
|
@ -11,15 +11,17 @@ struct Person {
|
||||||
}
|
}
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
// Steps:
|
// Steps:
|
||||||
// 1. If the length of the provided string is 0, then return an error
|
// 1. If the length of the provided string is 0 an error should be returned
|
||||||
// 2. Split the given string on the commas present in it
|
// 2. Split the given string on the commas present in it
|
||||||
// 3. Extract the first element from the split operation and use it as the name
|
// 3. Only 2 elements should returned from the split, otherwise return an error
|
||||||
// 4. If the name is empty, then return an error
|
// 4. Extract the first element from the split operation and use it as the name
|
||||||
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
|
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
|
||||||
// with something like `"4".parse::<usize>()`.
|
// with something like `"4".parse::<usize>()`.
|
||||||
// If while parsing the age, something goes wrong, then return an error
|
// 5. If while extracting the name and the age something goes wrong an error should be returned
|
||||||
// Otherwise, then return a Result of a Person object
|
// If everything goes well, then return a Result of a Person object
|
||||||
|
|
||||||
impl FromStr for Person {
|
impl FromStr for Person {
|
||||||
type Err = String;
|
type Err = String;
|
||||||
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
||||||
|
@ -48,50 +50,42 @@ mod tests {
|
||||||
assert_eq!(p.age, 32);
|
assert_eq!(p.age, 32);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn missing_age() {
|
fn missing_age() {
|
||||||
"John,".parse::<Person>().unwrap();
|
assert!("John,".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn invalid_age() {
|
fn invalid_age() {
|
||||||
"John,twenty".parse::<Person>().unwrap();
|
assert!("John,twenty".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn missing_comma_and_age() {
|
fn missing_comma_and_age() {
|
||||||
"John".parse::<Person>().unwrap();
|
assert!("John".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn missing_name() {
|
fn missing_name() {
|
||||||
",1".parse::<Person>().unwrap();
|
assert!(",1".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn missing_name_and_age() {
|
fn missing_name_and_age() {
|
||||||
",".parse::<Person>().unwrap();
|
assert!(",".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn missing_name_and_invalid_age() {
|
fn missing_name_and_invalid_age() {
|
||||||
",one".parse::<Person>().unwrap();
|
assert!(",one".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn trailing_comma() {
|
fn trailing_comma() {
|
||||||
"John,32,".parse::<Person>().unwrap();
|
assert!("John,32,".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn trailing_comma_and_some_string() {
|
fn trailing_comma_and_some_string() {
|
||||||
"John,32,man".parse::<Person>().unwrap();
|
assert!("John,32,man".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -232,6 +232,16 @@ path = "{}.rs""#,
|
||||||
|
|
||||||
State::Pending(context)
|
State::Pending(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check that the exercise looks to be solved using self.state()
|
||||||
|
// This is not the best way to check since
|
||||||
|
// the user can just remove the "I AM NOT DONE" string fromm the file
|
||||||
|
// without actually having solved anything.
|
||||||
|
// The only other way to truly check this would to compile and run
|
||||||
|
// the exercise; which would be both costly and counterintuitive
|
||||||
|
pub fn looks_done(&self) -> bool {
|
||||||
|
self.state() == State::Done
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Exercise {
|
impl Display for Exercise {
|
||||||
|
|
102
src/main.rs
102
src/main.rs
|
@ -7,7 +7,7 @@ use notify::DebouncedEvent;
|
||||||
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
|
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
use std::io::{self, prelude::*};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::channel;
|
||||||
|
@ -58,6 +58,45 @@ fn main() {
|
||||||
SubCommand::with_name("list")
|
SubCommand::with_name("list")
|
||||||
.alias("l")
|
.alias("l")
|
||||||
.about("Lists the exercises available in rustlings")
|
.about("Lists the exercises available in rustlings")
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("paths")
|
||||||
|
.long("paths")
|
||||||
|
.short("p")
|
||||||
|
.conflicts_with("names")
|
||||||
|
.help("Show only the paths of the exercises")
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("names")
|
||||||
|
.long("names")
|
||||||
|
.short("n")
|
||||||
|
.conflicts_with("paths")
|
||||||
|
.help("Show only the names of the exercises")
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("filter")
|
||||||
|
.long("filter")
|
||||||
|
.short("f")
|
||||||
|
.takes_value(true)
|
||||||
|
.empty_values(false)
|
||||||
|
.help(
|
||||||
|
"Provide a string to match the exercise names.\
|
||||||
|
\nComma separated patterns are acceptable."
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("unsolved")
|
||||||
|
.long("unsolved")
|
||||||
|
.short("u")
|
||||||
|
.conflicts_with("solved")
|
||||||
|
.help("Display only exercises not yet solved")
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("solved")
|
||||||
|
.long("solved")
|
||||||
|
.short("s")
|
||||||
|
.conflicts_with("unsolved")
|
||||||
|
.help("Display only exercises that have been solved")
|
||||||
|
)
|
||||||
)
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
|
@ -93,9 +132,51 @@ fn main() {
|
||||||
let exercises = toml::from_str::<ExerciseList>(toml_str).unwrap().exercises;
|
let exercises = toml::from_str::<ExerciseList>(toml_str).unwrap().exercises;
|
||||||
let verbose = matches.is_present("nocapture");
|
let verbose = matches.is_present("nocapture");
|
||||||
|
|
||||||
if matches.subcommand_matches("list").is_some() {
|
// Handle the list command
|
||||||
exercises.iter().for_each(|e| println!("{}", e.name));
|
if let Some(list_m) = matches.subcommand_matches("list") {
|
||||||
|
if ["paths", "names"].iter().all(|k| !list_m.is_present(k)) {
|
||||||
|
println!("{:<17}\t{:<46}\t{:<7}", "Name", "Path", "Status");
|
||||||
}
|
}
|
||||||
|
let filters = list_m.value_of("filter").unwrap_or_default().to_lowercase();
|
||||||
|
exercises.iter().for_each(|e| {
|
||||||
|
let fname = format!("{}", e.path.display());
|
||||||
|
let filter_cond = filters
|
||||||
|
.split(',')
|
||||||
|
.filter(|f| f.trim().len() > 0)
|
||||||
|
.any(|f| e.name.contains(&f) || fname.contains(&f));
|
||||||
|
let status = if e.looks_done() { "Done" } else { "Pending" };
|
||||||
|
let solve_cond = {
|
||||||
|
(e.looks_done() && list_m.is_present("solved"))
|
||||||
|
|| (!e.looks_done() && list_m.is_present("unsolved"))
|
||||||
|
|| (!list_m.is_present("solved") && !list_m.is_present("unsolved"))
|
||||||
|
};
|
||||||
|
if solve_cond && (filter_cond || !list_m.is_present("filter")) {
|
||||||
|
let line = if list_m.is_present("paths") {
|
||||||
|
format!("{}\n", fname)
|
||||||
|
} else if list_m.is_present("names") {
|
||||||
|
format!("{}\n", e.name)
|
||||||
|
} else {
|
||||||
|
format!("{:<17}\t{:<46}\t{:<7}\n", e.name, fname, status)
|
||||||
|
};
|
||||||
|
// Somehow using println! leads to the binary panicking
|
||||||
|
// when its output is piped.
|
||||||
|
// So, we're handling a Broken Pipe error and exiting with 0 anyway
|
||||||
|
let stdout = std::io::stdout();
|
||||||
|
{
|
||||||
|
let mut handle = stdout.lock();
|
||||||
|
handle.write_all(line.as_bytes()).unwrap_or_else(|e| {
|
||||||
|
match e.kind() {
|
||||||
|
std::io::ErrorKind::BrokenPipe => std::process::exit(0),
|
||||||
|
_ => std::process::exit(1),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle the run command
|
||||||
if let Some(ref matches) = matches.subcommand_matches("run") {
|
if let Some(ref matches) = matches.subcommand_matches("run") {
|
||||||
let name = matches.value_of("name").unwrap();
|
let name = matches.value_of("name").unwrap();
|
||||||
|
|
||||||
|
@ -123,13 +204,18 @@ fn main() {
|
||||||
println!("{}", exercise.hint);
|
println!("{}", exercise.hint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle the verify command
|
||||||
if matches.subcommand_matches("verify").is_some() {
|
if matches.subcommand_matches("verify").is_some() {
|
||||||
verify(&exercises, verbose).unwrap_or_else(|_| std::process::exit(1));
|
verify(&exercises, verbose).unwrap_or_else(|_| std::process::exit(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle the watch command
|
||||||
if matches.subcommand_matches("watch").is_some() {
|
if matches.subcommand_matches("watch").is_some() {
|
||||||
if let Err(e) = watch(&exercises, verbose) {
|
if let Err(e) = watch(&exercises, verbose) {
|
||||||
println!("Error: Could not watch your progess. Error message was {:?}.", e);
|
println!(
|
||||||
|
"Error: Could not watch your progess. Error message was {:?}.",
|
||||||
|
e
|
||||||
|
);
|
||||||
println!("Most likely you've run out of disk space or your 'inotify limit' has been reached.");
|
println!("Most likely you've run out of disk space or your 'inotify limit' has been reached.");
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
|
@ -223,7 +309,13 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<()> {
|
||||||
let filepath = b.as_path().canonicalize().unwrap();
|
let filepath = b.as_path().canonicalize().unwrap();
|
||||||
let pending_exercises = exercises
|
let pending_exercises = exercises
|
||||||
.iter()
|
.iter()
|
||||||
.skip_while(|e| !filepath.ends_with(&e.path));
|
.skip_while(|e| !filepath.ends_with(&e.path))
|
||||||
|
// .filter(|e| filepath.ends_with(&e.path))
|
||||||
|
.chain(
|
||||||
|
exercises
|
||||||
|
.iter()
|
||||||
|
.filter(|e| !e.looks_done() && !filepath.ends_with(&e.path))
|
||||||
|
);
|
||||||
clear_screen();
|
clear_screen();
|
||||||
match verify(pending_exercises, verbose) {
|
match verify(pending_exercises, verbose) {
|
||||||
Ok(_) => return Ok(()),
|
Ok(_) => return Ok(()),
|
||||||
|
|
|
@ -9,3 +9,10 @@ name = "pending_test_exercise"
|
||||||
path = "pending_test_exercise.rs"
|
path = "pending_test_exercise.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
hint = """"""
|
hint = """"""
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "finished_exercise"
|
||||||
|
path = "finished_exercise.rs"
|
||||||
|
mode = "compile"
|
||||||
|
hint = """"""
|
||||||
|
|
||||||
|
|
|
@ -181,3 +181,81 @@ fn run_single_test_success_without_output() {
|
||||||
.code(0)
|
.code(0)
|
||||||
.stdout(predicates::str::contains("THIS TEST TOO SHALL PAS").not());
|
.stdout(predicates::str::contains("THIS TEST TOO SHALL PAS").not());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_rustlings_list() {
|
||||||
|
Command::cargo_bin("rustlings")
|
||||||
|
.unwrap()
|
||||||
|
.args(&["list"])
|
||||||
|
.current_dir("tests/fixture/success")
|
||||||
|
.assert()
|
||||||
|
.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_rustlings_list_conflicting_display_options() {
|
||||||
|
Command::cargo_bin("rustlings")
|
||||||
|
.unwrap()
|
||||||
|
.args(&["list", "--names", "--paths"])
|
||||||
|
.current_dir("tests/fixture/success")
|
||||||
|
.assert()
|
||||||
|
.failure();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_rustlings_list_conflicting_solve_options() {
|
||||||
|
Command::cargo_bin("rustlings")
|
||||||
|
.unwrap()
|
||||||
|
.args(&["list", "--solved", "--unsolved"])
|
||||||
|
.current_dir("tests/fixture/success")
|
||||||
|
.assert()
|
||||||
|
.failure();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_rustlings_list_no_pending() {
|
||||||
|
Command::cargo_bin("rustlings")
|
||||||
|
.unwrap()
|
||||||
|
.args(&["list"])
|
||||||
|
.current_dir("tests/fixture/success")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicates::str::contains("Pending").not());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_rustlings_list_both_done_and_pending() {
|
||||||
|
Command::cargo_bin("rustlings")
|
||||||
|
.unwrap()
|
||||||
|
.args(&["list"])
|
||||||
|
.current_dir("tests/fixture/state")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(
|
||||||
|
predicates::str::contains("Done")
|
||||||
|
.and(predicates::str::contains("Pending"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_rustlings_list_without_pending() {
|
||||||
|
Command::cargo_bin("rustlings")
|
||||||
|
.unwrap()
|
||||||
|
.args(&["list", "--solved"])
|
||||||
|
.current_dir("tests/fixture/state")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicates::str::contains("Pending").not());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_rustlings_list_without_done() {
|
||||||
|
Command::cargo_bin("rustlings")
|
||||||
|
.unwrap()
|
||||||
|
.args(&["list", "--unsolved"])
|
||||||
|
.current_dir("tests/fixture/state")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicates::str::contains("Done").not());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue