[#14] Added fancy loops to pickup_times & search
parent
dd1efaa34d
commit
e78de73d83
|
@ -41,13 +41,14 @@ pub fn get_pickup_times(
|
||||||
|
|
||||||
let mut output: Vec<PickupTime> = Vec::new();
|
let mut output: Vec<PickupTime> = Vec::new();
|
||||||
|
|
||||||
for map in data.iter() {
|
for map in data
|
||||||
output.push(PickupTime::new(
|
.iter()
|
||||||
// TODO it's really not logical here that this would return an
|
.filter(|m| m.contains_key("date") && m.contains_key("label"))
|
||||||
// "InvalidArgument" error, it should just skip the item
|
{
|
||||||
BasicDate::try_from(map.get("date").unwrap().as_str())?,
|
// Because we filtered the maps in the loop, we can safely us unwrap here
|
||||||
map.get("label").unwrap().to_string(),
|
if let Ok(date) = BasicDate::try_from(map.get("date").unwrap().as_str()) {
|
||||||
))
|
output.push(PickupTime::new(date, map.get("label").unwrap().to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(output)
|
Ok(output)
|
||||||
|
|
|
@ -13,23 +13,17 @@ const SEARCH_URL: &str = "https://www.ivago.be/nl/particulier/autocomplete/garba
|
||||||
///
|
///
|
||||||
/// * `street` - name of the street
|
/// * `street` - name of the street
|
||||||
/// * `city` - city the street is in
|
/// * `city` - city the street is in
|
||||||
// TODO find out how to do this async
|
|
||||||
pub fn search_streets(street_name: &str) -> Result<Vec<Street>, FejError> {
|
pub fn search_streets(street_name: &str) -> Result<Vec<Street>, FejError> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?;
|
let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?;
|
||||||
let data: Vec<HashMap<String, String>> = response.json()?;
|
let data: Vec<HashMap<String, String>> = response.json()?;
|
||||||
|
|
||||||
let mut output: Vec<Street> = Vec::new();
|
// This is pretty cool, filter_map first does get() on all the maps, and
|
||||||
|
// then filters out any None values
|
||||||
// We iterate over every item and extract the needed data
|
// Then, we do the same thing for streets
|
||||||
for map in data.iter() {
|
Ok(data
|
||||||
if let Some(value) = map.get("value") {
|
.iter()
|
||||||
match Street::try_from(value.as_str()) {
|
.filter_map(|m| m.get("value"))
|
||||||
Ok(street) => output.push(street),
|
.filter_map(|v| Street::try_from(v.as_str()).ok())
|
||||||
Err(_) => continue,
|
.collect())
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(output)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue