asoiaf_api/item/
iterator.rs

1use crate::{
2    client::Client,
3    error::Error,
4    item::{Book, House},
5    requester::{
6        request::{BookRequest, CharacterRequest, HouseRequest},
7        ToRequest,
8    },
9};
10
11use super::Character;
12
13// This module should be refactored, but it will be done in the future.
14
15/**
16 * Iterates over the pages of a book request.
17 */
18pub struct BookIterator {
19    client: Client,
20    has_found_limit: bool,
21    request: BookRequest,
22}
23
24impl BookIterator {
25    pub(crate) fn new(request: BookRequest) -> Self {
26        Self {
27            has_found_limit: false,
28            request,
29            client: Client::new(),
30        }
31    }
32
33    /**
34     * Returns the next page of books.
35     *
36     * # Errors
37     *
38     * If there are no more pages to be found, it will return an Error::NoMorePages.
39     * If there is an error with the request, it will return an Error::RequestError.
40     */
41    pub async fn next(&mut self) -> Result<Vec<Book>, Error> {
42        if self.has_found_limit {
43            return Err(Error::NoMorePages);
44        }
45
46        let answer = self.client.get_request::<Book>(&self.request).await?;
47
48        self.request.next_page();
49
50        if answer.is_empty() {
51            self.has_found_limit = true;
52            Err(Error::NoMorePages)
53        } else {
54            Ok(answer)
55        }
56    }
57}
58
59/**
60 * Iterates over the pages of a house request.
61 */
62pub struct HouseIterator {
63    client: Client,
64    has_found_limit: bool,
65    request: HouseRequest,
66}
67
68impl HouseIterator {
69    pub(crate) fn new(request: HouseRequest) -> Self {
70        Self {
71            has_found_limit: false,
72            request,
73            client: Client::new(),
74        }
75    }
76
77    /**
78     * Returns the next page of houses.
79     *
80     * # Errors
81     *
82     * If there are no more pages to be found, it will return an Error::NoMorePages.
83     * If there is an error with the request, it will return an Error::RequestError.
84     */
85    pub async fn next(&mut self) -> Result<Vec<House>, Error> {
86        if self.has_found_limit {
87            return Err(Error::NoMorePages);
88        }
89
90        let answer = self.client.get_request::<House>(&self.request).await?;
91
92        self.request.next_page();
93
94        if answer.is_empty() {
95            self.has_found_limit = true;
96            Err(Error::NoMorePages)
97        } else {
98            Ok(answer)
99        }
100    }
101}
102
103/**
104 * Iterates over the pages of a character request.
105 */
106pub struct CharacterIterator {
107    client: Client,
108    has_found_limit: bool,
109    request: CharacterRequest,
110}
111
112impl CharacterIterator {
113    pub(crate) fn new(request: CharacterRequest) -> Self {
114        Self {
115            has_found_limit: false,
116            request,
117            client: Client::new(),
118        }
119    }
120
121    /**
122     * Returns the next page of characters.
123     *
124     * # Errors
125     *
126     * If there are no more pages to be found, it will return an Error::NoMorePages.
127     * If there is an error with the request, it will return an Error::RequestError.
128     */
129    pub async fn next(&mut self) -> Result<Vec<Character>, Error> {
130        if self.has_found_limit {
131            return Err(Error::NoMorePages);
132        }
133
134        let answer = self.client.get_request::<Character>(&self.request).await?;
135
136        self.request.next_page();
137
138        if answer.is_empty() {
139            self.has_found_limit = true;
140            Err(Error::NoMorePages)
141        } else {
142            Ok(answer)
143        }
144    }
145}