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
use failure::Error;
use reqwest::Url;
use super::{Bound, Order};
use crate::request::{build_url, response::ImagesResponse, QueryPairs, Request};
#[derive(Debug)]
pub struct Images<'a> {
query: QueryPairs<'a>,
}
impl<'a> Images<'a> {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let query = QueryPairs::new();
Images { query }
}
pub fn constraint(mut self, name: &'a str) -> Self {
self.query.insert("constraint", name);
self
}
pub fn constraint_bound(mut self, bound: Bound<'a>) -> Self {
let bound_query = bound.query_pair();
self.query.insert(bound_query.0, bound_query.1);
self
}
pub fn constraint_order(mut self, order: Order) -> Self {
self.query.insert("order", order);
self
}
pub fn page(mut self, page: u64) -> Self {
self.query.insert("page", page);
self
}
pub fn random(mut self) -> Self {
self.query.insert("random", true);
self
}
}
impl<'a> Request<'a> for Images<'a> {
type ResponseValue = ImagesResponse;
fn build(&self) -> Result<Url, Error> {
build_url("images.json", &self.query)
}
}
#[test]
fn request() {
let req = Images::new()
.constraint("id")
.constraint_bound(Bound::Gte("1941825"))
.constraint_order(Order::Descending)
.page(2)
.random()
.build()
.unwrap();
let expected = Url::parse_with_params(
"https://derpibooru.org/images.json",
&[
("constraint", "id"),
("gte", "1941825"),
("order", "d"),
("page", "2"),
("random", "true"),
],
)
.unwrap();
assert_eq!(req, expected);
}