summaryrefslogtreecommitdiffstats
path: root/src/history.rs
blob: 7c6076fbc0889ed453a33e9c7d98319988274dbc (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
use std::collections::{hash_map::Entry, HashMap};
use std::path::{Path, PathBuf};

use crate::sort;
use crate::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);
}

pub type JoshutoHistory = HashMap<PathBuf, JoshutoDirList>;

impl DirectoryHistory for JoshutoHistory {
    fn populate_to_root(&mut self, pathbuf: &PathBuf, sort_option: &sort::SortOption) {
        let mut ancestors = pathbuf.ancestors();
        match ancestors.next() {
            None => {}
            Some(mut ancestor) => for curr in ancestors {
                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 {
                                Some(i)
                            } else {
                                None
                            }
                        });
                        if let Some(i) = index {
                            s.index = Some(i);
                        }
                        self.insert(curr.to_path_buf(), s);
                    }
                    Err(e) => eprintln!("populate_to_root: {}", e),
                }
                ancestor = curr;
            }
        }
    }

    fn pop_or_create(
        &mut self,
        path: &Path,
        sort_option: &sort::SortOption,
    ) -> 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)?
                } else {
                    let metadata = std::fs::symlink_metadata(&dir_entry.path)?;

                    let modified = metadata.modified()?;
                    if modified > dir_entry.metadata.modified {
                        dir_entry.update_contents(&sort_option)?
                    }
                }
                Ok(dir_entry)
            }
            None => {
                let path_clone = path.to_path_buf();
                JoshutoDirList::new(path_clone, &sort_option)
            }
        }
    }
    fn get_mut_or_create(
        &mut self,
        path: &Path,
        sort_option: &sort::SortOption,
    ) -> Result<&mut JoshutoDirList, std::io::Error> {
        match self.entry(path.to_path_buf().clone()) {
            Entry::Occupied(entry) => {
                /*
                                if dir_entry.need_update() {
                                    dir_entry.update_contents(&sort_option)?;
                                }
                */
                Ok(entry.into_mut())
            }
            Entry::Vacant(entry) => {
                let s = JoshutoDirList::new(path.to_path_buf(), &sort_option)?;
                Ok(entry.insert(s))
            }
        }
    }

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