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
use super::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Query {
pub live_stream: bool,
pub offset: u64,
pub limit: Option<u64>,
pub tag: Option<String>,
position: u64,
count: u64
}
impl Query {
pub fn new(live_stream: bool, offset: u64, limit: Option<u64>, tag: Option<String>) -> Query {
Query {
offset: offset,
limit: limit,
tag: tag,
live_stream: live_stream,
position: offset,
count: 0
}
}
pub fn current() -> Query {
Query::new(false, 0, None, None)
}
pub fn live() -> Query {
Query::new(true, 0, None, None)
}
pub fn offset(mut self, offset: u64) -> Query {
self.offset = offset;
self.position = offset;
self
}
pub fn limit(mut self, limit: u64) -> Query {
self.limit = Some(limit);
self
}
pub fn by_tag(mut self, tag: &str) -> Query {
self.tag = Some(tag.to_owned());
self
}
pub fn matches(&self, event: &Event) -> bool {
match self.tag {
Some(ref tag) => self.position < event.id && event.tags.contains(tag),
None => self.position < event.id
}
}
pub fn is_active(&self) -> bool {
match self.limit {
Some(limit) => self.count < limit,
None => true
}
}
pub fn update(&mut self, event_id: u64) {
self.position = event_id;
self.count += 1;
}
pub fn interval(&self) -> Interval<u64> {
let start = self.position;
let end = if self.limit.is_none() || self.tag.is_some() {
u64::max_value()
} else {
start + self.limit.unwrap()
};
Interval::new(start, end)
}
}
#[cfg(test)]
mod tests {
use super::super::*;
#[test]
fn test_constructors_and_modifiers() {
let query = Query::new(true, 100, Some(20), Some("tag".to_owned()));
assert_eq!(query.live_stream, true);
assert_eq!(query.offset, 100);
assert_eq!(query.limit, Some(20));
assert_eq!(query.tag, Some("tag".to_owned()));
let query = Query::current();
assert_eq!(query.live_stream, false);
assert_eq!(query.offset, 0);
assert_eq!(query.limit, None);
assert_eq!(query.tag, None);
let query = Query::live();
assert_eq!(query.live_stream, true);
assert_eq!(query.offset, 0);
assert_eq!(query.limit, None);
assert_eq!(query.tag, None);
let query = query.offset(100);
assert_eq!(query.offset, 100);
let query = query.limit(20);
assert_eq!(query.limit, Some(20));
let query = query.by_tag("tag");
assert_eq!(query.tag, Some("tag".to_owned()));
}
#[test]
fn test_event_matching() {
let mut query = Query::current();
assert!(query.matches(&Event::new("data", vec!["tag1"]).with_id(1)));
query.update(1);
assert!(!query.matches(&Event::new("data", vec!["tag1"]).with_id(1)));
let mut query = Query::current().by_tag("tag1");
assert!(query.matches(&Event::new("data", vec!["tag1"]).with_id(1)));
assert!(!query.matches(&Event::new("data", vec!["tag2"]).with_id(1)));
query.update(1);
assert!(!query.matches(&Event::new("data", vec!["tag1"]).with_id(1)));
}
#[test]
fn test_is_active() {
let query = Query::current();
assert!(query.is_active());
let mut query = query.limit(3);
assert!(query.is_active());
query.update(1);
query.update(2);
query.update(3);
assert_eq!(query.position, 3);
assert_eq!(query.count, 3);
assert!(!query.is_active());
}
}