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
extern crate serde;
extern crate rmp;
extern crate rmp_serde as rmps;
use std::error::Error;
use std::fmt;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Subscription {
pub url: String,
pub original_url: String,
pub name: String,
pub directory: String,
pub backlog_limit: u64,
pub use_title_as_filename: bool,
feed_state: FeedState,
}
impl Subscription {
pub fn new(url: &str, name: &str, directory: Option<&str>) -> Subscription {
Subscription {
url: url.to_string(),
original_url: url.to_string(),
name: name.to_string(),
directory: process_directory(directory),
backlog_limit: 0,
use_title_as_filename: false,
feed_state: FeedState {
latest_entry_number: 0,
queue: Vec::new(),
entries: Vec::new(),
summary_queue: Vec::new(),
},
}
}
pub fn get_latest_entry_number(&self) -> u64 {
self.feed_state.latest_entry_number
}
}
impl fmt::Display for Subscription {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#?}", self)
}
}
pub fn vec_serialize(subs: &Vec<Subscription>) -> Vec<u8> {
let op_vec = rmps::to_vec(&subs);
match op_vec {
Ok(t) => return t,
Err(_) => return Vec::new(),
};
}
pub fn serialize(sub: &Subscription) -> Vec<u8> {
let op_vec = rmps::to_vec(&sub);
match op_vec {
Ok(t) => return t,
Err(_) => return Vec::new(),
};
}
pub fn deserialize(sub_vec: &Vec<u8>) -> Option<Subscription> {
let slice: &[u8] = sub_vec.as_slice();
let op_sub = rmps::from_slice(&slice);
match op_sub {
Ok(op_sub) => return Some(op_sub),
Err(_) => return None,
}
}
pub fn vec_deserialize(sub_vec: &Vec<u8>) -> Option<Vec<Subscription>> {
let slice: &[u8] = sub_vec.as_slice();
let op_subs = rmps::from_slice(&slice);
match op_subs {
Ok(op_sub) => return Some(op_sub),
Err(why) => panic!("{:#?}", why),
}
}
pub fn file_deserialize(path: &str) -> Option<Vec<Subscription>> {
let path = Path::new(&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.description()),
Ok(_) => (),
}
return vec_deserialize(&buffer);
}
fn process_directory(directory: Option<&str>) -> String {
match directory {
Some(x) => return x.to_string(),
None => return "fakedir".to_string(),
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
struct FeedState {
entries: Vec<Entry>,
queue: Vec<Entry>,
latest_entry_number: u64,
summary_queue: Vec<SummaryEntry>,
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
struct Entry {
title: String,
urls: Vec<String>,
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
struct SummaryEntry {
is_this_session: bool,
number: u64,
name: String,
}
impl fmt::Display for FeedState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#?}", self)
}
}
#[test]
fn serialize_deserialize_test() {
let sub = Subscription::new("testurl", "testname", None);
let s = serialize(&sub);
let re_sub = deserialize(&s);
assert_eq!(sub, re_sub.unwrap());
}
#[test]
fn vec_serialize_deserialize_test() {
let sub = Subscription::new("testurl", "testname", None);
let mut subs = Vec::new();
subs.push(sub);
let s = vec_serialize(&subs);
let re_subs = vec_deserialize(&s);
assert_eq!(subs, re_subs.unwrap());
}
#[test]
fn file_serialize_test() {
let test_path = "tmp_test.txt";
let sub = Subscription::new("testurl", "testname", None);
let mut subs = Vec::new();
subs.push(sub);
let s = vec_serialize(&subs);
let path = Path::new(test_path);
let display = path.display();
let mut file = match File::create(&path) {
Err(why) => panic!("couldn't create {}: {}", display, why.description()),
Ok(file) => file,
};
match file.write_all(s.as_slice()) {
Err(why) => panic!("couldn't write to {}: {}", display, why.description()),
Ok(_) => (),
}
let sub_vec = file_deserialize(test_path).unwrap();
assert_eq!(subs, sub_vec);
fs::remove_file(test_path);
}