From a325df55d1077c8613905bb82709cd8c80341641 Mon Sep 17 00:00:00 2001
From: mo8it <mo8it@proton.me>
Date: Sat, 23 Mar 2024 21:56:40 +0100
Subject: [PATCH 1/9] Cache filters

---
 src/main.rs | 39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index a06f0c56..9bf58668 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -128,31 +128,45 @@ fn main() {
                 println!("{:<17}\t{:<46}\t{:<7}", "Name", "Path", "Status");
             }
             let mut exercises_done: u16 = 0;
-            let filters = filter.clone().unwrap_or_default().to_lowercase();
-            exercises.iter().for_each(|e| {
-                let fname = format!("{}", e.path.display());
+            let lowercase_filter = filter
+                .as_ref()
+                .map(|s| s.to_lowercase())
+                .unwrap_or_default();
+            let filters = lowercase_filter
+                .split(',')
+                .filter_map(|f| {
+                    let f = f.trim();
+                    if f.is_empty() {
+                        None
+                    } else {
+                        Some(f)
+                    }
+                })
+                .collect::<Vec<_>>();
+
+            for exercise in &exercises {
+                let fname = format!("{}", exercise.path.display());
                 let filter_cond = filters
-                    .split(',')
-                    .filter(|f| !f.trim().is_empty())
-                    .any(|f| e.name.contains(f) || fname.contains(f));
-                let status = if e.looks_done() {
+                    .iter()
+                    .any(|f| exercise.name.contains(f) || fname.contains(f));
+                let status = if exercise.looks_done() {
                     exercises_done += 1;
                     "Done"
                 } else {
                     "Pending"
                 };
                 let solve_cond = {
-                    (e.looks_done() && solved)
-                        || (!e.looks_done() && unsolved)
+                    (exercise.looks_done() && solved)
+                        || (!exercise.looks_done() && unsolved)
                         || (!solved && !unsolved)
                 };
                 if solve_cond && (filter_cond || filter.is_none()) {
                     let line = if paths {
                         format!("{fname}\n")
                     } else if names {
-                        format!("{}\n", e.name)
+                        format!("{}\n", exercise.name)
                     } else {
-                        format!("{:<17}\t{fname:<46}\t{status:<7}\n", e.name)
+                        format!("{:<17}\t{fname:<46}\t{status:<7}\n", exercise.name)
                     };
                     // Somehow using println! leads to the binary panicking
                     // when its output is piped.
@@ -168,7 +182,8 @@ fn main() {
                         });
                     }
                 }
-            });
+            }
+
             let percentage_progress = exercises_done as f32 / exercises.len() as f32 * 100.0;
             println!(
                 "Progress: You completed {} / {} exercises ({:.1} %).",

From 01b7d6334c44d55f11d7f09c45e76b2db7fef948 Mon Sep 17 00:00:00 2001
From: mo8it <mo8it@proton.me>
Date: Sat, 23 Mar 2024 22:08:25 +0100
Subject: [PATCH 2/9] Remove unneeded to_string call

---
 src/verify.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/verify.rs b/src/verify.rs
index aee2afa3..e3a8e887 100644
--- a/src/verify.rs
+++ b/src/verify.rs
@@ -224,7 +224,7 @@ fn prompt_for_completion(
         let formatted_line = if context_line.important {
             format!("{}", style(context_line.line).bold())
         } else {
-            context_line.line.to_string()
+            context_line.line
         };
 
         println!(

From 0aeaccc3a50b5b60b6005161847641bade75effa Mon Sep 17 00:00:00 2001
From: mo8it <mo8it@proton.me>
Date: Sun, 24 Mar 2024 18:34:46 +0100
Subject: [PATCH 3/9] Optimize state

---
 src/exercise.rs | 112 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 75 insertions(+), 37 deletions(-)

diff --git a/src/exercise.rs b/src/exercise.rs
index 664b362b..b112fe8a 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -1,16 +1,16 @@
 use regex::Regex;
 use serde::Deserialize;
-use std::env;
 use std::fmt::{self, Display, Formatter};
 use std::fs::{self, remove_file, File};
-use std::io::Read;
+use std::io::{self, BufRead, BufReader};
 use std::path::PathBuf;
 use std::process::{self, Command};
+use std::{array, env, mem};
 
 const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"];
 const RUSTC_EDITION_ARGS: &[&str] = &["--edition", "2021"];
 const RUSTC_NO_DEBUG_ARGS: &[&str] = &["-C", "strip=debuginfo"];
-const I_AM_DONE_REGEX: &str = r"(?m)^\s*///?\s*I\s+AM\s+NOT\s+DONE";
+const I_AM_DONE_REGEX: &str = r"^\s*///?\s*I\s+AM\s+NOT\s+DONE";
 const CONTEXT: usize = 2;
 const CLIPPY_CARGO_TOML_PATH: &str = "./exercises/22_clippy/Cargo.toml";
 
@@ -205,51 +205,89 @@ path = "{}.rs""#,
     }
 
     pub fn state(&self) -> State {
-        let mut source_file = File::open(&self.path).unwrap_or_else(|e| {
+        let source_file = File::open(&self.path).unwrap_or_else(|e| {
             panic!(
                 "We were unable to open the exercise file {}! {e}",
                 self.path.display()
             )
         });
-
-        let source = {
-            let mut s = String::new();
-            source_file.read_to_string(&mut s).unwrap_or_else(|e| {
-                panic!(
-                    "We were unable to read the exercise file {}! {e}",
-                    self.path.display()
-                )
-            });
-            s
+        let mut source_reader = BufReader::new(source_file);
+        let mut read_line = |buf: &mut String| -> io::Result<_> {
+            let n = source_reader.read_line(buf)?;
+            if buf.ends_with('\n') {
+                buf.pop();
+                if buf.ends_with('\r') {
+                    buf.pop();
+                }
+            }
+            Ok(n)
         };
 
         let re = Regex::new(I_AM_DONE_REGEX).unwrap();
+        let mut matched_line_ind: usize = 0;
+        let mut prev_lines: [_; CONTEXT] = array::from_fn(|_| String::with_capacity(256));
+        let mut line = String::with_capacity(256);
 
-        if !re.is_match(&source) {
-            return State::Done;
+        loop {
+            match read_line(&mut line) {
+                Ok(0) => break,
+                Ok(_) => {
+                    if re.is_match(&line) {
+                        let mut context = Vec::with_capacity(2 * CONTEXT + 1);
+                        for (ind, prev_line) in prev_lines
+                            .into_iter()
+                            .rev()
+                            .take(matched_line_ind)
+                            .enumerate()
+                        {
+                            context.push(ContextLine {
+                                line: prev_line,
+                                // TODO
+                                number: matched_line_ind - CONTEXT + ind + 1,
+                                important: false,
+                            });
+                        }
+
+                        context.push(ContextLine {
+                            line,
+                            number: matched_line_ind + 1,
+                            important: true,
+                        });
+
+                        for ind in 0..CONTEXT {
+                            let mut next_line = String::with_capacity(256);
+                            let Ok(n) = read_line(&mut next_line) else {
+                                break;
+                            };
+
+                            if n == 0 {
+                                break;
+                            }
+
+                            context.push(ContextLine {
+                                line: next_line,
+                                number: matched_line_ind + ind + 2,
+                                important: false,
+                            });
+                        }
+
+                        return State::Pending(context);
+                    }
+
+                    matched_line_ind += 1;
+                    for prev_line in &mut prev_lines {
+                        mem::swap(&mut line, prev_line);
+                    }
+                    line.clear();
+                }
+                Err(e) => panic!(
+                    "We were unable to read the exercise file {}! {e}",
+                    self.path.display()
+                ),
+            }
         }
 
-        let matched_line_index = source
-            .lines()
-            .enumerate()
-            .find_map(|(i, line)| if re.is_match(line) { Some(i) } else { None })
-            .expect("This should not happen at all");
-
-        let min_line = ((matched_line_index as i32) - (CONTEXT as i32)).max(0) as usize;
-        let max_line = matched_line_index + CONTEXT;
-
-        let context = source
-            .lines()
-            .enumerate()
-            .filter(|&(i, _)| i >= min_line && i <= max_line)
-            .map(|(i, line)| ContextLine {
-                line: line.to_string(),
-                number: i + 1,
-                important: i == matched_line_index,
-            })
-            .collect();
-
-        State::Pending(context)
+        State::Done
     }
 
     // Check that the exercise looks to be solved using self.state()

From e1375ef4319641749611124ae495346d32e04e2d Mon Sep 17 00:00:00 2001
From: mo8it <mo8it@proton.me>
Date: Sun, 24 Mar 2024 18:47:27 +0100
Subject: [PATCH 4/9] Use to_string_lossy

---
 src/main.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main.rs b/src/main.rs
index 9bf58668..067c810c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -145,7 +145,7 @@ fn main() {
                 .collect::<Vec<_>>();
 
             for exercise in &exercises {
-                let fname = format!("{}", exercise.path.display());
+                let fname = exercise.path.to_string_lossy();
                 let filter_cond = filters
                     .iter()
                     .any(|f| exercise.name.contains(f) || fname.contains(f));

From f205ee3d4c6f259c82e4f1226acc6a5ae5e70031 Mon Sep 17 00:00:00 2001
From: mo8it <mo8it@proton.me>
Date: Sun, 24 Mar 2024 18:50:46 +0100
Subject: [PATCH 5/9] Call looks_done only once

---
 src/main.rs | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 067c810c..f646fdca 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -149,17 +149,15 @@ fn main() {
                 let filter_cond = filters
                     .iter()
                     .any(|f| exercise.name.contains(f) || fname.contains(f));
-                let status = if exercise.looks_done() {
+                let looks_done = exercise.looks_done();
+                let status = if looks_done {
                     exercises_done += 1;
                     "Done"
                 } else {
                     "Pending"
                 };
-                let solve_cond = {
-                    (exercise.looks_done() && solved)
-                        || (!exercise.looks_done() && unsolved)
-                        || (!solved && !unsolved)
-                };
+                let solve_cond =
+                    (looks_done && solved) || (!looks_done && unsolved) || (!solved && !unsolved);
                 if solve_cond && (filter_cond || filter.is_none()) {
                     let line = if paths {
                         format!("{fname}\n")

From c0c112985b531bbcf503a2b1a8c2764030a16c99 Mon Sep 17 00:00:00 2001
From: mo8it <mo8it@proton.me>
Date: Sun, 24 Mar 2024 19:18:19 +0100
Subject: [PATCH 6/9] Replace regex with winnow

---
 Cargo.lock      |  2 +-
 Cargo.toml      |  2 +-
 src/exercise.rs | 44 ++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 3950c476..e42b8f40 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -533,10 +533,10 @@ dependencies = [
  "indicatif",
  "notify-debouncer-mini",
  "predicates",
- "regex",
  "serde",
  "serde_json",
  "toml",
+ "winnow",
 ]
 
 [[package]]
diff --git a/Cargo.toml b/Cargo.toml
index 218b7990..dd4c0c30 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,10 +15,10 @@ glob = "0.3.0"
 home = "0.5.9"
 indicatif = "0.17.8"
 notify-debouncer-mini = "0.4.1"
-regex = "1.10.3"
 serde_json = "1.0.114"
 serde = { version = "1.0.197", features = ["derive"] }
 toml = "0.8.10"
+winnow = "0.6.5"
 
 [[bin]]
 name = "rustlings"
diff --git a/src/exercise.rs b/src/exercise.rs
index b112fe8a..8f580d30 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -1,4 +1,3 @@
-use regex::Regex;
 use serde::Deserialize;
 use std::fmt::{self, Display, Formatter};
 use std::fs::{self, remove_file, File};
@@ -6,14 +5,34 @@ use std::io::{self, BufRead, BufReader};
 use std::path::PathBuf;
 use std::process::{self, Command};
 use std::{array, env, mem};
+use winnow::ascii::{space0, space1};
+use winnow::combinator::opt;
+use winnow::Parser;
 
 const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"];
 const RUSTC_EDITION_ARGS: &[&str] = &["--edition", "2021"];
 const RUSTC_NO_DEBUG_ARGS: &[&str] = &["-C", "strip=debuginfo"];
-const I_AM_DONE_REGEX: &str = r"^\s*///?\s*I\s+AM\s+NOT\s+DONE";
 const CONTEXT: usize = 2;
 const CLIPPY_CARGO_TOML_PATH: &str = "./exercises/22_clippy/Cargo.toml";
 
+fn not_done(input: &str) -> bool {
+    (
+        space0::<_, ()>,
+        "//",
+        opt('/'),
+        space0,
+        'I',
+        space1,
+        "AM",
+        space1,
+        "NOT",
+        space1,
+        "DONE",
+    )
+        .parse_next(&mut &*input)
+        .is_ok()
+}
+
 // Get a temporary file name that is hopefully unique
 #[inline]
 fn temp_file() -> String {
@@ -223,7 +242,6 @@ path = "{}.rs""#,
             Ok(n)
         };
 
-        let re = Regex::new(I_AM_DONE_REGEX).unwrap();
         let mut matched_line_ind: usize = 0;
         let mut prev_lines: [_; CONTEXT] = array::from_fn(|_| String::with_capacity(256));
         let mut line = String::with_capacity(256);
@@ -232,7 +250,7 @@ path = "{}.rs""#,
             match read_line(&mut line) {
                 Ok(0) => break,
                 Ok(_) => {
-                    if re.is_match(&line) {
+                    if not_done(&line) {
                         let mut context = Vec::with_capacity(2 * CONTEXT + 1);
                         for (ind, prev_line) in prev_lines
                             .into_iter()
@@ -413,4 +431,22 @@ mod test {
         let out = exercise.compile().unwrap().run().unwrap();
         assert!(out.stdout.contains("THIS TEST TOO SHALL PASS"));
     }
+
+    #[test]
+    fn test_not_done() {
+        assert!(not_done("// I AM NOT DONE"));
+        assert!(not_done("/// I AM NOT DONE"));
+        assert!(not_done("//  I AM NOT DONE"));
+        assert!(not_done("///  I AM NOT DONE"));
+        assert!(not_done("// I  AM NOT DONE"));
+        assert!(not_done("// I AM  NOT DONE"));
+        assert!(not_done("// I AM NOT  DONE"));
+        assert!(not_done("// I AM NOT DONE "));
+        assert!(not_done("// I AM NOT DONE!"));
+
+        assert!(!not_done("I AM NOT DONE"));
+        assert!(!not_done("// NOT DONE"));
+        assert!(!not_done("DONE"));
+        assert!(!not_done("// i am not done"));
+    }
 }

From bdf826a026cfe7f89c31433cfd2b9a32cbe66d2c Mon Sep 17 00:00:00 2001
From: mo8it <mo8it@proton.me>
Date: Sun, 24 Mar 2024 22:22:55 +0100
Subject: [PATCH 7/9] Make "I AM NOT DONE" caseless

---
 src/exercise.rs | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/src/exercise.rs b/src/exercise.rs
index 8f580d30..136e9439 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -5,7 +5,7 @@ use std::io::{self, BufRead, BufReader};
 use std::path::PathBuf;
 use std::process::{self, Command};
 use std::{array, env, mem};
-use winnow::ascii::{space0, space1};
+use winnow::ascii::{space0, Caseless};
 use winnow::combinator::opt;
 use winnow::Parser;
 
@@ -21,13 +21,7 @@ fn not_done(input: &str) -> bool {
         "//",
         opt('/'),
         space0,
-        'I',
-        space1,
-        "AM",
-        space1,
-        "NOT",
-        space1,
-        "DONE",
+        Caseless("I AM NOT DONE"),
     )
         .parse_next(&mut &*input)
         .is_ok()
@@ -438,15 +432,13 @@ mod test {
         assert!(not_done("/// I AM NOT DONE"));
         assert!(not_done("//  I AM NOT DONE"));
         assert!(not_done("///  I AM NOT DONE"));
-        assert!(not_done("// I  AM NOT DONE"));
-        assert!(not_done("// I AM  NOT DONE"));
-        assert!(not_done("// I AM NOT  DONE"));
         assert!(not_done("// I AM NOT DONE "));
         assert!(not_done("// I AM NOT DONE!"));
+        assert!(not_done("// I am not done"));
+        assert!(not_done("// i am NOT done"));
 
         assert!(!not_done("I AM NOT DONE"));
         assert!(!not_done("// NOT DONE"));
         assert!(!not_done("DONE"));
-        assert!(!not_done("// i am not done"));
     }
 }

From 7a6f71f09092e8a521d53456491e7d9d8a159602 Mon Sep 17 00:00:00 2001
From: mo8it <mo8it@proton.me>
Date: Tue, 26 Mar 2024 02:14:25 +0100
Subject: [PATCH 8/9] Fix context of previous lines and improve readability

---
 src/exercise.rs | 154 +++++++++++++++++++++++++-----------------------
 1 file changed, 80 insertions(+), 74 deletions(-)

diff --git a/src/exercise.rs b/src/exercise.rs
index 136e9439..e841aed2 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -3,7 +3,7 @@ use std::fmt::{self, Display, Formatter};
 use std::fs::{self, remove_file, File};
 use std::io::{self, BufRead, BufReader};
 use std::path::PathBuf;
-use std::process::{self, Command};
+use std::process::{self, exit, Command};
 use std::{array, env, mem};
 use winnow::ascii::{space0, Caseless};
 use winnow::combinator::opt;
@@ -15,7 +15,8 @@ const RUSTC_NO_DEBUG_ARGS: &[&str] = &["-C", "strip=debuginfo"];
 const CONTEXT: usize = 2;
 const CLIPPY_CARGO_TOML_PATH: &str = "./exercises/22_clippy/Cargo.toml";
 
-fn not_done(input: &str) -> bool {
+// Checks if the line contains the "I AM NOT DONE" comment.
+fn contains_not_done_comment(input: &str) -> bool {
     (
         space0::<_, ()>,
         "//",
@@ -219,12 +220,15 @@ path = "{}.rs""#,
 
     pub fn state(&self) -> State {
         let source_file = File::open(&self.path).unwrap_or_else(|e| {
-            panic!(
-                "We were unable to open the exercise file {}! {e}",
-                self.path.display()
-            )
+            println!(
+                "Failed to open the exercise file {}: {e}",
+                self.path.display(),
+            );
+            exit(1);
         });
         let mut source_reader = BufReader::new(source_file);
+
+        // Read the next line into `buf` without the newline at the end.
         let mut read_line = |buf: &mut String| -> io::Result<_> {
             let n = source_reader.read_line(buf)?;
             if buf.ends_with('\n') {
@@ -236,70 +240,72 @@ path = "{}.rs""#,
             Ok(n)
         };
 
-        let mut matched_line_ind: usize = 0;
+        let mut current_line_number: usize = 1;
         let mut prev_lines: [_; CONTEXT] = array::from_fn(|_| String::with_capacity(256));
         let mut line = String::with_capacity(256);
 
         loop {
-            match read_line(&mut line) {
-                Ok(0) => break,
-                Ok(_) => {
-                    if not_done(&line) {
-                        let mut context = Vec::with_capacity(2 * CONTEXT + 1);
-                        for (ind, prev_line) in prev_lines
-                            .into_iter()
-                            .rev()
-                            .take(matched_line_ind)
-                            .enumerate()
-                        {
-                            context.push(ContextLine {
-                                line: prev_line,
-                                // TODO
-                                number: matched_line_ind - CONTEXT + ind + 1,
-                                important: false,
-                            });
-                        }
+            let n = read_line(&mut line).unwrap_or_else(|e| {
+                println!(
+                    "Failed to read the exercise file {}: {e}",
+                    self.path.display(),
+                );
+                exit(1);
+            });
 
-                        context.push(ContextLine {
-                            line,
-                            number: matched_line_ind + 1,
-                            important: true,
-                        });
-
-                        for ind in 0..CONTEXT {
-                            let mut next_line = String::with_capacity(256);
-                            let Ok(n) = read_line(&mut next_line) else {
-                                break;
-                            };
-
-                            if n == 0 {
-                                break;
-                            }
-
-                            context.push(ContextLine {
-                                line: next_line,
-                                number: matched_line_ind + ind + 2,
-                                important: false,
-                            });
-                        }
-
-                        return State::Pending(context);
-                    }
-
-                    matched_line_ind += 1;
-                    for prev_line in &mut prev_lines {
-                        mem::swap(&mut line, prev_line);
-                    }
-                    line.clear();
-                }
-                Err(e) => panic!(
-                    "We were unable to read the exercise file {}! {e}",
-                    self.path.display()
-                ),
+            // Reached the end of the file and didn't find the comment.
+            if n == 0 {
+                return State::Done;
             }
-        }
 
-        State::Done
+            if contains_not_done_comment(&line) {
+                let mut context = Vec::with_capacity(2 * CONTEXT + 1);
+                for (ind, prev_line) in prev_lines
+                    .into_iter()
+                    .take(current_line_number - 1)
+                    .enumerate()
+                    .rev()
+                {
+                    context.push(ContextLine {
+                        line: prev_line,
+                        number: current_line_number - 1 - ind,
+                        important: false,
+                    });
+                }
+
+                context.push(ContextLine {
+                    line,
+                    number: current_line_number,
+                    important: true,
+                });
+
+                for ind in 0..CONTEXT {
+                    let mut next_line = String::with_capacity(256);
+                    let Ok(n) = read_line(&mut next_line) else {
+                        break;
+                    };
+
+                    if n == 0 {
+                        break;
+                    }
+
+                    context.push(ContextLine {
+                        line: next_line,
+                        number: current_line_number + 1 + ind,
+                        important: false,
+                    });
+                }
+
+                return State::Pending(context);
+            }
+
+            current_line_number += 1;
+            // Recycle the buffers.
+            for prev_line in &mut prev_lines {
+                mem::swap(&mut line, prev_line);
+            }
+            line.clear();
+        }
     }
 
     // Check that the exercise looks to be solved using self.state()
@@ -428,17 +434,17 @@ mod test {
 
     #[test]
     fn test_not_done() {
-        assert!(not_done("// I AM NOT DONE"));
-        assert!(not_done("/// I AM NOT DONE"));
-        assert!(not_done("//  I AM NOT DONE"));
-        assert!(not_done("///  I AM NOT DONE"));
-        assert!(not_done("// I AM NOT DONE "));
-        assert!(not_done("// I AM NOT DONE!"));
-        assert!(not_done("// I am not done"));
-        assert!(not_done("// i am NOT done"));
+        assert!(contains_not_done_comment("// I AM NOT DONE"));
+        assert!(contains_not_done_comment("/// I AM NOT DONE"));
+        assert!(contains_not_done_comment("//  I AM NOT DONE"));
+        assert!(contains_not_done_comment("///  I AM NOT DONE"));
+        assert!(contains_not_done_comment("// I AM NOT DONE "));
+        assert!(contains_not_done_comment("// I AM NOT DONE!"));
+        assert!(contains_not_done_comment("// I am not done"));
+        assert!(contains_not_done_comment("// i am NOT done"));
 
-        assert!(!not_done("I AM NOT DONE"));
-        assert!(!not_done("// NOT DONE"));
-        assert!(!not_done("DONE"));
+        assert!(!contains_not_done_comment("I AM NOT DONE"));
+        assert!(!contains_not_done_comment("// NOT DONE"));
+        assert!(!contains_not_done_comment("DONE"));
     }
 }

From 078f6ffc1cf18546079d03bee99f0903c9e14703 Mon Sep 17 00:00:00 2001
From: mo8it <mo8it@proton.me>
Date: Tue, 26 Mar 2024 02:26:26 +0100
Subject: [PATCH 9/9] Add comments

---
 src/exercise.rs | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/exercise.rs b/src/exercise.rs
index e841aed2..cdf8d205 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -241,6 +241,7 @@ path = "{}.rs""#,
         };
 
         let mut current_line_number: usize = 1;
+        // Keep the last `CONTEXT` lines while iterating over the file lines.
         let mut prev_lines: [_; CONTEXT] = array::from_fn(|_| String::with_capacity(256));
         let mut line = String::with_capacity(256);
 
@@ -260,6 +261,7 @@ path = "{}.rs""#,
 
             if contains_not_done_comment(&line) {
                 let mut context = Vec::with_capacity(2 * CONTEXT + 1);
+                // Previous lines.
                 for (ind, prev_line) in prev_lines
                     .into_iter()
                     .take(current_line_number - 1)
@@ -273,18 +275,22 @@ path = "{}.rs""#,
                     });
                 }
 
+                // Current line.
                 context.push(ContextLine {
                     line,
                     number: current_line_number,
                     important: true,
                 });
 
+                // Next lines.
                 for ind in 0..CONTEXT {
                     let mut next_line = String::with_capacity(256);
                     let Ok(n) = read_line(&mut next_line) else {
+                        // If an error occurs, just ignore the next lines.
                         break;
                     };
 
+                    // Reached the end of the file.
                     if n == 0 {
                         break;
                     }
@@ -300,10 +306,12 @@ path = "{}.rs""#,
             }
 
             current_line_number += 1;
-            // Recycle the buffers.
+            // Add the current line as a previous line and shift the older lines by one.
             for prev_line in &mut prev_lines {
                 mem::swap(&mut line, prev_line);
             }
+            // The current line now contains the oldest previous line.
+            // Recycle it for reading the next line.
             line.clear();
         }
     }