From 5dd2b3a8786aa4d61a012345e506fa7a2abfaa8b Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 5 Apr 2021 12:20:55 +0200 Subject: [PATCH] Stopped exposing PickupTime fields --- src/ivago/controller/pickup_times.rs | 12 ++++----- src/ivago/controller/structs/basic_date.rs | 27 +++++++++++---------- src/ivago/controller/structs/pickup_time.rs | 23 ++++++++++++------ 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/ivago/controller/pickup_times.rs b/src/ivago/controller/pickup_times.rs index 3727117..0a9bd3d 100644 --- a/src/ivago/controller/pickup_times.rs +++ b/src/ivago/controller/pickup_times.rs @@ -31,8 +31,8 @@ pub fn get_pickup_times( .query(&[ ("_format", "json"), ("type", ""), - ("start", &i64::from(start_date).to_string()), - ("end", &i64::from(end_date).to_string()), + ("start", &start_date.epoch().to_string()), + ("end", &end_date.epoch().to_string()), ]) .send()?; let data: Vec> = response.json()?; @@ -40,11 +40,11 @@ pub fn get_pickup_times( let mut output: Vec = Vec::new(); for map in data.iter() { - output.push(PickupTime { + output.push(PickupTime::new( // TODO should I check here if the parsing worked? - date: BasicDate::try_from(map.get("date").unwrap().as_str()).unwrap(), - label: map.get("label").unwrap().to_string(), - }) + BasicDate::try_from(map.get("date").unwrap().as_str()).unwrap(), + map.get("label").unwrap().to_string(), + )) } Ok(output) diff --git a/src/ivago/controller/structs/basic_date.rs b/src/ivago/controller/structs/basic_date.rs index 8669954..421d7df 100644 --- a/src/ivago/controller/structs/basic_date.rs +++ b/src/ivago/controller/structs/basic_date.rs @@ -15,6 +15,20 @@ pub struct BasicDate { 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. impl<'v> FromFormValue<'v> for BasicDate { type Error = &'v RawStr; @@ -61,16 +75,3 @@ impl Serialize for BasicDate { serializer.serialize_str(&self.to_string()) } } - -impl From 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() - } -} diff --git a/src/ivago/controller/structs/pickup_time.rs b/src/ivago/controller/structs/pickup_time.rs index 78d7e8e..6f2249d 100644 --- a/src/ivago/controller/structs/pickup_time.rs +++ b/src/ivago/controller/structs/pickup_time.rs @@ -1,6 +1,22 @@ use super::BasicDate; 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 { fn serialize(&self, serializer: S) -> Result where @@ -12,10 +28,3 @@ impl Serialize for PickupTime { 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, -}