Stopped exposing PickupTime fields

master
Jef Roosens 2021-04-05 12:20:55 +02:00
parent e7bd93c3a4
commit 5dd2b3a878
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
3 changed files with 36 additions and 26 deletions

View File

@ -31,8 +31,8 @@ pub fn get_pickup_times(
.query(&[ .query(&[
("_format", "json"), ("_format", "json"),
("type", ""), ("type", ""),
("start", &i64::from(start_date).to_string()), ("start", &start_date.epoch().to_string()),
("end", &i64::from(end_date).to_string()), ("end", &end_date.epoch().to_string()),
]) ])
.send()?; .send()?;
let data: Vec<HashMap<String, String>> = response.json()?; let data: Vec<HashMap<String, String>> = response.json()?;
@ -40,11 +40,11 @@ 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.iter() {
output.push(PickupTime { output.push(PickupTime::new(
// TODO should I check here if the parsing worked? // TODO should I check here if the parsing worked?
date: BasicDate::try_from(map.get("date").unwrap().as_str()).unwrap(), BasicDate::try_from(map.get("date").unwrap().as_str()).unwrap(),
label: map.get("label").unwrap().to_string(), map.get("label").unwrap().to_string(),
}) ))
} }
Ok(output) Ok(output)

View File

@ -15,6 +15,20 @@ pub struct BasicDate {
day: u8, day: u8,
} }
impl BasicDate {
/// Return the seconds since epoch for this date
pub fn epoch(&self) -> i64 {
// Timezone of Brussels is UTC + 2 hours in the western hemisphere
FixedOffset::west(7_200)
.ymd(self.year as i32, self.month as u32, self.day as u32)
// For some reason, I couldn't get .timestamp() to work on a Date
// without a time component, even though the docs seemed to
// indicate this was possible
.and_hms(0, 0, 0)
.timestamp()
}
}
/// This allows us to use BasicDate as a query parameter in our routes. /// This allows us to use BasicDate as a query parameter in our routes.
impl<'v> FromFormValue<'v> for BasicDate { impl<'v> FromFormValue<'v> for BasicDate {
type Error = &'v RawStr; type Error = &'v RawStr;
@ -61,16 +75,3 @@ impl Serialize for BasicDate {
serializer.serialize_str(&self.to_string()) serializer.serialize_str(&self.to_string())
} }
} }
impl From<BasicDate> for i64 {
fn from(date: BasicDate) -> i64 {
// Timezone of Brussels is UTC + 2 hours in the western hemisphere
FixedOffset::west(7_200)
.ymd(date.year as i32, date.month as u32, date.day as u32)
// For some reason, I couldn't get .timestamp() to work on a Date
// without a time component, even though the docs seemed to
// indicate this was possible
.and_hms(0, 0, 0)
.timestamp()
}
}

View File

@ -1,6 +1,22 @@
use super::BasicDate; use super::BasicDate;
use serde::ser::{Serialize, SerializeStruct, Serializer}; use serde::ser::{Serialize, SerializeStruct, Serializer};
/// Represents a pickup time instance. All fields are a direct map of the
/// original API
pub struct PickupTime {
date: BasicDate,
label: String,
}
impl PickupTime {
pub fn new(date: BasicDate, label: String) -> PickupTime {
PickupTime {
date: date,
label: label,
}
}
}
impl Serialize for PickupTime { impl Serialize for PickupTime {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
@ -12,10 +28,3 @@ impl Serialize for PickupTime {
s.end() s.end()
} }
} }
/// Represents a pickup time instance. All fields are a direct map of the
/// original API
pub struct PickupTime {
pub date: BasicDate,
pub label: String,
}