swapi_client/
common.rs

1//! Shared libs
2
3use std::error::Error;
4use std::fmt;
5
6/// List API data type
7#[derive(Debug, serde::Deserialize, Default, Clone)]
8pub struct ListData<T> {
9    pub current_page: Option<u8>,
10    pub count: usize,
11    pub next: Option<String>,
12    pub previous: Option<String>,
13    pub results: Vec<T>,
14}
15
16impl<T> ListData<T> {
17    pub fn has_next(&self) -> bool {
18        match self.next {
19            Some(_) => true,
20            _ => false,
21        }
22    }
23
24    pub fn has_prev(&self) -> bool {
25        match self.previous {
26            Some(_) => true,
27            _ => false,
28        }
29    }
30}
31
32impl<T> IntoIterator for ListData<T> {
33    type Item = T;
34    type IntoIter = std::vec::IntoIter<Self::Item>;
35
36    fn into_iter(self) -> Self::IntoIter {
37        self.results.into_iter()
38    }
39}
40
41/// Custom error type for return failed response
42#[derive(Debug)]
43pub struct RequestFailed(pub reqwest::blocking::Response);
44
45impl fmt::Display for RequestFailed {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        write!(f, "Request failed")
48    }
49}
50
51impl Error for RequestFailed {}