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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
/// Items to be stored in the Shelf
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Item {
title: String,
authors: BTreeSet<String>,
genres: BTreeSet<String>,
}
impl Item {
pub fn new(title: String, authors: BTreeSet<String>, genres: BTreeSet<String>) -> Item {
Item {
title,
authors,
genres,
}
}
/// Get the title, authors, and genres of the item
pub fn export(&self) -> (&String, &BTreeSet<String>, &BTreeSet<String>) {
return (&self.title, &self.authors, &self.genres);
}
}
/// The shelf indexes all items and a list of favorites
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Shelf {
index: BTreeMap<(String, String), Item>,
favorites: BTreeSet<(String, String)>,
}
impl Shelf {
pub fn new() -> Shelf {
Shelf {
index: BTreeMap::new(),
favorites: BTreeSet::new(),
}
}
/// Get the index
pub fn get_index(&self) -> &BTreeMap<(String, String), Item> {
&self.index
}
/// Check it an item is in the shelf
pub fn has_item(&self, module: &str, code: &str) -> bool {
self.index
.contains_key(&(module.to_string(), code.to_string()))
}
// Add to index but not download
pub fn add_item(
&mut self,
module: &str,
code: &str,
title: String,
authors: BTreeSet<String>,
genres: BTreeSet<String>,
) {
self.index.insert(
(module.to_string(), code.to_string()),
Item::new(title, authors, genres),
);
}
/// Search for items matching the provided parameters
pub fn search_item(
&self,
module: Option<&str>,
title_regex: Option<&str>,
authors: Option<&str>,
genres: Option<&str>,
blacklist: Option<&str>,
broad_search: bool,
favorite: bool,
) -> Result<BTreeSet<(String, String)>, regex::Error> {
// --favorite
let mut result: BTreeSet<(String, String)> = if favorite {
self.favorites.clone()
} else {
self.index.keys().cloned().collect()
};
// --modules
if let Some(module) = module {
let tmp = result.clone();
for (m, c) in tmp {
if m != module {
result.remove(&(m, c));
}
}
}
// --authors (match if any author matches)
if let Some(authors) = authors {
for key in result.clone() {
for author in authors.split(",") {
if let Some(item) = self.index.get(&key) {
if !item.authors.contains(&author.to_string()) {
result.remove(&key);
}
} else {
result.remove(&key);
}
}
}
}
// --genres
if let Some(genres) = genres {
if broad_search {
// broad search (match item if at least 1 genre matches)
for key in result.clone() {
for genre in genres.split(",") {
if let Some(item) = self.index.get(&key) {
if !item.genres.contains(&genre.to_string()) {
result.remove(&key);
}
} else {
result.remove(&key);
}
}
}
} else {
// normal search (match item if all genres match)
for key in result.clone() {
let mut matches: bool = true;
for genre in genres.split(",") {
if let Some(item) = self.index.get(&key) {
if !item.genres.contains(&genre.to_string()) {
matches = false;
}
} else {
result.remove(&key);
}
}
if !matches {
result.remove(&key);
}
}
}
}
// --blacklist
if let Some(blacklist) = blacklist {
for key in result.clone() {
for genre in blacklist.split(",") {
if let Some(item) = self.index.get(&key) {
if item.genres.contains(&genre.to_string()) {
result.remove(&key);
}
} else {
result.remove(&key);
}
}
}
}
// --title (match regex against title)
if let Some(title_regex) = title_regex {
match Regex::new(title_regex) {
Ok(regex) => {
for key in result.clone() {
if let Some(item) = self.index.get(&key) {
if !®ex.is_match(&item.title) {
result.remove(&key);
}
} else {
result.remove(&key);
}
}
}
Err(e) => {
return Err(e);
}
}
}
Ok(result)
}
/// Remove item from index (and favorites)
pub fn remove_item(&mut self, module: &str, code: &str) {
let key: (String, String) = (module.to_string(), code.to_string());
self.index.remove(&key);
self.favorites.remove(&key);
}
/// Get the item corresponding to the module and code
pub fn get_item(&self, module: &str, code: &str) -> Option<&Item> {
self.index.get(&(module.to_string(), code.to_string()))
}
/// Edit item with provided parameters
pub fn edit_item(
&mut self,
module: Option<&str>,
code: Option<&str>,
title: Option<&str>,
authors: Option<&str>,
genres: Option<&str>,
favorite: bool,
) {
// these 2 are required and can be safely unwrap'd
let k = (module.unwrap().to_string(), code.unwrap().to_string());
// update values
if let Some(item) = self.index.get_mut(&k) {
if let Some(t) = title {
item.title = t.to_string();
}
if let Some(s) = authors {
let mut authors: BTreeSet<String> = BTreeSet::new();
for author in s.split(",") {
authors.insert(author.to_string());
}
item.authors = authors;
}
if let Some(t) = genres {
let mut genres: BTreeSet<String> = BTreeSet::new();
for genre in t.split(",") {
genres.insert(genre.to_string());
}
item.genres = genres;
}
}
// insert if item wasn't in favorites
if favorite {
if !self.favorites.remove(&k) {
self.favorites.insert(k);
}
}
}
/// Import a shelf into self, extending self's index and favorites
pub fn import(&mut self, new_shelf: &Shelf) {
// index
for ((module, code), item) in new_shelf.index.iter() {
self.index
.insert((module.clone(), code.clone()), item.clone());
}
// favorites
for (module, code) in new_shelf.favorites.iter() {
self.favorites.insert((module.clone(), code.clone()));
}
}
}