diff options
Diffstat (limited to 'src/joshuto/history.rs')
-rw-r--r-- | src/joshuto/history.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/joshuto/history.rs b/src/joshuto/history.rs index 58445ef..dbf2f68 100644 --- a/src/joshuto/history.rs +++ b/src/joshuto/history.rs @@ -4,6 +4,7 @@ use std::path; use joshuto::structs; use joshuto::sort; +use std::collections::hash_map::Entry; pub struct DirHistory { map: HashMap<path::PathBuf, structs::JoshutoDirList>, @@ -67,6 +68,34 @@ impl DirHistory { } } + pub fn get_mut_or_create(&mut self, path: &path::Path, + sort_type: &sort::SortType) + -> Option<&mut structs::JoshutoDirList> + { + let pathbuf = path.to_path_buf(); + + { + let entry = self.map.entry(pathbuf.clone()); + match entry { + Entry::Occupied(mut entry) => { + { + let dir_entry = entry.get_mut(); + if dir_entry.need_update() { + dir_entry.update(sort_type); + } + } + }, + Entry::Vacant(entry) => { + if let Ok(s) = structs::JoshutoDirList::new(path.clone().to_path_buf(), &sort_type) { + entry.insert(s); + } + }, + }; + } + + self.map.get_mut(&pathbuf) + } + pub fn put_back(&mut self, dirlist: Option<structs::JoshutoDirList>) { if let Some(s) = dirlist { |