summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock6
-rw-r--r--Cargo.toml8
-rw-r--r--README.md5
-rw-r--r--exercises/quizzes/README.md3
-rw-r--r--exercises/quizzes/quiz1.rs (renamed from exercises/quiz1.rs)0
-rw-r--r--exercises/quizzes/quiz2.rs (renamed from exercises/quiz2.rs)0
-rw-r--r--exercises/quizzes/quiz3.rs (renamed from exercises/quiz3.rs)0
-rw-r--r--info.toml3
-rw-r--r--rustlings-macros/Cargo.toml2
-rw-r--r--rustlings-macros/src/lib.rs99
-rw-r--r--solutions/00_intro/intro1.rs1
-rw-r--r--solutions/00_intro/intro2.rs1
-rw-r--r--solutions/01_variables/variables1.rs1
-rw-r--r--solutions/01_variables/variables2.rs1
-rw-r--r--solutions/01_variables/variables3.rs1
-rw-r--r--solutions/01_variables/variables4.rs1
-rw-r--r--solutions/01_variables/variables5.rs1
-rw-r--r--solutions/01_variables/variables6.rs1
-rw-r--r--solutions/02_functions/functions1.rs1
-rw-r--r--solutions/02_functions/functions2.rs1
-rw-r--r--solutions/02_functions/functions3.rs1
-rw-r--r--solutions/02_functions/functions4.rs1
-rw-r--r--solutions/02_functions/functions5.rs1
-rw-r--r--solutions/03_if/if1.rs1
-rw-r--r--solutions/03_if/if2.rs1
-rw-r--r--solutions/03_if/if3.rs1
-rw-r--r--solutions/04_primitive_types/primitive_types1.rs1
-rw-r--r--solutions/04_primitive_types/primitive_types2.rs1
-rw-r--r--solutions/04_primitive_types/primitive_types3.rs1
-rw-r--r--solutions/04_primitive_types/primitive_types4.rs1
-rw-r--r--solutions/04_primitive_types/primitive_types5.rs1
-rw-r--r--solutions/04_primitive_types/primitive_types6.rs1
-rw-r--r--solutions/05_vecs/vecs1.rs1
-rw-r--r--solutions/05_vecs/vecs2.rs1
-rw-r--r--solutions/06_move_semantics/move_semantics1.rs1
-rw-r--r--solutions/06_move_semantics/move_semantics2.rs1
-rw-r--r--solutions/06_move_semantics/move_semantics3.rs1
-rw-r--r--solutions/06_move_semantics/move_semantics4.rs1
-rw-r--r--solutions/06_move_semantics/move_semantics5.rs1
-rw-r--r--solutions/06_move_semantics/move_semantics6.rs1
-rw-r--r--solutions/07_structs/structs1.rs1
-rw-r--r--solutions/07_structs/structs2.rs1
-rw-r--r--solutions/07_structs/structs3.rs1
-rw-r--r--solutions/08_enums/enums1.rs1
-rw-r--r--solutions/08_enums/enums2.rs1
-rw-r--r--solutions/08_enums/enums3.rs1
-rw-r--r--solutions/09_strings/strings1.rs1
-rw-r--r--solutions/09_strings/strings2.rs1
-rw-r--r--solutions/09_strings/strings3.rs1
-rw-r--r--solutions/09_strings/strings4.rs1
-rw-r--r--solutions/10_modules/modules1.rs1
-rw-r--r--solutions/10_modules/modules2.rs1
-rw-r--r--solutions/10_modules/modules3.rs1
-rw-r--r--solutions/11_hashmaps/hashmaps1.rs1
-rw-r--r--solutions/11_hashmaps/hashmaps2.rs1
-rw-r--r--solutions/11_hashmaps/hashmaps3.rs1
-rw-r--r--solutions/12_options/options1.rs1
-rw-r--r--solutions/12_options/options2.rs1
-rw-r--r--solutions/12_options/options3.rs1
-rw-r--r--solutions/13_error_handling/errors1.rs1
-rw-r--r--solutions/13_error_handling/errors2.rs1
-rw-r--r--solutions/13_error_handling/errors3.rs1
-rw-r--r--solutions/13_error_handling/errors4.rs1
-rw-r--r--solutions/13_error_handling/errors5.rs1
-rw-r--r--solutions/13_error_handling/errors6.rs1
-rw-r--r--solutions/14_generics/generics1.rs1
-rw-r--r--solutions/14_generics/generics2.rs1
-rw-r--r--solutions/15_traits/traits1.rs1
-rw-r--r--solutions/15_traits/traits2.rs1
-rw-r--r--solutions/15_traits/traits3.rs1
-rw-r--r--solutions/15_traits/traits4.rs1
-rw-r--r--solutions/15_traits/traits5.rs1
-rw-r--r--solutions/16_lifetimes/lifetimes1.rs1
-rw-r--r--solutions/16_lifetimes/lifetimes2.rs1
-rw-r--r--solutions/16_lifetimes/lifetimes3.rs1
-rw-r--r--solutions/17_tests/tests1.rs1
-rw-r--r--solutions/17_tests/tests2.rs1
-rw-r--r--solutions/17_tests/tests3.rs1
-rw-r--r--solutions/17_tests/tests4.rs1
-rw-r--r--solutions/18_iterators/iterators1.rs1
-rw-r--r--solutions/18_iterators/iterators2.rs1
-rw-r--r--solutions/18_iterators/iterators3.rs1
-rw-r--r--solutions/18_iterators/iterators4.rs1
-rw-r--r--solutions/18_iterators/iterators5.rs1
-rw-r--r--solutions/19_smart_pointers/arc1.rs1
-rw-r--r--solutions/19_smart_pointers/box1.rs1
-rw-r--r--solutions/19_smart_pointers/cow1.rs1
-rw-r--r--solutions/19_smart_pointers/rc1.rs1
-rw-r--r--solutions/20_threads/threads1.rs1
-rw-r--r--solutions/20_threads/threads2.rs1
-rw-r--r--solutions/20_threads/threads3.rs1
-rw-r--r--solutions/21_macros/macros1.rs1
-rw-r--r--solutions/21_macros/macros2.rs1
-rw-r--r--solutions/21_macros/macros3.rs1
-rw-r--r--solutions/21_macros/macros4.rs1
-rw-r--r--solutions/22_clippy/clippy1.rs1
-rw-r--r--solutions/22_clippy/clippy2.rs1
-rw-r--r--solutions/22_clippy/clippy3.rs1
-rw-r--r--solutions/23_conversions/as_ref_mut.rs1
-rw-r--r--solutions/23_conversions/from_into.rs1
-rw-r--r--solutions/23_conversions/from_str.rs1
-rw-r--r--solutions/23_conversions/try_from_into.rs1
-rw-r--r--solutions/23_conversions/using_as.rs1
-rw-r--r--solutions/quizzes/quiz1.rs1
-rw-r--r--solutions/quizzes/quiz2.rs1
-rw-r--r--solutions/quizzes/quiz3.rs1
-rw-r--r--src/app_state.rs68
-rw-r--r--src/embedded.rs147
-rw-r--r--src/exercise.rs29
-rw-r--r--src/info_file.rs29
-rw-r--r--src/init.rs4
-rw-r--r--src/main.rs1
-rw-r--r--src/run.rs14
-rw-r--r--src/terminal_link.rs23
-rw-r--r--src/watch/state.rs35
115 files changed, 359 insertions, 213 deletions
diff --git a/Cargo.lock b/Cargo.lock
index fdc7936..93617fa 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -656,9 +656,9 @@ checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
[[package]]
name = "rustix"
-version = "0.38.33"
+version = "0.38.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e3cc72858054fcff6d7dea32df2aeaee6a7c24227366d7ea429aada2f26b16ad"
+checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f"
dependencies = [
"bitflags 2.5.0",
"errno",
@@ -690,6 +690,8 @@ name = "rustlings-macros"
version = "6.0.0-alpha.0"
dependencies = [
"quote",
+ "serde",
+ "toml_edit",
]
[[package]]
diff --git a/Cargo.toml b/Cargo.toml
index 6181b1e..1e6a24c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,6 +17,10 @@ authors = [
license = "MIT"
edition = "2021"
+[workspace.dependencies]
+serde = { version = "1.0.198", features = ["derive"] }
+toml_edit = { version = "0.22.12", default-features = false, features = ["parse", "serde"] }
+
[package]
name = "rustlings"
description = "Small exercises to get you used to reading and writing Rust code!"
@@ -41,8 +45,8 @@ hashbrown = "0.14.3"
notify-debouncer-mini = "0.4.1"
ratatui = "0.26.2"
rustlings-macros = { path = "rustlings-macros", version = "6.0.0-alpha.0" }
-serde = { version = "1.0.198", features = ["derive"] }
-toml_edit = { version = "0.22.12", default-features = false, features = ["parse", "serde"] }
+serde.workspace = true
+toml_edit.workspace = true
which = "6.0.1"
[dev-dependencies]
diff --git a/README.md b/README.md
index bfa3f06..2164269 100644
--- a/README.md
+++ b/README.md
@@ -90,11 +90,6 @@ You can also get the hint for the next pending exercise with the following comma
rustlings hint
```
-## Quizzes
-
-After every couple of sections, there will be a quiz that'll test your knowledge on a bunch of sections at once.
-These quizzes are found in `exercises/quizN.rs`.
-
## Continuing On
<!-- TODO: Mention third-party exercises -->
diff --git a/exercises/quizzes/README.md b/exercises/quizzes/README.md
new file mode 100644
index 0000000..4d3bcd9
--- /dev/null
+++ b/exercises/quizzes/README.md
@@ -0,0 +1,3 @@
+# Quizzes
+
+After every couple of sections, there will be a quiz in this directory that'll test your knowledge on a bunch of sections at once.
diff --git a/exercises/quiz1.rs b/exercises/quizzes/quiz1.rs
index edb672e..edb672e 100644
--- a/exercises/quiz1.rs
+++ b/exercises/quizzes/quiz1.rs
diff --git a/exercises/quiz2.rs b/exercises/quizzes/quiz2.rs
index 0a29e78..0a29e78 100644
--- a/exercises/quiz2.rs
+++ b/exercises/quizzes/quiz2.rs
diff --git a/exercises/quiz3.rs b/exercises/quizzes/quiz3.rs
index f255cb5..f255cb5 100644
--- a/exercises/quiz3.rs
+++ b/exercises/quizzes/quiz3.rs
diff --git a/info.toml b/info.toml
index 27071a5..6549e1c 100644
--- a/info.toml
+++ b/info.toml
@@ -236,6 +236,7 @@ Make sure the type is consistent across all arms."""
[[exercises]]
name = "quiz1"
+dir = "quizzes"
mode = "test"
hint = "No hints this time ;)"
@@ -637,6 +638,7 @@ Learn more at https://doc.rust-lang.org/book/ch08-03-hash-maps.html#updating-a-v
[[exercises]]
name = "quiz2"
+dir = "quizzes"
mode = "test"
hint = "No hints this time ;)"
@@ -870,6 +872,7 @@ See the documentation at: https://doc.rust-lang.org/book/ch10-02-traits.html#spe
[[exercises]]
name = "quiz3"
+dir = "quizzes"
mode = "test"
hint = """
To find the best solution to this challenge you're going to need to think back
diff --git a/rustlings-macros/Cargo.toml b/rustlings-macros/Cargo.toml
index 095deaa..f925f69 100644
--- a/rustlings-macros/Cargo.toml
+++ b/rustlings-macros/Cargo.toml
@@ -11,3 +11,5 @@ proc-macro = true
[dependencies]
quote = "1.0.36"
+serde.workspace = true
+toml_edit.workspace = true
diff --git a/rustlings-macros/src/lib.rs b/rustlings-macros/src/lib.rs
index d95a93a..0bf3dbd 100644
--- a/rustlings-macros/src/lib.rs
+++ b/rustlings-macros/src/lib.rs
@@ -1,86 +1,39 @@
use proc_macro::TokenStream;
use quote::quote;
-use std::{fs::read_dir, panic, path::PathBuf};
+use serde::Deserialize;
-fn path_to_string(path: PathBuf) -> String {
- path.into_os_string()
- .into_string()
- .unwrap_or_else(|original| {
- panic!("The path {} is invalid UTF8", original.to_string_lossy());
- })
+#[derive(Deserialize)]
+struct ExerciseInfo {
+ name: String,
+ dir: String,
+}
+
+#[derive(Deserialize)]
+struct InfoFile {
+ exercises: Vec<ExerciseInfo>,
}
#[proc_macro]
pub fn include_files(_: TokenStream) -> TokenStream {
- let mut files = Vec::with_capacity(8);
- let mut dirs = Vec::with_capacity(128);
-
- for entry in read_dir("exercises").expect("Failed to open the `exercises` directory") {
- let entry = entry.expect("Failed to read the `exercises` directory");
-
- if entry.file_type().unwrap().is_file() {
- let path = entry.path();
- if path.file_name().unwrap() != "README.md" {
- files.push(path_to_string(path));
- }
-
- continue;
- }
-
- let dir_path = entry.path();
- let dir_files = read_dir(&dir_path).unwrap_or_else(|e| {
- panic!("Failed to open the directory {}: {e}", dir_path.display());
- });
- let dir_path = path_to_string(dir_path);
- let dir_files = dir_files.filter_map(|entry| {
- let entry = entry.unwrap_or_else(|e| {
- panic!("Failed to read the directory {dir_path}: {e}");
- });
- let path = entry.path();
-
- if !entry.file_type().unwrap().is_file() {
- panic!("Found {} but expected only files", path.display());
- }
-
- if path.file_name().unwrap() == "README.md" {
- return None;
- }
-
- Some(path_to_string(path))
- });
-
- dirs.push(quote! {
- EmbeddedFlatDir {
- path: #dir_path,
- readme: EmbeddedFile {
- path: ::std::concat!(#dir_path, "/README.md"),
- content: ::std::include_bytes!(::std::concat!("../", #dir_path, "/README.md")),
- },
- content: &[
- #(EmbeddedFile {
- path: #dir_files,
- content: ::std::include_bytes!(::std::concat!("../", #dir_files)),
- }),*
- ],
- }
- });
- }
+ let exercises = toml_edit::de::from_str::<InfoFile>(include_str!("../../info.toml"))
+ .expect("Failed to parse `info.toml`")
+ .exercises;
+
+ let exercise_files = exercises
+ .iter()
+ .map(|exercise| format!("../exercises/{}/{}.rs", exercise.dir, exercise.name));
+ let solution_files = exercises
+ .iter()
+ .map(|exercise| format!("../solutions/{}/{}.rs", exercise.dir, exercise.name));
+ let dirs = exercises.iter().map(|exercise| &exercise.dir);
+ let readmes = exercises
+ .iter()
+ .map(|exercise| format!("../exercises/{}/README.md", exercise.dir));
quote! {
EmbeddedFiles {
- exercises_dir: ExercisesDir {
- readme: EmbeddedFile {
- path: "exercises/README.md",
- content: ::std::include_bytes!("../exercises/README.md"),
- },
- files: &[#(
- EmbeddedFile {
- path: #files,
- content: ::std::include_bytes!(::std::concat!("../", #files)),
- }
- ),*],
- dirs: &[#(#dirs),*],
- },
+ exercise_files: &[#(ExerciseFiles { exercise: include_bytes!(#exercise_files), solution: include_bytes!(#solution_files) }),*],
+ exercise_dirs: &[#(ExerciseDir { name: #dirs, readme: include_bytes!(#readmes) }),*]
}
}
.into()
diff --git a/solutions/00_intro/intro1.rs b/solutions/00_intro/intro1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/00_intro/intro1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/00_intro/intro2.rs b/solutions/00_intro/intro2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/00_intro/intro2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/01_variables/variables1.rs b/solutions/01_variables/variables1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/01_variables/variables1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/01_variables/variables2.rs b/solutions/01_variables/variables2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/01_variables/variables2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/01_variables/variables3.rs b/solutions/01_variables/variables3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/01_variables/variables3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/01_variables/variables4.rs b/solutions/01_variables/variables4.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/01_variables/variables4.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/01_variables/variables5.rs b/solutions/01_variables/variables5.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/01_variables/variables5.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/01_variables/variables6.rs b/solutions/01_variables/variables6.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/01_variables/variables6.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/02_functions/functions1.rs b/solutions/02_functions/functions1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/02_functions/functions1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/02_functions/functions2.rs b/solutions/02_functions/functions2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/02_functions/functions2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/02_functions/functions3.rs b/solutions/02_functions/functions3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/02_functions/functions3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/02_functions/functions4.rs b/solutions/02_functions/functions4.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/02_functions/functions4.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/02_functions/functions5.rs b/solutions/02_functions/functions5.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/02_functions/functions5.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/03_if/if1.rs b/solutions/03_if/if1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/03_if/if1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/03_if/if2.rs b/solutions/03_if/if2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/03_if/if2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/03_if/if3.rs b/solutions/03_if/if3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/03_if/if3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/04_primitive_types/primitive_types1.rs b/solutions/04_primitive_types/primitive_types1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/04_primitive_types/primitive_types1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/04_primitive_types/primitive_types2.rs b/solutions/04_primitive_types/primitive_types2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/04_primitive_types/primitive_types2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/04_primitive_types/primitive_types3.rs b/solutions/04_primitive_types/primitive_types3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/04_primitive_types/primitive_types3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/04_primitive_types/primitive_types4.rs b/solutions/04_primitive_types/primitive_types4.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/04_primitive_types/primitive_types4.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/04_primitive_types/primitive_types5.rs b/solutions/04_primitive_types/primitive_types5.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/04_primitive_types/primitive_types5.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/04_primitive_types/primitive_types6.rs b/solutions/04_primitive_types/primitive_types6.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/04_primitive_types/primitive_types6.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/05_vecs/vecs1.rs b/solutions/05_vecs/vecs1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/05_vecs/vecs1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/05_vecs/vecs2.rs b/solutions/05_vecs/vecs2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/05_vecs/vecs2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/06_move_semantics/move_semantics1.rs b/solutions/06_move_semantics/move_semantics1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/06_move_semantics/move_semantics1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/06_move_semantics/move_semantics2.rs b/solutions/06_move_semantics/move_semantics2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/06_move_semantics/move_semantics2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/06_move_semantics/move_semantics3.rs b/solutions/06_move_semantics/move_semantics3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/06_move_semantics/move_semantics3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/06_move_semantics/move_semantics4.rs b/solutions/06_move_semantics/move_semantics4.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/06_move_semantics/move_semantics4.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/06_move_semantics/move_semantics5.rs b/solutions/06_move_semantics/move_semantics5.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/06_move_semantics/move_semantics5.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/06_move_semantics/move_semantics6.rs b/solutions/06_move_semantics/move_semantics6.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/06_move_semantics/move_semantics6.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/07_structs/structs1.rs b/solutions/07_structs/structs1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/07_structs/structs1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/07_structs/structs2.rs b/solutions/07_structs/structs2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/07_structs/structs2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/07_structs/structs3.rs b/solutions/07_structs/structs3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/07_structs/structs3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/08_enums/enums1.rs b/solutions/08_enums/enums1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/08_enums/enums1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/08_enums/enums2.rs b/solutions/08_enums/enums2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/08_enums/enums2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/08_enums/enums3.rs b/solutions/08_enums/enums3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/08_enums/enums3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/09_strings/strings1.rs b/solutions/09_strings/strings1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/09_strings/strings1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/09_strings/strings2.rs b/solutions/09_strings/strings2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/09_strings/strings2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/09_strings/strings3.rs b/solutions/09_strings/strings3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/09_strings/strings3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/09_strings/strings4.rs b/solutions/09_strings/strings4.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/09_strings/strings4.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/10_modules/modules1.rs b/solutions/10_modules/modules1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/10_modules/modules1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/10_modules/modules2.rs b/solutions/10_modules/modules2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/10_modules/modules2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/10_modules/modules3.rs b/solutions/10_modules/modules3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/10_modules/modules3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/11_hashmaps/hashmaps1.rs b/solutions/11_hashmaps/hashmaps1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/11_hashmaps/hashmaps1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/11_hashmaps/hashmaps2.rs b/solutions/11_hashmaps/hashmaps2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/11_hashmaps/hashmaps2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/11_hashmaps/hashmaps3.rs b/solutions/11_hashmaps/hashmaps3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/11_hashmaps/hashmaps3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/12_options/options1.rs b/solutions/12_options/options1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/12_options/options1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/12_options/options2.rs b/solutions/12_options/options2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/12_options/options2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/12_options/options3.rs b/solutions/12_options/options3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/12_options/options3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/13_error_handling/errors1.rs b/solutions/13_error_handling/errors1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/13_error_handling/errors1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/13_error_handling/errors2.rs b/solutions/13_error_handling/errors2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/13_error_handling/errors2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/13_error_handling/errors3.rs b/solutions/13_error_handling/errors3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/13_error_handling/errors3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/13_error_handling/errors4.rs b/solutions/13_error_handling/errors4.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/13_error_handling/errors4.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/13_error_handling/errors5.rs b/solutions/13_error_handling/errors5.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/13_error_handling/errors5.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/13_error_handling/errors6.rs b/solutions/13_error_handling/errors6.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/13_error_handling/errors6.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/14_generics/generics1.rs b/solutions/14_generics/generics1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/14_generics/generics1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/14_generics/generics2.rs b/solutions/14_generics/generics2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/14_generics/generics2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/15_traits/traits1.rs b/solutions/15_traits/traits1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/15_traits/traits1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/15_traits/traits2.rs b/solutions/15_traits/traits2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/15_traits/traits2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/15_traits/traits3.rs b/solutions/15_traits/traits3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/15_traits/traits3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/15_traits/traits4.rs b/solutions/15_traits/traits4.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/15_traits/traits4.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/15_traits/traits5.rs b/solutions/15_traits/traits5.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/15_traits/traits5.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/16_lifetimes/lifetimes1.rs b/solutions/16_lifetimes/lifetimes1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/16_lifetimes/lifetimes1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/16_lifetimes/lifetimes2.rs b/solutions/16_lifetimes/lifetimes2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/16_lifetimes/lifetimes2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/16_lifetimes/lifetimes3.rs b/solutions/16_lifetimes/lifetimes3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/16_lifetimes/lifetimes3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/17_tests/tests1.rs b/solutions/17_tests/tests1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/17_tests/tests1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/17_tests/tests2.rs b/solutions/17_tests/tests2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/17_tests/tests2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/17_tests/tests3.rs b/solutions/17_tests/tests3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/17_tests/tests3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/17_tests/tests4.rs b/solutions/17_tests/tests4.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/17_tests/tests4.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/18_iterators/iterators1.rs b/solutions/18_iterators/iterators1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/18_iterators/iterators1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/18_iterators/iterators2.rs b/solutions/18_iterators/iterators2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/18_iterators/iterators2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/18_iterators/iterators3.rs b/solutions/18_iterators/iterators3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/18_iterators/iterators3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/18_iterators/iterators4.rs b/solutions/18_iterators/iterators4.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/18_iterators/iterators4.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/18_iterators/iterators5.rs b/solutions/18_iterators/iterators5.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/18_iterators/iterators5.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/19_smart_pointers/arc1.rs b/solutions/19_smart_pointers/arc1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/19_smart_pointers/arc1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/19_smart_pointers/box1.rs b/solutions/19_smart_pointers/box1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/19_smart_pointers/box1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/19_smart_pointers/cow1.rs b/solutions/19_smart_pointers/cow1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/19_smart_pointers/cow1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/19_smart_pointers/rc1.rs b/solutions/19_smart_pointers/rc1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/19_smart_pointers/rc1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/20_threads/threads1.rs b/solutions/20_threads/threads1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/20_threads/threads1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/20_threads/threads2.rs b/solutions/20_threads/threads2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/20_threads/threads2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/20_threads/threads3.rs b/solutions/20_threads/threads3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/20_threads/threads3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/21_macros/macros1.rs b/solutions/21_macros/macros1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/21_macros/macros1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/21_macros/macros2.rs b/solutions/21_macros/macros2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/21_macros/macros2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/21_macros/macros3.rs b/solutions/21_macros/macros3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/21_macros/macros3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/21_macros/macros4.rs b/solutions/21_macros/macros4.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/21_macros/macros4.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/22_clippy/clippy1.rs b/solutions/22_clippy/clippy1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/22_clippy/clippy1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/22_clippy/clippy2.rs b/solutions/22_clippy/clippy2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/22_clippy/clippy2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/22_clippy/clippy3.rs b/solutions/22_clippy/clippy3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/22_clippy/clippy3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/23_conversions/as_ref_mut.rs b/solutions/23_conversions/as_ref_mut.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/23_conversions/as_ref_mut.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/23_conversions/from_into.rs b/solutions/23_conversions/from_into.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/23_conversions/from_into.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/23_conversions/from_str.rs b/solutions/23_conversions/from_str.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/23_conversions/from_str.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/23_conversions/try_from_into.rs b/solutions/23_conversions/try_from_into.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/23_conversions/try_from_into.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/23_conversions/using_as.rs b/solutions/23_conversions/using_as.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/23_conversions/using_as.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/quizzes/quiz1.rs b/solutions/quizzes/quiz1.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/quizzes/quiz1.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/quizzes/quiz2.rs b/solutions/quizzes/quiz2.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/quizzes/quiz2.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/solutions/quizzes/quiz3.rs b/solutions/quizzes/quiz3.rs
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/solutions/quizzes/quiz3.rs
@@ -0,0 +1 @@
+// TODO
diff --git a/src/app_state.rs b/src/app_state.rs
index 09de2a3..33d3de2 100644
--- a/src/app_state.rs
+++ b/src/app_state.rs
@@ -11,11 +11,7 @@ use std::{
process::{Command, Stdio},
};
-use crate::{
- embedded::{WriteStrategy, EMBEDDED_FILES},
- exercise::Exercise,
- info_file::ExerciseInfo,
-};
+use crate::{embedded::EMBEDDED_FILES, exercise::Exercise, info_file::ExerciseInfo, DEBUG_PROFILE};
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
@@ -100,10 +96,15 @@ impl AppState {
exercise_info.name.shrink_to_fit();
let name = exercise_info.name.leak();
+ let dir = exercise_info.dir.map(|mut dir| {
+ dir.shrink_to_fit();
+ &*dir.leak()
+ });
let hint = exercise_info.hint.trim().to_owned();
Exercise {
+ dir,
name,
path,
mode: exercise_info.mode,
@@ -181,10 +182,16 @@ impl AppState {
Ok(())
}
- fn reset_path(&self, path: &str) -> Result<()> {
+ fn reset(&self, ind: usize, dir_name: Option<&str>, path: &str) -> Result<()> {
if self.official_exercises {
return EMBEDDED_FILES
- .write_exercise_to_disk(path, WriteStrategy::Overwrite)
+ .write_exercise_to_disk(
+ ind,
+ dir_name.context(
+ "Official exercises must be nested in the `exercises` directory",
+ )?,
+ path,
+ )
.with_context(|| format!("Failed to reset the exercise {path}"));
}
@@ -209,11 +216,11 @@ impl AppState {
}
pub fn reset_current_exercise(&mut self) -> Result<&'static str> {
- let path = self.current_exercise().path;
self.set_pending(self.current_exercise_ind)?;
- self.reset_path(path)?;
+ let exercise = self.current_exercise();
+ self.reset(self.current_exercise_ind, exercise.dir, exercise.path)?;
- Ok(path)
+ Ok(exercise.path)
}
pub fn reset_exercise_by_ind(&mut self, exercise_ind: usize) -> Result<&'static str> {
@@ -221,11 +228,11 @@ impl AppState {
bail!(BAD_INDEX_ERR);
}
- let path = self.exercises[exercise_ind].path;
self.set_pending(exercise_ind)?;
- self.reset_path(path)?;
+ let exercise = &self.exercises[exercise_ind];
+ self.reset(exercise_ind, exercise.dir, exercise.path)?;
- Ok(path)
+ Ok(exercise.path)
}
fn next_pending_exercise_ind(&self) -> Option<usize> {
@@ -250,6 +257,41 @@ impl AppState {
}
}
+ pub fn current_solution_path(&self) -> Result<Option<String>> {
+ if DEBUG_PROFILE {
+ return Ok(None);
+ }
+
+ let current_exercise = self.current_exercise();
+
+ if self.official_exercises {
+ let dir_name = current_exercise
+ .dir
+ .context("Official exercises must be nested in the `exercises` directory")?;
+ let solution_path = format!("solutions/{dir_name}/{}.rs", current_exercise.name);
+
+ EMBEDDED_FILES.write_solution_to_disk(
+ self.current_exercise_ind,
+ dir_name,
+ &solution_path,
+ )?;
+
+ Ok(Some(solution_path))
+ } else {
+ let solution_path = if let Some(dir) = current_exercise.dir {
+ format!("solutions/{dir}/{}.rs", current_exercise.name)
+ } else {
+ format!("solutions/{}.rs", current_exercise.name)
+ };
+
+ if Path::new(&solution_path).exists() {
+ return Ok(Some(solution_path));
+ }
+
+ Ok(None)
+ }
+ }
+
pub fn done_current_exercise(&mut self, writer: &mut StdoutLock) -> Result<ExercisesProgress> {
let exercise = &mut self.exercises[self.current_exercise_ind];
if !exercise.done {
diff --git a/src/embedded.rs b/src/embedded.rs
index eae3099..d7952a1 100644
--- a/src/embedded.rs
+++ b/src/embedded.rs
@@ -1,9 +1,11 @@
+use anyhow::{bail, Context, Error, Result};
use std::{
- fs::{create_dir, File, OpenOptions},
+ fs::{create_dir, create_dir_all, OpenOptions},
io::{self, Write},
- path::Path,
};
+use crate::info_file::ExerciseInfo;
+
pub static EMBEDDED_FILES: EmbeddedFiles = rustlings_macros::include_files!();
#[derive(Clone, Copy)]
@@ -13,107 +15,110 @@ pub enum WriteStrategy {
}
impl WriteStrategy {
- fn open<P: AsRef<Path>>(self, path: P) -> io::Result<File> {
- match self {
+ fn write(self, path: &str, content: &[u8]) -> Result<()> {
+ let file = match self {
Self::IfNotExists => OpenOptions::new().create_new(true).write(true).open(path),
Self::Overwrite => OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(path),
- }
- }
-}
-
-struct EmbeddedFile {
- path: &'static str,
- content: &'static [u8],
-}
+ };
-impl EmbeddedFile {
- fn write_to_disk(&self, strategy: WriteStrategy) -> io::Result<()> {
- strategy.open(self.path)?.write_all(self.content)
+ file.context("Failed to open the file `{path}` in write mode")?
+ .write_all(content)
+ .context("Failed to write the file {path}")
}
}
-struct EmbeddedFlatDir {
- path: &'static str,
- readme: EmbeddedFile,
- content: &'static [EmbeddedFile],
+struct ExerciseFiles {
+ exercise: &'static [u8],
+ solution: &'static [u8],
}
-impl EmbeddedFlatDir {
- fn init_on_disk(&self) -> io::Result<()> {
- let path = Path::new(self.path);
+struct ExerciseDir {
+ name: &'static str,
+ readme: &'static [u8],
+}
- if let Err(e) = create_dir(path) {
- if e.kind() != io::ErrorKind::AlreadyExists {
- return Err(e);
+impl ExerciseDir {
+ fn init_on_disk(&self) -> Result<()> {
+ let path_prefix = "exercises/";
+ let readme_path_postfix = "/README.md";
+ let mut dir_path =
+ String::with_capacity(path_prefix.len() + self.name.len() + readme_path_postfix.len());
+ dir_path.push_str(path_prefix);
+ dir_path.push_str(self.name);
+
+ if let Err(e) = create_dir(&dir_path) {
+ if e.kind() == io::ErrorKind::AlreadyExists {
+ return Ok(());
}
+
+ return Err(
+ Error::from(e).context(format!("Failed to create the directory {dir_path}"))
+ );
}
- self.readme.write_to_disk(WriteStrategy::Overwrite)
- }
-}
+ let readme_path = {
+ dir_path.push_str(readme_path_postfix);
+ dir_path
+ };
+ WriteStrategy::Overwrite.write(&readme_path, self.readme)?;
-struct ExercisesDir {
- readme: EmbeddedFile,
- files: &'static [EmbeddedFile],
- dirs: &'static [EmbeddedFlatDir],
+ Ok(())
+ }
}
pub struct EmbeddedFiles {
- exercises_dir: ExercisesDir,
+ exercise_files: &'static [ExerciseFiles],
+ exercise_dirs: &'static [ExerciseDir],
}
impl EmbeddedFiles {
- pub fn init_exercises_dir(&self) -> io::Result<()> {
- create_dir("exercises")?;
-
- self.exercises_dir
- .readme
- .write_to_disk(WriteStrategy::IfNotExists)?;
+ pub fn init_exercises_dir(&self, exercise_infos: &[ExerciseInfo]) -> Result<()> {
+ create_dir("exercises").context("Failed to create the directory `exercises`")?;
- for file in self.exercises_dir.files {
- file.write_to_disk(WriteStrategy::IfNotExists)?;
- }
+ WriteStrategy::IfNotExists.write(
+ "exercises/README.md",
+ include_bytes!("../exercises/README.md"),
+ )?;
- for dir in self.exercises_dir.dirs {
+ for dir in self.exercise_dirs {
dir.init_on_disk()?;
+ }
- for file in dir.content {
- file.write_to_disk(WriteStrategy::IfNotExists)?;
- }
+ for (exercise_info, exercise_files) in exercise_infos.iter().zip(self.exercise_files) {
+ WriteStrategy::IfNotExists.write(&exercise_info.path(), exercise_files.exercise)?;
}
Ok(())
}
- pub fn write_exercise_to_disk<P>(&self, path: P, strategy: WriteStrategy) -> io::Result<()>
- where
- P: AsRef<Path>,
- {
- let path = path.as_ref();
-
- if let Some(file) = self
- .exercises_dir
- .files
- .iter()
- .find(|file| Path::new(file.path) == path)
- {
- return file.write_to_disk(strategy);
- }
-
- for dir in self.exercises_dir.dirs {
- if let Some(file) = dir.content.iter().find(|file| Path::new(file.path) == path) {
- dir.init_on_disk()?;
- return file.write_to_disk(strategy);
- }
- }
+ pub fn write_exercise_to_disk(
+ &self,
+ exercise_ind: usize,
+ dir_name: &str,
+ path: &str,
+ ) -> Result<()> {
+ let Some(dir) = self.exercise_dirs.iter().find(|dir| dir.name == dir_name) else {
+ bail!("`{dir_name}` not found in the embedded directories");
+ };
+
+ dir.init_on_disk()?;
+ WriteStrategy::Overwrite.write(path, self.exercise_files[exercise_ind].exercise)
+ }
- Err(io::Error::new(
- io::ErrorKind::NotFound,
- format!("{} not found in the embedded files", path.display()),
- ))
+ pub fn write_solution_to_disk(
+ &self,
+ exercise_ind: usize,
+ dir_name: &str,
+ path: &str,
+ ) -> Result<()> {
+ let dir_path = format!("solutions/{dir_name}");
+ create_dir_all(&dir_path)
+ .with_context(|| format!("Failed to create the directory {dir_path}"))?;
+
+ WriteStrategy::Overwrite.write(path, self.exercise_files[exercise_ind].solution)
}
}
diff --git a/src/exercise.rs b/src/exercise.rs
index e85efe4..45ac208 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -2,35 +2,14 @@ use anyhow::{Context, Result};
use crossterm::style::{style, StyledContent, Stylize};
use std::{
fmt::{self, Display, Formatter},
- fs,
path::Path,
process::{Command, Output},
};
-use crate::{info_file::Mode, DEBUG_PROFILE};
-
-pub struct TerminalFileLink<'a> {
- path: &'a str,
-}
-
-impl<'a> Display for TerminalFileLink<'a> {
- fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
- if let Ok(Some(canonical_path)) = fs::canonicalize(self.path)
- .as_deref()
- .map(|path| path.to_str())
- {
- write!(
- f,
- "\x1b]8;;file://{}\x1b\\{}\x1b]8;;\x1b\\",
- canonical_path, self.path,
- )
- } else {
- write!(f, "{}", self.path,)
- }
- }
-}
+use crate::{info_file::Mode, terminal_link::TerminalFileLink, DEBUG_PROFILE};
pub struct Exercise {
+ pub dir: Option<&'static str>,
// Exercise's unique name
pub name: &'static str,
// Exercise's path
@@ -84,9 +63,7 @@ impl Exercise {
}
pub fn terminal_link(&self) -> StyledContent<TerminalFileLink<'_>> {
- style(TerminalFileLink { path: self.path })
- .underlined()
- .blue()
+ style(TerminalFileLink(self.path)).underlined().blue()
}
}
diff --git a/src/info_file.rs b/src/info_file.rs
index 879609e..f344464 100644
--- a/src/info_file.rs
+++ b/src/info_file.rs
@@ -1,6 +1,8 @@
use anyhow::{bail, Context, Error, Result};
use serde::Deserialize;
-use std::fs;
+use std::{fs, io::ErrorKind};
+
+use crate::DEBUG_PROFILE;
// The mode of the exercise.
#[derive(Deserialize, Copy, Clone)]
@@ -46,18 +48,27 @@ pub struct InfoFile {
}
impl InfoFile {
+ fn from_embedded() -> Result<Self> {
+ toml_edit::de::from_str(include_str!("../info.toml"))
+ .context("Failed to parse the embedded `info.toml` file")
+ }
+
pub fn parse() -> Result<Self> {
+ if DEBUG_PROFILE {
+ return Self::from_embedded();
+ }
+
// Read a local `info.toml` if it exists.
- let slf: Self = match fs::read_to_string("info.toml") {
- Ok(file_content) => toml_edit::de::from_str(&file_content)
+ let slf = match fs::read_to_string("info.toml") {
+ Ok(file_content) => toml_edit::de::from_str::<Self>(&file_content)
.context("Failed to parse the `info.toml` file")?,
- Err(e) => match e.kind() {
- std::io::ErrorKind::NotFound => {
- toml_edit::de::from_str(include_str!("../info.toml"))
- .context("Failed to parse the embedded `info.toml` file")?
+ Err(e) => {
+ if e.kind() == ErrorKind::NotFound {
+ return Self::from_embedded();
}
- _ => return Err(Error::from(e).context("Failed to read the `info.toml` file")),
- },
+
+ return Err(Error::from(e).context("Failed to read the `info.toml` file"));
+ }
};
if slf.exercises.is_empty() {
diff --git a/src/init.rs b/src/init.rs
index f210db7..f1a9509 100644
--- a/src/init.rs
+++ b/src/init.rs
@@ -24,11 +24,11 @@ pub fn init() -> Result<()> {
set_current_dir("rustlings")
.context("Failed to change the current directory to `rustlings`")?;
+ let info_file = InfoFile::parse()?;
EMBEDDED_FILES
- .init_exercises_dir()
+ .init_exercises_dir(&info_file.exercises)
.context("Failed to initialize the `rustlings/exercises` directory")?;
- let info_file = InfoFile::parse()?;
let current_cargo_toml = include_str!("../dev/Cargo.toml");
// Skip the first line (comment).
let newline_ind = current_cargo_toml
diff --git a/src/main.rs b/src/main.rs
index 9ff218a..790fff6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -23,6 +23,7 @@ mod init;
mod list;
mod progress_bar;
mod run;
+mod terminal_link;
mod watch;
const CURRENT_FORMAT_VERSION: u8 = 1;
diff --git a/src/run.rs b/src/run.rs
index 863b584..a2b6972 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -1,8 +1,11 @@
use anyhow::{bail, Result};
-use crossterm::style::Stylize;
+use crossterm::style::{style, Stylize};
use std::io::{self, Write};
-use crate::app_state::{AppState, ExercisesProgress};
+use crate::{
+ app_state::{AppState, ExercisesProgress},
+ terminal_link::TerminalFileLink,
+};
pub fn run(app_state: &mut AppState) -> Result<()> {
let exercise = app_state.current_exercise();
@@ -29,6 +32,13 @@ pub fn run(app_state: &mut AppState) -> Result<()> {
exercise.path.green(),
))?;
+ if let Some(solution_path) = app_state.current_solution_path()? {
+ println!(
+ "\nA solution file can be found at {}\n",
+ style(TerminalFileLink(&solution_path)).underlined().green(),
+ );
+ }
+
match app_state.done_current_exercise(&mut stdout)? {
ExercisesProgress::AllDone => (),
ExercisesProgress::Pending => println!(
diff --git a/src/terminal_link.rs b/src/terminal_link.rs
new file mode 100644
index 0000000..c9e6bce
--- /dev/null
+++ b/src/terminal_link.rs
@@ -0,0 +1,23 @@
+use std::{
+ fmt::{self, Display, Formatter},
+ fs,
+};
+
+pub struct TerminalFileLink<'a>(pub &'a str);
+
+impl<'a> Display for TerminalFileLink<'a> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ if let Ok(Some(canonical_path)) = fs::canonicalize(self.0)
+ .as_deref()
+ .map(|path| path.to_str())
+ {
+ write!(
+ f,
+ "\x1b]8;;file://{}\x1b\\{}\x1b]8;;\x1b\\",
+ canonical_path, self.0,
+ )
+ } else {
+ write!(f, "{}", self.0)
+ }
+ }
+}
diff --git a/src/watch/state.rs b/src/watch/state.rs
index c0f6c53..5f4abf3 100644
--- a/src/watch/state.rs
+++ b/src/watch/state.rs
@@ -1,6 +1,6 @@
use anyhow::Result;
use crossterm::{
- style::Stylize,
+ style::{style, Stylize},
terminal::{size, Clear, ClearType},
ExecutableCommand,
};
@@ -9,15 +9,22 @@ use std::io::{self, StdoutLock, Write};
use crate::{
app_state::{AppState, ExercisesProgress},
progress_bar::progress_bar,
+ terminal_link::TerminalFileLink,
};
+enum DoneStatus {
+ DoneWithSolution(String),
+ DoneWithoutSolution,
+ Pending,
+}
+
pub struct WatchState<'a> {
writer: StdoutLock<'a>,
app_state: &'a mut AppState,
stdout: Option<Vec<u8>>,
stderr: Option<Vec<u8>>,
show_hint: bool,
- show_done: bool,
+ done_status: DoneStatus,
manual_run: bool,
}
@@ -31,7 +38,7 @@ impl<'a> WatchState<'a> {
stdout: None,
stderr: None,
show_hint: false,
- show_done: false,
+ done_status: DoneStatus::Pending,
manual_run,
}
}
@@ -49,13 +56,18 @@ impl<'a> WatchState<'a> {
if output.status.success() {
self.stderr = None;
- self.show_done = true;
+ self.done_status =
+ if let Some(solution_path) = self.app_state.current_solution_path()? {
+ DoneStatus::DoneWithSolution(solution_path)
+ } else {
+ DoneStatus::DoneWithoutSolution
+ };
} else {
self.app_state
.set_pending(self.app_state.current_exercise_ind())?;
self.stderr = Some(output.stderr);
- self.show_done = false;
+ self.done_status = DoneStatus::Pending;
}
self.render()
@@ -67,7 +79,7 @@ impl<'a> WatchState<'a> {
}
pub fn next_exercise(&mut self) -> Result<ExercisesProgress> {
- if !self.show_done {
+ if matches!(self.done_status, DoneStatus::Pending) {
self.writer
.write_all(b"The current exercise isn't done yet\n")?;
self.show_prompt()?;
@@ -84,7 +96,7 @@ impl<'a> WatchState<'a> {
self.writer.write_fmt(format_args!("{}un/", 'r'.bold()))?;
}
- if self.show_done {
+ if !matches!(self.done_status, DoneStatus::Pending) {
self.writer.write_fmt(format_args!("{}ext/", 'n'.bold()))?;
}
@@ -124,7 +136,7 @@ impl<'a> WatchState<'a> {
))?;
}
- if self.show_done {
+ if !matches!(self.done_status, DoneStatus::Pending) {
self.writer.write_fmt(format_args!(
"{}\n\n",
"Exercise done ✓
@@ -134,6 +146,13 @@ When you are done experimenting, enter `n` or `next` to go to the next exercise
))?;
}
+ if let DoneStatus::DoneWithSolution(solution_path) = &self.done_status {
+ self.writer.write_fmt(format_args!(
+ "A solution file can be found at {}\n\n",
+ style(TerminalFileLink(solution_path)).underlined().green()
+ ))?;
+ }
+
let line_width = size()?.0;
let progress_bar = progress_bar(
self.app_state.n_done(),