1use std::collections;
21use std::io;
22use std::iter::FromIterator;
23use std::path;
24use serde_json;
25use Result;
26
27
28#[derive(Hash, Serialize, Deserialize)]
29pub struct Entry {
30 directory: path::PathBuf,
31 file: path::PathBuf,
32 command: Vec<String>,
33 output: Option<path::PathBuf>
34}
35
36impl Entry {
37 pub fn new(directory: path::PathBuf,
38 file: path::PathBuf,
39 output: Option<path::PathBuf>,
40 arguments: Vec<String>) -> Entry {
41 Entry {
42 directory: directory,
43 file: file,
44 command: arguments,
45 output: output
46 }
47 }
48
49 pub fn get_directory(&self) -> &path::Path {
50 &self.directory
51 }
52
53 pub fn get_file(&self) -> &path::Path {
54 &self.file
55 }
56
57 pub fn get_command(&self) -> &[String] {
58 &self.command
59 }
60
61 pub fn get_output(&self) -> &Option<path::PathBuf> {
62 &self.output
63 }
64}
65
66impl PartialEq for Entry {
67 fn eq(&self, other: &Entry) -> bool {
68 self.directory == other.directory &&
69 self.file == other.file &&
70 self.command == other.command
71 }
72}
73
74impl Eq for Entry {}
75
76
77pub struct Database {
78 entries: collections::HashSet<Entry>
79}
80
81impl Database {
82 pub fn new() -> Database {
83 Database { entries: collections::HashSet::new() }
84 }
85
86 pub fn load(&mut self, source: &mut io::Read) -> Result<()> {
87 let entries: Vec<Entry> = serde_json::from_reader(source)?;
88 let result = self.add_entries(entries);
89 Ok(result)
90 }
91
92 pub fn save(&self, target: &mut io::Write) -> Result<()> {
93 let values = Vec::from_iter(self.entries.iter());
94 let result = serde_json::to_writer(target, &values)?;
95 Ok(result)
96 }
97
98 pub fn add_entry(&mut self, entry: Entry) -> () {
99 self.entries.insert(entry);
100 }
101
102 pub fn add_entries(&mut self, entries: Vec<Entry>) -> () {
103 let fresh: collections::HashSet<Entry> = collections::HashSet::from_iter(entries);
104 self.entries.union(&fresh);
105 }
106}