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
use std::collections;
use std::io;
use std::iter::FromIterator;
use std::path;
use serde_json;
use Result;
#[derive(Hash, Serialize, Deserialize)]
pub struct Entry {
directory: path::PathBuf,
file: path::PathBuf,
command: Vec<String>,
output: Option<path::PathBuf>
}
impl Entry {
pub fn new(directory: path::PathBuf,
file: path::PathBuf,
output: Option<path::PathBuf>,
arguments: Vec<String>) -> Entry {
Entry {
directory: directory,
file: file,
command: arguments,
output: output
}
}
pub fn get_directory(&self) -> &path::Path {
&self.directory
}
pub fn get_file(&self) -> &path::Path {
&self.file
}
pub fn get_command(&self) -> &[String] {
&self.command
}
pub fn get_output(&self) -> &Option<path::PathBuf> {
&self.output
}
}
impl PartialEq for Entry {
fn eq(&self, other: &Entry) -> bool {
self.directory == other.directory &&
self.file == other.file &&
self.command == other.command
}
}
impl Eq for Entry {}
pub struct Database {
entries: collections::HashSet<Entry>
}
impl Database {
pub fn new() -> Database {
Database { entries: collections::HashSet::new() }
}
pub fn load(&mut self, source: &mut io::Read) -> Result<()> {
let entries: Vec<Entry> = serde_json::from_reader(source)?;
let result = self.add_entries(entries);
Ok(result)
}
pub fn save(&self, target: &mut io::Write) -> Result<()> {
let values = Vec::from_iter(self.entries.iter());
let result = serde_json::to_writer(target, &values)?;
Ok(result)
}
pub fn add_entry(&mut self, entry: Entry) -> () {
self.entries.insert(entry);
}
pub fn add_entries(&mut self, entries: Vec<Entry>) -> () {
let fresh: collections::HashSet<Entry> = collections::HashSet::from_iter(entries);
self.entries.union(&fresh);
}
}