fix: handle no results

This commit is contained in:
Anna 2018-09-05 09:29:20 -04:00
parent d3c68a49d9
commit 2b936652dd
4 changed files with 24 additions and 1 deletions

View File

@ -16,6 +16,8 @@ pub use self::{
selectors!(
PAGINATION_TOTAL => ".parts__total";
PAGINATION_PAGES => ".btn__pager__current";
NO_RESULTS => "p.parts__zero";
);
crate fn parse_pagination(html: &Html) -> Result<Pagination> {
@ -48,3 +50,10 @@ crate fn parse_pagination(html: &Html) -> Result<Pagination> {
total_results,
})
}
crate fn parse_no_results<'a>(html: &Html) -> bool {
html.select(&*NO_RESULTS)
.next()
.map(|x| x.text().collect::<String>() == "Your search yielded no results.")
.unwrap_or(false)
}

View File

@ -32,6 +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)?;
let results: Vec<CharacterSearchItem> = html

View File

@ -38,6 +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)?;
let results: Vec<FreeCompanySearchItem> = html

View File

@ -1,7 +1,7 @@
pub mod character;
pub mod free_company;
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Pagination {
pub current_page: u64,
pub total_pages: u64,