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
use super::{deserialise, Mastodon, Result};
use entities::itemsiter::ItemsIter;
use hyper_old_types::header::{parsing, Link, RelationType};
use reqwest::{header::LINK, Response};
use serde::Deserialize;
use url::Url;
use http_send::HttpSend;
macro_rules! pages {
($($direction:ident: $fun:ident),*) => {
$(
doc_comment!(concat!(
"Method to retrieve the ", stringify!($direction), " page of results"),
pub fn $fun(&mut self) -> Result<Option<Vec<T>>> {
let url = match self.$direction.take() {
Some(s) => s,
None => return Ok(None),
};
let response = self.mastodon.send(
self.mastodon.client.get(url)
)?;
let (prev, next) = get_links(&response)?;
self.next = next;
self.prev = prev;
deserialise(response)
});
)*
}
}
#[derive(Debug, Clone)]
pub struct OwnedPage<T: for<'de> Deserialize<'de>, H: HttpSend> {
mastodon: Mastodon<H>,
next: Option<Url>,
prev: Option<Url>,
pub initial_items: Vec<T>,
}
impl<T: for<'de> Deserialize<'de>, H: HttpSend> OwnedPage<T, H> {
pages! {
next: next_page,
prev: prev_page
}
}
impl<'a, T: for<'de> Deserialize<'de>, H: HttpSend> From<Page<'a, T, H>> for OwnedPage<T, H> {
fn from(page: Page<'a, T, H>) -> OwnedPage<T, H> {
OwnedPage {
mastodon: page.mastodon.clone(),
next: page.next,
prev: page.prev,
initial_items: page.initial_items,
}
}
}
#[derive(Debug, Clone)]
pub struct Page<'a, T: for<'de> Deserialize<'de>, H: 'a + HttpSend> {
mastodon: &'a Mastodon<H>,
next: Option<Url>,
prev: Option<Url>,
pub initial_items: Vec<T>,
}
impl<'a, T: for<'de> Deserialize<'de>, H: HttpSend> Page<'a, T, H> {
pages! {
next: next_page,
prev: prev_page
}
pub(crate) fn new(mastodon: &'a Mastodon<H>, response: Response) -> Result<Self> {
let (prev, next) = get_links(&response)?;
Ok(Page {
initial_items: deserialise(response)?,
next,
prev,
mastodon,
})
}
}
impl<'a, T: Clone + for<'de> Deserialize<'de>, H: HttpSend> Page<'a, T, H> {
pub fn to_owned(self) -> OwnedPage<T, H> {
OwnedPage::from(self)
}
pub fn items_iter(self) -> impl Iterator<Item = T> + 'a
where
T: 'a,
{
ItemsIter::new(self)
}
}
fn get_links(response: &Response) -> Result<(Option<Url>, Option<Url>)> {
let mut prev = None;
let mut next = None;
if let Some(link_header) = response.headers().get(LINK) {
let link_header = link_header.to_str()?;
let link_header = link_header.as_bytes();
let link_header: Link = parsing::from_raw_str(&link_header)?;
for value in link_header.values() {
if let Some(relations) = value.rel() {
if relations.contains(&RelationType::Next) {
next = Some(Url::parse(value.link())?);
}
if relations.contains(&RelationType::Prev) {
prev = Some(Url::parse(value.link())?);
}
}
}
}
Ok((prev, next))
}