diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-24 22:36:09 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-24 23:15:13 -0400 |
commit | 2be79f4cd0ee0e89c5cadf5121a8b979391d0d51 (patch) | |
tree | adbd77abb51ea2a4613c78d210896f1ed55b4498 /src/history.rs | |
parent | c3919c91a866a7fd520d7dcb8b99c7d4aafbbb93 (diff) |
major refactoring
- removed parent_list from JoshutoTab struct
- parent_list will behave much like previewing now where the contents
will be retrieved from the dictionary
- completely remove DirHistory struct in favour of trait implemenation
on top of HashMap
Diffstat (limited to 'src/history.rs')
-rw-r--r-- | src/history.rs | 59 |
1 files changed, 28 insertions, 31 deletions
diff --git a/src/history.rs b/src/history.rs index aa98c7f..064dffe 100644 --- a/src/history.rs +++ b/src/history.rs @@ -2,24 +2,29 @@ use std::collections::{hash_map::Entry, HashMap}; use std::path::{Path, PathBuf}; use crate::sort; -use crate::structs; +use crate::structs::JoshutoDirList; -pub struct DirHistory { - map: HashMap<PathBuf, structs::JoshutoDirList>, +pub trait DirectoryHistory { + fn populate_to_root(&mut self, pathbuf: &PathBuf, sort_option: &sort::SortOption); + fn pop_or_create( + &mut self, + path: &Path, + sort_option: &sort::SortOption, + ) -> Result<JoshutoDirList, std::io::Error>; + fn get_mut_or_create( + &mut self, + path: &Path, + sort_option: &sort::SortOption, + ) -> Result<&mut JoshutoDirList, std::io::Error>; + fn depreciate_all_entries(&mut self); } -impl DirHistory { - pub fn new() -> Self { - DirHistory { - map: HashMap::new(), - } - } - - pub fn populate_to_root(&mut self, pathbuf: &PathBuf, sort_option: &sort::SortOption) { +impl DirectoryHistory for HashMap<PathBuf, JoshutoDirList> { + fn populate_to_root(&mut self, pathbuf: &PathBuf, sort_option: &sort::SortOption) { let mut ancestors = pathbuf.ancestors(); if let Some(mut ancestor) = ancestors.next() { for curr in ancestors { - match structs::JoshutoDirList::new(curr.to_path_buf().clone(), sort_option) { + match JoshutoDirList::new(curr.to_path_buf().clone(), sort_option) { Ok(mut s) => { let index = s.contents.iter().enumerate().find_map(|(i, dir)| { if dir.path == ancestor { @@ -31,7 +36,7 @@ impl DirHistory { if let Some(i) = index { s.index = Some(i); } - self.map.insert(curr.to_path_buf(), s); + self.insert(curr.to_path_buf(), s); } Err(e) => eprintln!("populate_to_root: {}", e), }; @@ -39,13 +44,12 @@ impl DirHistory { } } } - - pub fn pop_or_create( + fn pop_or_create( &mut self, path: &Path, sort_option: &sort::SortOption, - ) -> Result<structs::JoshutoDirList, std::io::Error> { - match self.map.remove(&path.to_path_buf()) { + ) -> Result<JoshutoDirList, std::io::Error> { + match self.remove(&path.to_path_buf()) { Some(mut dir_entry) => { if dir_entry.need_update() { dir_entry.update_contents(&sort_option)? @@ -54,17 +58,16 @@ impl DirHistory { } None => { let path_clone = path.to_path_buf(); - structs::JoshutoDirList::new(path_clone, &sort_option) + JoshutoDirList::new(path_clone, &sort_option) } } } - - pub fn get_mut_or_create( + fn get_mut_or_create( &mut self, path: &Path, sort_option: &sort::SortOption, - ) -> Result<&mut structs::JoshutoDirList, std::io::Error> { - match self.map.entry(path.to_path_buf().clone()) { + ) -> Result<&mut JoshutoDirList, std::io::Error> { + match self.entry(path.to_path_buf().clone()) { Entry::Occupied(mut entry) => { let dir_entry = entry.get_mut(); if dir_entry.need_update() { @@ -73,19 +76,13 @@ impl DirHistory { Ok(entry.into_mut()) } Entry::Vacant(entry) => { - let s = structs::JoshutoDirList::new(path.to_path_buf(), &sort_option)?; + let s = JoshutoDirList::new(path.to_path_buf(), &sort_option)?; Ok(entry.insert(s)) } } } - pub fn put_back(&mut self, dirlist: Option<structs::JoshutoDirList>) { - if let Some(s) = dirlist { - self.map.insert(s.path.clone(), s); - } - } - - pub fn depecrate_all_entries(&mut self) { - self.map.iter_mut().for_each(|(_, v)| v.depreciate()); + fn depreciate_all_entries(&mut self) { + self.iter_mut().for_each(|(_, v)| v.depreciate()); } } |