summaryrefslogtreecommitdiffstats
path: root/src/history.rs
blob: 60d9641ccd485f990799658798c35d84813151b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::collections::{hash_map::Entry, HashMap};
use std::path::{Path, PathBuf};

use crate::fs::{JoshutoDirEntry, JoshutoDirList};
use crate::util::sort;

pub trait DirectoryHistory {
    fn populate_to_root(
        &mut self,
        path: &Path,
        sort_option: &sort::SortOption,
    ) -> std::io::Result<()>;
    fn create_or_soft_update(
        &mut self,
        path: &Path,
        sort_option: &sort::SortOption,
    ) -> std::io::Result<()>;
    fn create_or_reload(
        &mut self,
        path: &Path,
        sort_option: &sort::SortOption,
    ) -> std::io::Result<()>;
    fn reload(&mut self, path: &Path, sort_option: &sort::SortOption) -> std::io::Result<()>;
    fn depreciate_all_entries(&mut self);

    fn depreciate_entry(&mut self, path: &Path);
}

pub type JoshutoHistory = HashMap<PathBuf, JoshutoDirList>;

impl DirectoryHistory for JoshutoHistory {
    fn populate_to_root(
        &mut self,
        path: &Path,
        sort_option: &sort::SortOption,
    ) -> std::io::Result<()> {
        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);
                        }
                    }
                    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);
                    prev = Some(curr);
                }
            }
        }
        Ok(())
    }

    fn create_or_soft_update(
        &mut self,
        path: &Path,
        sort_option: &sort::SortOption,
    ) -> std::io::Result<()> {
        match self.entry(path.to_path_buf()) {
            Entry::Occupied(mut entry) => {
                let dirlist = entry.get_mut();
                if dirlist.need_update() {
                    dirlist.reload_contents(sort_option)?;
                }
            }
            Entry::Vacant(entry) => {
                let dirlist = JoshutoDirList::new(path.to_path_buf(), sort_option)?;
                entry.insert(dirlist);
            }
        }
        Ok(())
    }

    fn create_or_reload(
        &mut self,
        path: &Path,
        sort_option: &sort::SortOption,
    ) -> 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 dirlist = JoshutoDirList::new(path.to_path_buf(), sort_option)?;
                entry.insert(dirlist);
            }
        }
        Ok(())
    }

    fn reload(&mut self, path: &Path, sort_option: &sort::SortOption) -> std::io::Result<()> {
        if let Entry::Occupied(mut entry) = self.entry(path.to_path_buf()) {
            let dirlist = entry.get_mut();
            dirlist.reload_contents(sort_option)?;
        }
        Ok(())
    }

    fn depreciate_all_entries(&mut self) {
        self.iter_mut().for_each(|(_, v)| v.depreciate());
    }

    fn depreciate_entry(&mut self, path: &Path) {
        if let Some(v) = self.get_mut(path) {
            v.depreciate();
        }
    }
}

fn get_index_of_value(arr: &[JoshutoDirEntry], val: &Path) -> Option<usize> {
    arr.iter().enumerate().find_map(|(i, dir)| {
        if dir.file_path() == val {
            Some(i)
        } else {
            None
        }
    })
}