lotr_api/request/
pagination.rs

1/// This struct contains the date for the pagination of the API.
2///
3/// # Example
4///
5/// ```
6/// use lotr_api::{request::{GetUrl, pagination::Pagination}};
7///
8/// let pagination = Pagination::new(10, 2, 1);
9///
10/// assert_eq!(pagination.get_url(), "limit=10&offset=2&page=1");
11///
12#[derive(Debug, Copy, Clone, PartialEq, Eq)]
13pub struct Pagination {
14    limit: u32,
15    offset: u32,
16    page: u32,
17}
18
19impl Pagination {
20    pub fn new(limit: u32, offset: u32, page: u32) -> Self {
21        Self {
22            limit,
23            offset,
24            page,
25        }
26    }
27
28    pub fn get_url(&self) -> String {
29        let mut values = vec![];
30
31        if self.limit != 0 {
32            values.push(format!("limit={}", self.limit));
33        }
34        if self.offset != 0 {
35            values.push(format!("offset={}", self.offset));
36        }
37        if self.page != 0 {
38            values.push(format!("page={}", self.page));
39        }
40
41        if values.is_empty() {
42            String::new()
43        } else {
44            values.join("&")
45        }
46    }
47}