summaryrefslogtreecommitdiffstats
path: root/src/joshuto/history.rs
blob: dbf2f6896b24b9a51927d8e396b47ae85c7c148d (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
use std;
use std::collections::HashMap;
use std::path;

use joshuto::structs;
use joshuto::sort;
use std::collections::hash_map::Entry;

pub struct DirHistory {
    map: HashMap<path::PathBuf, structs::JoshutoDirList>,
}

impl DirHistory {

    pub fn new() -> Self
    {
        DirHistory {
            map: HashMap::new()
        }
    }

    pub fn insert(&mut self, dirlist: structs::JoshutoDirList)
    {
        self.map.insert(dirlist.path.clone(), dirlist);
    }

    pub fn populate_to_root(&mut self, pathbuf: &path::PathBuf,
       sort_type: &sort::SortType)
    {
        let mut pathbuf = pathbuf.clone();

        while pathbuf.parent() != None {
            {
                let parent = pathbuf.parent().unwrap().to_path_buf();
                match structs::JoshutoDirList::new(parent.clone(), sort_type) {
                    Ok(mut s) => {
                        for (i, dirent) in s.contents.iter().enumerate() {
                            if dirent.path == pathbuf {
                                s.index = i as i32;
                                break;
                            }
                        }
                        self.map.insert(parent, s);
                    },
                    Err(e) => eprintln!("{}", e),
                };
            }
            if pathbuf.pop() == false {
                break;
            }
        }
    }

    pub fn pop_or_create(&mut self, path : &path::Path,
       sort_type: &sort::SortType)
            -> Result<structs::JoshutoDirList, std::io::Error>
    {
        match self.map.remove(&path.to_path_buf()) {
            Some(mut dir_entry) => {
                if dir_entry.need_update() {
                    dir_entry.update(&sort_type);
                }
                Ok(dir_entry)
            },
            None => {
                structs::JoshutoDirList::new(path.clone().to_path_buf(), &sort_type)
            }
        }
    }

    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 {
            self.map.insert(s.path.clone(), s);
        }
    }

    pub fn depecrate_all_entries(&mut self)
    {
        self.map.iter_mut().for_each(|(_, v)| v.update_needed = true);
    }
}