asoiaf_api/requester/
request.rs

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
use super::{
    filter::{BookFilter, CharacterFilter, HouseFilter},
    pagination::Pagination,
    ToRequest, ToUrl,
};

/// Struct that is used to make book requests to the API.
#[derive(Default)]
pub(crate) struct BookRequest {
    filter: Option<BookFilter>,
    pagination: Option<Pagination>,
}

impl ToRequest for BookRequest {
    fn to_request(&self) -> String {
        let mut url = String::from("books");
        let mut options = Vec::new();
        if let Some(pagination) = &self.pagination {
            options.push(pagination.to_url());
        }
        if let Some(filter) = &self.filter {
            options.push(filter.to_url());
        }

        if !options.is_empty() {
            url.push('?');
            url.push_str(&options.join("&"));
        }
        url
    }

    fn update_pagination(&mut self, pagination: Pagination) {
        self.pagination = Some(pagination);
    }

    fn next_page(&mut self) {
        if let Some(pagination) = &mut self.pagination {
            pagination.next_page();
        }
    }
}

impl BookRequest {
    pub fn pagination(mut self, pagination: Pagination) -> Self {
        self.pagination = Some(pagination);
        self
    }

    pub fn filter(mut self, filter: BookFilter) -> Self {
        self.filter = Some(filter);
        self
    }
}

#[derive(Default)]
pub(crate) struct CharacterRequest {
    filter: Option<CharacterFilter>,
    pagination: Option<Pagination>,
}

impl ToRequest for CharacterRequest {
    fn to_request(&self) -> String {
        let mut url = String::from("characters");

        let mut options = Vec::new();
        if let Some(pagination) = &self.pagination {
            options.push(pagination.to_url());
        }
        if let Some(filter) = &self.filter {
            options.push(filter.to_url());
        }

        if !options.is_empty() {
            url.push('?');
            url.push_str(&options.join("&"));
        }
        url
    }

    fn update_pagination(&mut self, pagination: Pagination) {
        self.pagination = Some(pagination);
    }

    fn next_page(&mut self) {
        if let Some(pagination) = &mut self.pagination {
            pagination.next_page();
        }
    }
}

impl CharacterRequest {
    pub fn pagination(mut self, pagination: Pagination) -> Self {
        self.pagination = Some(pagination);
        self
    }

    pub fn filter(mut self, filter: CharacterFilter) -> Self {
        self.filter = Some(filter);
        self
    }
}

#[derive(Default)]
pub(crate) struct HouseRequest {
    filter: Option<HouseFilter>,
    pagination: Option<Pagination>,
}

impl ToRequest for HouseRequest {
    fn to_request(&self) -> String {
        let mut url = String::from("houses");
        let mut options = Vec::new();
        if let Some(pagination) = &self.pagination {
            options.push(pagination.to_url());
        }
        if let Some(filter) = &self.filter {
            options.push(filter.to_url());
        }

        if !options.is_empty() {
            url.push('?');
            url.push_str(&options.join("&"));
        }
        url
    }

    fn update_pagination(&mut self, pagination: Pagination) {
        self.pagination = Some(pagination);
    }

    fn next_page(&mut self) {
        if let Some(pagination) = &mut self.pagination {
            pagination.next_page();
        }
    }
}

impl HouseRequest {
    pub fn pagination(mut self, pagination: Pagination) -> Self {
        self.pagination = Some(pagination);
        self
    }

    pub fn filter(mut self, filter: HouseFilter) -> Self {
        self.filter = Some(filter);
        self
    }
}