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
use eventsource_threaded::event::Event;
use json;
pub type CreationID = u64;
#[derive(Debug, Clone)]
pub struct Subscription {
pub channel_uid: String,
pub creation_id: CreationID,
pub app: String,
pub path: String,
pub message_list: Vec<String>,
}
impl Subscription {
fn event_matches(&self, event: &Event) -> bool {
if let Some(json) = &json::parse(&event.data).ok() {
return self.creation_id.to_string() == json["id"].dump();
}
false
}
pub fn add_to_message_list(&mut self, event: &Event) -> Option<u64> {
if self.event_matches(&event) {
let json = &json::parse(&event.data).ok()?["json"];
if !json.is_null() {
self.message_list.push(json.dump());
return Some(self.message_list.len() as u64);
}
}
None
}
pub fn pop_message(&mut self) -> Option<String> {
if self.message_list.len() == 0 {
return None;
}
let messages = self.message_list.clone();
let (head, tail) = messages.split_at(1);
self.message_list = tail.to_vec();
Some(head.to_owned()[0].clone())
}
}