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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
extern crate serde_yaml as yamls;
extern crate xdg;
use std::io::prelude::*;
use std::fmt;
use std::fs::File;
use std::path::Path;
use itertools::Itertools;
use subscription;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Config {
pub cache_location: String,
subscriptions: Vec<subscription::Subscription>,
}
impl Config {
pub fn new(cache_location: Option<String>) -> Config {
Config {
cache_location: process_location(cache_location).to_string(),
subscriptions: Vec::new(),
}
}
pub fn load_cache(&mut self) {
self.subscriptions = subscription::file_deserialize(&self.cache_location).unwrap()
}
pub fn get_names(&self) -> Vec<String> {
self.subscriptions
.clone()
.into_iter()
.map(|s| s.name)
.collect::<Vec<String>>()
}
pub fn get_entry_counts(&self) -> Vec<u64> {
self.subscriptions
.clone()
.into_iter()
.map(|s| s.get_latest_entry_number())
.collect::<Vec<u64>>()
}
pub fn get_highest_entry_count_sub(&self) -> subscription::Subscription {
self.subscriptions
.clone()
.into_iter()
.sorted_by(|b, a| Ord::cmp(&a.get_latest_entry_number(), &b.get_latest_entry_number()))
.into_iter()
.collect::<Vec<subscription::Subscription>>()
.first()
.unwrap()
.clone()
}
pub fn get_highest_entry_count_sub_name(&self) -> String {
self.get_highest_entry_count_sub().name
}
}
impl fmt::Display for Config {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#?}", self)
}
}
fn process_location(cache_location: Option<String>) -> String {
if cache_location != None {
return cache_location.unwrap();
}
let xdg_dirs = xdg::BaseDirectories::with_prefix("puckfetcher").unwrap();
let op_cache_file = xdg_dirs.find_cache_file("puckcache");
if op_cache_file == None {
panic!("No puckfetcher cache available");
}
let path = op_cache_file.unwrap();
let file_str = path.to_str().unwrap().to_string();
return file_str;
}
pub fn read_config() -> Option<Config> {
let xdg_dirs = xdg::BaseDirectories::with_prefix("podstats").unwrap();
let op_config_path = xdg_dirs.find_config_file("config.yaml");
if op_config_path == None {
return None;
}
let config_path = op_config_path.unwrap();
let path = Path::new(&config_path);
let display = path.display();
let mut file = match File::open(&path) {
Err(why) => panic!("couldn't open {}: {:?}", display, why),
Ok(file) => file,
};
let mut buffer = Vec::new();
match file.read_to_end(&mut buffer) {
Err(why) => panic!("couldn't read {}: {}", display, why),
Ok(_) => (),
}
let op_config = yamls::from_slice(buffer.as_slice());
match op_config {
Ok(config) => return Some(config),
Err(_) => return None,
}
}
pub fn write_config(config: Config) {
let op_config = yamls::to_vec(&config);
match op_config {
Err(why) => panic!("couldn't encode config: {}", why),
Ok(_) => (),
}
let xdg_dirs = xdg::BaseDirectories::with_prefix("podstats").unwrap();
let mut op_config_path = xdg_dirs.find_config_file("config.yaml");
if op_config_path == None {
let path_res = xdg_dirs.place_config_file("config.yaml");
match path_res {
Err(why) => panic!("Couldn't find path for config: {}", why),
Ok(_) => (),
};
let config_path = path_res.unwrap();
let path = Path::new(&config_path);
let mut file = match File::create(path) {
Err(why) => panic!("couldn't create file: {:?}", why),
Ok(file) => file,
};
file.write_all(b"");
op_config_path = xdg_dirs.find_config_file("config.yaml");
}
let config_path = op_config_path.unwrap();
let path = Path::new(&config_path);
let display = path.display();
let mut file = match File::create(&path) {
Err(why) => panic!("couldn't create {}: {:?}", display, why),
Ok(file) => file,
};
let res = file.write(op_config.unwrap().as_slice());
let bytes = match res {
Ok(n) => n,
Err(why) => panic!("couldn't write to file: {}", why),
};
file.flush();
}
#[cfg(test)]
mod tests {
use std::io::prelude::*;
use std::fs;
use std::fs::File;
use std::path::Path;
use subscription;
use config;
fn setup_loaded_cache(loc: Option<&str>,
subs: Option<Vec<subscription::Subscription>>)
-> config::Config {
let test_cache_loc = match loc {
Some(l) => l,
None => "testcache",
};
let mut config = config::Config::new(Some(test_cache_loc.to_string()));
let unpacked_subs = match subs {
Some(s) => s,
None => {
let sub1 = subscription::Subscription::new("testurl1", "testname1", None);
let sub2 = subscription::Subscription::new("testurl2", "testname2", None);
let mut subs = Vec::new();
subs.push(sub1);
subs.push(sub2);
subs
}
};
let s = subscription::vec_serialize(&unpacked_subs);
let path = Path::new(test_cache_loc);
let display = path.display();
let mut file = match File::create(&path) {
Err(why) => panic!("couldn't create {}: {}", display, why),
Ok(file) => file,
};
match file.write_all(s.as_slice()) {
Err(why) => panic!("couldn't write to {}: {}", display, why),
Ok(_) => println!("successfully wrote to {}", display),
}
config.load_cache();
fs::remove_file(test_cache_loc);
return config;
}
#[test]
fn test_get_names() {
let conf = setup_loaded_cache(Some("testcache1"), None);
let mut n = Vec::new();
n.push("testname1");
n.push("testname2");
let names = conf.get_names();
assert_eq!(n, names);
}
#[test]
fn test_get_entry_counts() {
let conf = setup_loaded_cache(Some("testcache2"), None);
let mut l_vec = Vec::new();
l_vec.push(0);
l_vec.push(0);
let latest_vec = conf.get_entry_counts();
assert_eq!(l_vec, latest_vec);
}
#[test]
fn test_get_highest_entry_count_sub() {
let sub1 = subscription::Subscription::new("testurl1", "testname1", None);
let sub2 = subscription::Subscription::new("testurl2", "testname2", None);
let mut subs = Vec::new();
subs.push(sub1.clone());
subs.push(sub2.clone());
let conf = setup_loaded_cache(Some("testcache3"), Some(subs));
let sub = conf.get_highest_entry_count_sub();
assert_eq!(sub1, sub);
}
#[test]
fn test_get_highest_entry_count_sub_name() {
let sub1 = subscription::Subscription::new("testurl1", "testname1", None);
let sub2 = subscription::Subscription::new("testurl2", "testname2", None);
let mut subs = Vec::new();
subs.push(sub1.clone());
subs.push(sub2.clone());
let conf = setup_loaded_cache(Some("testcache4"), Some(subs));
let name = conf.get_highest_entry_count_sub_name();
assert_eq!(sub1.name, name);
}
}