lotr_api/request/
sort.rs

1use crate::attribute::Attribute;
2
3use super::GetUrl;
4
5/// This struct contains the data for the sorting of the API.
6///
7/// # Example
8/// ```
9/// use lotr_api::{
10///     attribute::{Attribute, BookAttribute},
11///     sort::{Sort, SortOrder},
12///     request::{GetUrl}};
13///
14/// let sort = Sort::new(SortOrder::Ascending, Attribute::Book(BookAttribute::Name));
15/// assert_eq!(sort.get_url(), "sort=name:asc");
16/// ```
17#[derive(Debug, Copy, Clone, PartialEq, Eq)]
18pub struct Sort {
19    pub(crate) sort_type: SortOrder,
20    pub(crate) sort_by: Attribute,
21}
22
23impl GetUrl for Sort {
24    fn get_url(&self) -> String {
25        let mut url = String::from("sort=");
26        url.push_str(format!("{}:{}", self.sort_by.get_url(), self.sort_type.get_url()).as_str());
27        url
28    }
29}
30
31impl Sort {
32    pub fn new(sort_type: SortOrder, sort_by: Attribute) -> Self {
33        Self { sort_type, sort_by }
34    }
35
36    pub(crate) fn get_item_type(&self) -> crate::ItemType {
37        self.sort_by.get_item_type()
38    }
39}
40
41/// Define the sort order.
42#[derive(Debug, Copy, Clone, PartialEq, Eq)]
43pub enum SortOrder {
44    Ascending,
45    Descending,
46}
47
48impl GetUrl for SortOrder {
49    fn get_url(&self) -> String {
50        match self {
51            SortOrder::Ascending => "asc",
52            SortOrder::Descending => "desc",
53        }
54        .to_string()
55    }
56}