-
-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathpagination.rs
More file actions
42 lines (34 loc) · 948 Bytes
/
pagination.rs
File metadata and controls
42 lines (34 loc) · 948 Bytes
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
use std::str::FromStr;
use crate::utils::http;
#[derive(Debug, Clone)]
pub struct Link {
results: bool,
cursor: String,
}
#[derive(Debug, Default, Clone)]
pub struct Pagination {
next: Option<Link>,
}
impl Pagination {
pub fn into_next_cursor(self) -> Option<String> {
self.next
.and_then(|x| if x.results { Some(x.cursor) } else { None })
}
}
impl FromStr for Pagination {
type Err = ();
fn from_str(s: &str) -> Result<Pagination, ()> {
let mut rv = Pagination::default();
for item in http::parse_link_header(s) {
let target = match item.get("rel") {
Some(&"next") => &mut rv.next,
_ => continue,
};
*target = Some(Link {
results: item.get("results") == Some(&"true"),
cursor: (*item.get("cursor").unwrap_or(&"")).to_owned(),
});
}
Ok(rv)
}
}