diff --git a/src/html_parser.rs b/src/html_parser.rs index c0b36af..7cfe42c 100644 --- a/src/html_parser.rs +++ b/src/html_parser.rs @@ -10,7 +10,7 @@ pub fn parse_inner_node(inner_node: &str) -> String { } if let Some(end) = end { - let new_end = end - 1; + let new_end = end.saturating_sub(1); if new_end >= start_index { end_index = new_end diff --git a/src/remaining_place.rs b/src/remaining_place.rs index 334ca4a..5a41ba1 100644 --- a/src/remaining_place.rs +++ b/src/remaining_place.rs @@ -8,6 +8,18 @@ pub struct RemainingPlace { pub free: usize, } +impl Ord for RemainingPlace { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.id.cmp(&other.id) + } +} + +impl PartialOrd for RemainingPlace { + fn partial_cmp(&self, other: &Self) -> Option { + self.id.partial_cmp(&other.id) + } +} + pub fn get_current_places(url: &str) -> Result, reqwest::Error> { let body = reqwest::blocking::get(url)?.text()?; @@ -35,19 +47,47 @@ pub fn get_current_places(url: &str) -> Result, reqwest::Err } } - lines.chunks(4).for_each(|chunk| { + lines.chunks(6).for_each(|chunk| { + let free = try_parse_free_slot(&chunk[4]); + let (id, description) = if let Some((id, description)) = chunk[0].split_once(' ') { + (id, description) + } else { + return; + }; + + if free.is_none() { + return; + } + let new_remaining_place = RemainingPlace { - id: chunk[0].clone(), - description: chunk[1].clone(), + id: id.to_string(), + description: description.to_string(), date: chunk[2].clone(), - free: match chunk[3].parse() { - Ok(value) => value, - Err(_) => 0, - }, + free: free.unwrap() }; places.push(new_remaining_place); }); + places.sort(); + Ok(places) } + +fn try_parse_free_slot(content: &str) -> Option { + if let Ok(slots) = content.parse::() { + return Some(slots) + } + + if let Some((left, right)) = content.split_once(' ') { + if let Ok(slots) = left.parse::() { + return Some(slots) + } + + if let Ok(slots) = right.parse::() { + return Some(slots) + } + } + + None +}