intercept/
database.rs

1/*  Copyright (C) 2012-2018 by László Nagy
2    This file is part of Bear.
3
4    Bear is a tool to generate compilation database for clang tooling.
5
6    Bear is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10
11    Bear is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20use 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}