lotr_api/request/
pagination.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
/// This struct contains the date for the pagination of the API.
///
/// # Example
///
/// ```
/// use lotr_api::{request::{GetUrl, pagination::Pagination}};
///
/// let pagination = Pagination::new(10, 2, 1);
///
/// assert_eq!(pagination.get_url(), "limit=10&offset=2&page=1");
///
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Pagination {
    limit: u32,
    offset: u32,
    page: u32,
}

impl Pagination {
    pub fn new(limit: u32, offset: u32, page: u32) -> Self {
        Self {
            limit,
            offset,
            page,
        }
    }

    pub fn get_url(&self) -> String {
        let mut values = vec![];

        if self.limit != 0 {
            values.push(format!("limit={}", self.limit));
        }
        if self.offset != 0 {
            values.push(format!("offset={}", self.offset));
        }
        if self.page != 0 {
            values.push(format!("page={}", self.page));
        }

        if values.is_empty() {
            String::new()
        } else {
            values.join("&")
        }
    }
}