Skip to content

Commit

Permalink
Added Regional Rail Schedule Endpoint API
Browse files Browse the repository at this point in the history
Added the API to get schedule of each train for the regional rail.
  • Loading branch information
StefanBossbaly committed Apr 18, 2023
1 parent 13b41eb commit 40874f5
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

| Endpoint | Implemented | Tested |
| ------------------------------ | ----------- | ------ |
| `/RRSchedules/index.php` | | |
| `/RRSchedules/index.php` | | |
| `/BusSchedules/index.php` |||
| `/Stops/index.php` |||
| `/locations/get_locations.php` |||
Expand Down
7 changes: 7 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ impl Client {
) -> Result<responses::NextToArriveResponse> {
self.get_request("/NextToArrive/index.php", request).await
}

pub async fn rail_schedule(
&self,
request: requests::RailScheduleRequest,
) -> Result<responses::RailScheduleResponse> {
self.get_request("/RRSchedules/index.php", request).await
}
}
58 changes: 58 additions & 0 deletions src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,64 @@ impl<'a> de::Visitor<'a> for NaiveTimeVisitor {
}
}

pub fn deserialize_naive_time_with_space<'a, D: de::Deserializer<'a>>(
deserializer: D,
) -> Result<NaiveTime, D::Error> {
deserializer.deserialize_str(NaiveTimeWithSpaceVisitor)
}

const TIME_FORMAT_WITH_SPACE: &str = "%I:%M %p";

struct NaiveTimeWithSpaceVisitor;

impl<'a> de::Visitor<'a> for NaiveTimeWithSpaceVisitor {
type Value = NaiveTime;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(
formatter,
"an encoded time string with a space between the minutes and the AM/PM"
)
}

fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> {
match NaiveTime::parse_from_str(value, TIME_FORMAT_WITH_SPACE) {
Ok(date) => Ok(date),
Err(e) => Err(E::custom(format!("Error {} parsing time {}", e, value))),
}
}
}

pub fn deserialize_option_naive_time_with_space<'a, D: de::Deserializer<'a>>(
deserializer: D,
) -> Result<Option<NaiveTime>, D::Error> {
deserializer.deserialize_str(OptionNaiveTimeWithSpaceVisitor)
}

struct OptionNaiveTimeWithSpaceVisitor;

impl<'a> de::Visitor<'a> for OptionNaiveTimeWithSpaceVisitor {
type Value = Option<NaiveTime>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(
formatter,
"an encoded time string with a space between the minutes and the AM/PM or 'na'"
)
}

fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> {
if value == "na" {
Ok(None)
} else {
match NaiveTime::parse_from_str(value, TIME_FORMAT_WITH_SPACE) {
Ok(date) => Ok(Some(date)),
Err(e) => Err(E::custom(format!("Error {} parsing time {}", e, value))),
}
}
}
}

pub fn deserialize_bool<'a, D: de::Deserializer<'a>>(deserializer: D) -> Result<bool, D::Error> {
deserializer.deserialize_str(BoolStringVisitor)
}
Expand Down
10 changes: 10 additions & 0 deletions src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ impl Request for NextToArriveRequest {
params
}
}

pub struct RailScheduleRequest {
pub train_number: String,
}

impl Request for RailScheduleRequest {
fn into_params(self) -> Vec<(&'static str, String)> {
vec![("req1", self.train_number)]
}
}
30 changes: 29 additions & 1 deletion src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::convert::TryFrom;
use crate::{
deserialize::{
deserialize_api_error, deserialize_bool, deserialize_csv_encoded_string, deserialize_f64,
deserialize_naive_date_time, deserialize_naive_time, deserialize_optional_string_enum,
deserialize_naive_date_time, deserialize_naive_time, deserialize_naive_time_with_space,
deserialize_option_naive_time_with_space, deserialize_optional_string_enum,
deserialize_string_enum,
},
types::{RegionalRailStop, RegionalRailsLine, ServiceType},
Expand Down Expand Up @@ -177,3 +178,30 @@ pub struct NextToArrive {
#[serde(rename = "isdirect", deserialize_with = "deserialize_bool")]
pub is_direct: bool,
}

pub type RailScheduleApiResponse = ApiResponse<RailScheduleResponse>;
pub type RailScheduleResponse = Vec<RailSchedule>;

#[derive(Debug, Deserialize)]
pub struct RailSchedule {
#[serde(deserialize_with = "deserialize_string_enum")]
pub station: RegionalRailStop,

#[serde(
rename = "sched_tm",
deserialize_with = "deserialize_naive_time_with_space"
)]
pub scheduled_time: NaiveTime,

#[serde(
rename = "est_tm",
deserialize_with = "deserialize_naive_time_with_space"
)]
pub estimated_time: NaiveTime,

#[serde(
rename = "act_tm",
deserialize_with = "deserialize_option_naive_time_with_space"
)]
pub actual_time: Option<NaiveTime>,
}
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub enum RegionalRailStop {

// Media/Wawa Line Stops
Wawa,
#[strum(serialize = "Elwyn", serialize = "Elwyn Station", to_string = "Elwyn")]
Elwyn,
Media,
#[strum(serialize = "Moylan-Rose Valley")]
Expand Down
Loading

0 comments on commit 40874f5

Please sign in to comment.