summaryrefslogtreecommitdiffstats
path: root/src/history.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/history.rs')
-rw-r--r--src/history.rs86
1 files changed, 27 insertions, 59 deletions
diff --git a/src/history.rs b/src/history.rs
index e843d29..036912e 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -10,16 +10,11 @@ pub trait DirectoryHistory {
path: &Path,
sort_option: &sort::SortOption,
) -> std::io::Result<()>;
- fn pop_or_create(
+ fn create_or_update(
&mut self,
path: &Path,
sort_option: &sort::SortOption,
- ) -> std::io::Result<JoshutoDirList>;
- fn get_mut_or_create(
- &mut self,
- path: &Path,
- sort_option: &sort::SortOption,
- ) -> std::io::Result<&mut JoshutoDirList>;
+ ) -> std::io::Result<()>;
fn depreciate_all_entries(&mut self);
}
@@ -31,77 +26,50 @@ impl DirectoryHistory for JoshutoHistory {
path: &Path,
sort_option: &sort::SortOption,
) -> std::io::Result<()> {
- let mut ancestors = path.ancestors();
- if let Some(mut ancestor) = ancestors.next() {
- for curr in ancestors {
- match self.entry(curr.to_path_buf()) {
- Entry::Occupied(mut entry) => {
- let dirlist = entry.get_mut();
- dirlist.reload_contents(sort_option)?;
- if let Some(i) = get_index_of_value(&dirlist.contents, &ancestor) {
+ let mut prev: Option<&Path> = None;
+ for curr in path.ancestors() {
+ match self.entry(curr.to_path_buf()) {
+ Entry::Occupied(mut entry) => {
+ let dirlist = entry.get_mut();
+ dirlist.reload_contents(sort_option)?;
+ if let Some(ancestor) = prev.as_ref() {
+ if let Some(i) = get_index_of_value(&dirlist.contents, ancestor) {
dirlist.index = Some(i);
}
}
- Entry::Vacant(entry) => {
- let mut dirlist =
- JoshutoDirList::new(curr.to_path_buf().clone(), sort_option)?;
- if let Some(i) = get_index_of_value(&dirlist.contents, &ancestor) {
+ prev = Some(curr);
+ }
+ Entry::Vacant(entry) => {
+ let mut dirlist = JoshutoDirList::new(curr.to_path_buf().clone(), sort_option)?;
+ if let Some(ancestor) = prev.as_ref() {
+ if let Some(i) = get_index_of_value(&dirlist.contents, ancestor) {
dirlist.index = Some(i);
}
- entry.insert(dirlist);
}
+ entry.insert(dirlist);
+ prev = Some(curr);
}
- ancestor = curr;
}
}
Ok(())
}
- fn pop_or_create(
+ fn create_or_update(
&mut self,
path: &Path,
sort_option: &sort::SortOption,
- ) -> std::io::Result<JoshutoDirList> {
- match self.remove(&path.to_path_buf()) {
- Some(mut dirlist) => {
- if dirlist.need_update() {
- dirlist.reload_contents(&sort_option)?
- } else {
- let metadata = std::fs::symlink_metadata(dirlist.file_path())?;
-
- let modified = metadata.modified()?;
- if modified > dirlist.metadata.modified {
- dirlist.reload_contents(&sort_option)?
- }
- }
- Ok(dirlist)
- }
- None => {
- let path_clone = path.to_path_buf();
- let dirlist = JoshutoDirList::new(path_clone, &sort_option)?;
- Ok(dirlist)
- }
- }
- }
- fn get_mut_or_create(
- &mut self,
- path: &Path,
- sort_option: &sort::SortOption,
- ) -> std::io::Result<&mut JoshutoDirList> {
- match self.entry(path.to_path_buf().clone()) {
- Entry::Occupied(entry) => {
- /*
- if dir_entry.need_update() {
- dir_entry.reload_contents(&sort_option)?;
- }
- */
- Ok(entry.into_mut())
+ ) -> std::io::Result<()> {
+ match self.entry(path.to_path_buf()) {
+ Entry::Occupied(mut entry) => {
+ let dirlist = entry.get_mut();
+ dirlist.reload_contents(sort_option)?;
}
Entry::Vacant(entry) => {
- let s = JoshutoDirList::new(path.to_path_buf(), &sort_option)?;
- Ok(entry.insert(s))
+ let mut dirlist = JoshutoDirList::new(path.to_path_buf(), sort_option)?;
+ entry.insert(dirlist);
}
}
+ Ok(())
}
fn depreciate_all_entries(&mut self) {