asoiaf_api/item/iterator.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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
use crate::{
client::Client,
error::Error,
item::{Book, House},
requester::{
request::{BookRequest, CharacterRequest, HouseRequest},
ToRequest,
},
};
use super::Character;
// This module should be refactored, but it will be done in the future.
/**
* Iterates over the pages of a book request.
*/
pub struct BookIterator {
client: Client,
has_found_limit: bool,
request: BookRequest,
}
impl BookIterator {
pub(crate) fn new(request: BookRequest) -> Self {
Self {
has_found_limit: false,
request,
client: Client::new(),
}
}
/**
* Returns the next page of books.
*
* # Errors
*
* If there are no more pages to be found, it will return an Error::NoMorePages.
* If there is an error with the request, it will return an Error::RequestError.
*/
pub async fn next(&mut self) -> Result<Vec<Book>, Error> {
if self.has_found_limit {
return Err(Error::NoMorePages);
}
let answer = self.client.get_request::<Book>(&self.request).await?;
self.request.next_page();
if answer.is_empty() {
self.has_found_limit = true;
Err(Error::NoMorePages)
} else {
Ok(answer)
}
}
}
/**
* Iterates over the pages of a house request.
*/
pub struct HouseIterator {
client: Client,
has_found_limit: bool,
request: HouseRequest,
}
impl HouseIterator {
pub(crate) fn new(request: HouseRequest) -> Self {
Self {
has_found_limit: false,
request,
client: Client::new(),
}
}
/**
* Returns the next page of houses.
*
* # Errors
*
* If there are no more pages to be found, it will return an Error::NoMorePages.
* If there is an error with the request, it will return an Error::RequestError.
*/
pub async fn next(&mut self) -> Result<Vec<House>, Error> {
if self.has_found_limit {
return Err(Error::NoMorePages);
}
let answer = self.client.get_request::<House>(&self.request).await?;
self.request.next_page();
if answer.is_empty() {
self.has_found_limit = true;
Err(Error::NoMorePages)
} else {
Ok(answer)
}
}
}
/**
* Iterates over the pages of a character request.
*/
pub struct CharacterIterator {
client: Client,
has_found_limit: bool,
request: CharacterRequest,
}
impl CharacterIterator {
pub(crate) fn new(request: CharacterRequest) -> Self {
Self {
has_found_limit: false,
request,
client: Client::new(),
}
}
/**
* Returns the next page of characters.
*
* # Errors
*
* If there are no more pages to be found, it will return an Error::NoMorePages.
* If there is an error with the request, it will return an Error::RequestError.
*/
pub async fn next(&mut self) -> Result<Vec<Character>, Error> {
if self.has_found_limit {
return Err(Error::NoMorePages);
}
let answer = self.client.get_request::<Character>(&self.request).await?;
self.request.next_page();
if answer.is_empty() {
self.has_found_limit = true;
Err(Error::NoMorePages)
} else {
Ok(answer)
}
}
}