fix: show an error for invalid pages
This commit is contained in:
parent
2b936652dd
commit
7fe2af29e0
|
@ -8,6 +8,8 @@ pub enum Error {
|
||||||
MissingElement(String),
|
MissingElement(String),
|
||||||
#[fail(display = "the content scraped from the Lodestone was invalid: {}", _0)]
|
#[fail(display = "the content scraped from the Lodestone was invalid: {}", _0)]
|
||||||
InvalidContent(String),
|
InvalidContent(String),
|
||||||
|
#[fail(display = "invalid page (1 through {} available)", _0)]
|
||||||
|
InvalidPage(u64),
|
||||||
|
|
||||||
#[fail(display = "invalid number: {}", _0)]
|
#[fail(display = "invalid number: {}", _0)]
|
||||||
InvalidNumber(std::num::ParseIntError),
|
InvalidNumber(std::num::ParseIntError),
|
||||||
|
|
|
@ -21,6 +21,8 @@ selectors!(
|
||||||
);
|
);
|
||||||
|
|
||||||
crate fn parse_pagination(html: &Html) -> Result<Pagination> {
|
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_str = crate::logic::plain_parse(&html, &*PAGINATION_TOTAL)?;
|
||||||
let total_results: u64 = total_str
|
let total_results: u64 = total_str
|
||||||
.split(' ')
|
.split(' ')
|
||||||
|
@ -29,20 +31,27 @@ crate fn parse_pagination(html: &Html) -> Result<Pagination> {
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(Error::InvalidNumber)?;
|
.map_err(Error::InvalidNumber)?;
|
||||||
|
|
||||||
let pages_str = crate::logic::plain_parse(&html, &*PAGINATION_PAGES)?;
|
let (total_pages, current_page) = if parse_no_results(html) {
|
||||||
let mut pages_split = pages_str.split(' ');
|
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
|
let current_page: u64 = pages_split
|
||||||
.nth(1)
|
.nth(1)
|
||||||
.ok_or_else(|| Error::invalid_content("4 items in pages string", None))?
|
.ok_or_else(|| Error::invalid_content("4 items in pages string", None))?
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(Error::InvalidNumber)?;
|
.map_err(Error::InvalidNumber)?;
|
||||||
|
|
||||||
let total_pages: u64 = pages_split
|
let total_pages: u64 = pages_split
|
||||||
.nth(1)
|
.nth(1)
|
||||||
.ok_or_else(|| Error::invalid_content("4 items in pages string", None))?
|
.ok_or_else(|| Error::invalid_content("4 items in pages string", None))?
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(Error::InvalidNumber)?;
|
.map_err(Error::InvalidNumber)?;
|
||||||
|
(total_pages, current_page)
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Pagination {
|
Ok(Pagination {
|
||||||
current_page,
|
current_page,
|
||||||
|
|
|
@ -32,15 +32,13 @@ selectors!(
|
||||||
pub fn parse(html: &str) -> Result<Paginated<CharacterSearchItem>> {
|
pub fn parse(html: &str) -> Result<Paginated<CharacterSearchItem>> {
|
||||||
let html = Html::parse_document(html);
|
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)?;
|
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
|
let results: Vec<CharacterSearchItem> = html
|
||||||
.select(&*ITEM_ENTRY)
|
.select(&*ITEM_ENTRY)
|
||||||
.map(|x| parse_single(x))
|
.map(|x| parse_single(x))
|
||||||
|
|
|
@ -38,15 +38,13 @@ selectors!(
|
||||||
pub fn parse(s: &str) -> Result<Paginated<FreeCompanySearchItem>> {
|
pub fn parse(s: &str) -> Result<Paginated<FreeCompanySearchItem>> {
|
||||||
let html = Html::parse_document(s);
|
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)?;
|
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
|
let results: Vec<FreeCompanySearchItem> = html
|
||||||
.select(&*ITEM_ENTRY)
|
.select(&*ITEM_ENTRY)
|
||||||
.map(|x| parse_single(x))
|
.map(|x| parse_single(x))
|
||||||
|
|
Loading…
Reference in New Issue