-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(history): Tweak initial impl * feat(history): Add new keybindings to serialization * chore(history): Add a sanity test
- Loading branch information
1 parent
7b525a6
commit 16e2818
Showing
6 changed files
with
85 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,76 @@ | ||
use smart_debug::SmartDebug; | ||
use std::path::PathBuf; | ||
use std::path::{Path, PathBuf}; | ||
|
||
#[derive(SmartDebug, Clone, PartialEq)] | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct History { | ||
history: Vec<PathBuf>, | ||
index: usize, | ||
} | ||
|
||
impl History { | ||
pub fn new(path_buf: PathBuf) -> Self { | ||
History { | ||
Self { | ||
history: vec![path_buf], | ||
index: 0, | ||
} | ||
} | ||
pub fn truncate(&mut self) { | ||
if self.index + 1 < self.history.len() { | ||
self.history.truncate(self.index + 1); | ||
} | ||
|
||
pub fn get_path(&self) -> &Path { | ||
self.history | ||
.get(self.index) | ||
.expect("History should always be in bounds") | ||
.as_path() | ||
} | ||
pub fn append(&mut self, file_path: PathBuf) { | ||
|
||
pub fn make_next(&mut self, file_path: PathBuf) { | ||
self.history.truncate(self.index + 1); | ||
self.history.push(file_path); | ||
self.index += 1; | ||
} | ||
pub fn get_path(&self) -> &PathBuf { | ||
self.history | ||
.get(self.index) | ||
.expect("History should be bound checked for all possible indexes.") | ||
} | ||
|
||
#[allow(clippy::should_implement_trait)] | ||
pub fn next(&mut self) -> Option<&PathBuf> { | ||
pub fn next(&mut self) -> Option<&Path> { | ||
if self.index + 1 == self.history.len() { | ||
return None; | ||
None | ||
} else { | ||
self.index += 1; | ||
Some(self.get_path()) | ||
} | ||
self.index += 1; | ||
Some(self.get_path()) | ||
} | ||
pub fn previous(&mut self) -> Option<&PathBuf> { | ||
|
||
pub fn previous(&mut self) -> Option<&Path> { | ||
if self.index == 0 { | ||
return None; | ||
None | ||
} else { | ||
self.index -= 1; | ||
Some(self.get_path()) | ||
} | ||
self.index -= 1; | ||
Some(self.get_path()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn sanity() { | ||
let root = PathBuf::from("a"); | ||
let fork1 = PathBuf::from("b"); | ||
let fork2 = PathBuf::from("c"); | ||
|
||
let mut hist = History::new(root.clone()); | ||
assert_eq!(hist.get_path(), root); | ||
assert_eq!(hist.previous(), None); | ||
|
||
hist.make_next(fork1.clone()); | ||
assert_eq!(hist.get_path(), fork1); | ||
|
||
assert_eq!(hist.previous().unwrap(), root); | ||
hist.make_next(fork2.clone()); | ||
assert_eq!(hist.get_path(), fork2); | ||
|
||
assert_eq!(hist.previous().unwrap(), root); | ||
assert_eq!(hist.previous(), None); | ||
assert_eq!(hist.next().unwrap(), fork2); | ||
assert_eq!(hist.next(), None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters