fix: show an error for invalid pages

This commit is contained in:
Anna 2018-09-05 09:39:21 -04:00
parent 2b936652dd
commit 7fe2af29e0
4 changed files with 33 additions and 26 deletions

View File

@ -8,6 +8,8 @@ pub enum Error {
MissingElement(String),
#[fail(display = "the content scraped from the Lodestone was invalid: {}", _0)]
InvalidContent(String),
#[fail(display = "invalid page (1 through {} available)", _0)]
InvalidPage(u64),
#[fail(display = "invalid number: {}", _0)]
InvalidNumber(std::num::ParseIntError),

View File

@ -21,6 +21,8 @@ selectors!(
);
crate fn parse_pagination(html: &Html) -> Result<Pagination> {
const LODESTONE_PER_PAGE: f32 = 50.0;
let total_str = crate::logic::plain_parse(&html, &*PAGINATION_TOTAL)?;
let total_results: u64 = total_str
.split(' ')
@ -29,20 +31,27 @@ crate fn parse_pagination(html: &Html) -> Result<Pagination> {
.parse()
.map_err(Error::InvalidNumber)?;
let pages_str = crate::logic::plain_parse(&html, &*PAGINATION_PAGES)?;
let mut pages_split = pages_str.split(' ');
let (total_pages, current_page) = if parse_no_results(html) {
let total_pages = (total_results as f32 / LODESTONE_PER_PAGE).ceil() as u64;
let current_page = 0;
(total_pages, current_page)
} else {
let pages_str = crate::logic::plain_parse(&html, &*PAGINATION_PAGES)?;
let mut pages_split = pages_str.split(' ');
let current_page: u64 = pages_split
.nth(1)
.ok_or_else(|| Error::invalid_content("4 items in pages string", None))?
.parse()
.map_err(Error::InvalidNumber)?;
let current_page: u64 = pages_split
.nth(1)
.ok_or_else(|| Error::invalid_content("4 items in pages string", None))?
.parse()
.map_err(Error::InvalidNumber)?;
let total_pages: u64 = pages_split
.nth(1)
.ok_or_else(|| Error::invalid_content("4 items in pages string", None))?
.parse()
.map_err(Error::InvalidNumber)?;
let total_pages: u64 = pages_split
.nth(1)
.ok_or_else(|| Error::invalid_content("4 items in pages string", None))?
.parse()
.map_err(Error::InvalidNumber)?;
(total_pages, current_page)
};
Ok(Pagination {
current_page,

View File

@ -32,15 +32,13 @@ selectors!(
pub fn parse(html: &str) -> Result<Paginated<CharacterSearchItem>> {
let html = Html::parse_document(html);
if crate::logic::search::parse_no_results(&html) {
return Ok(Paginated {
pagination: Default::default(),
results: Default::default(),
});
}
let pagination = crate::logic::search::parse_pagination(&html)?;
// has results but requested an invalid page
if pagination.total_results != 0 && pagination.current_page == 0 {
return Err(Error::InvalidPage(pagination.total_pages));
}
let results: Vec<CharacterSearchItem> = html
.select(&*ITEM_ENTRY)
.map(|x| parse_single(x))

View File

@ -38,15 +38,13 @@ selectors!(
pub fn parse(s: &str) -> Result<Paginated<FreeCompanySearchItem>> {
let html = Html::parse_document(s);
if crate::logic::search::parse_no_results(&html) {
return Ok(Paginated {
pagination: Default::default(),
results: Default::default(),
});
}
let pagination = crate::logic::search::parse_pagination(&html)?;
// has results but requested an invalid page
if pagination.total_results != 0 && pagination.current_page == 0 {
return Err(Error::InvalidPage(pagination.total_pages));
}
let results: Vec<FreeCompanySearchItem> = html
.select(&*ITEM_ENTRY)
.map(|x| parse_single(x))