From b43cbc1ba788674fadc3e3b8244aa25e2626675b Mon Sep 17 00:00:00 2001 From: qkzk Date: Fri, 16 Dec 2022 00:02:04 +0100 Subject: massive documentaion and some refactoring. --- src/visited.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/visited.rs') diff --git a/src/visited.rs b/src/visited.rs index b488f0e..1dbd251 100644 --- a/src/visited.rs +++ b/src/visited.rs @@ -1,12 +1,21 @@ use std::path::PathBuf; +/// A Vec of pathbuf of visited files. +/// It's mostly used as a stack but we want to avoid multiple instances of the +/// same path and visit in a certain order. +/// A `BTreeSet` may be used instead. +/// We also need to drop the queue, which isn't easy with a BTreeSet... #[derive(Default, Clone)] pub struct History { + /// The visited paths pub visited: Vec, + /// The currently selected index. By default it's the last one. pub index: usize, } impl History { + /// Add a new path in the stack, without duplicates, and select the last + /// one. pub fn push(&mut self, path: &PathBuf) { if !self.visited.contains(path) { self.visited.push(path.to_owned()); @@ -14,14 +23,17 @@ impl History { } } + /// True if nothing was visited. Shouldn't be the case... pub fn is_empty(&self) -> bool { self.visited.is_empty() } + /// Number of visited paths. pub fn len(&self) -> usize { self.visited.len() } + /// Select the next visited path. pub fn next(&mut self) { if self.is_empty() { self.index = 0 @@ -32,6 +44,7 @@ impl History { } } + /// Select the previously visited path. pub fn prev(&mut self) { if self.is_empty() { self.index = 0; @@ -40,6 +53,7 @@ impl History { } } + /// Returns the currently selected visited path. pub fn selected(&self) -> Option { if self.index < self.len() { Some(self.visited[self.index].clone()) @@ -48,6 +62,8 @@ impl History { } } + /// Drop the last visited paths from the stack, after the selected one. + /// Used to go back a few steps in time. pub fn drop_queue(&mut self) { if self.is_empty() { return; -- cgit v1.2.3