use crate::blocking::{build_req_with_query, build_req_with_skip, LTAClient};
use crate::models::bus::prelude::*;
use crate::LTAResult;
use crate::{api_url, Bus, Client};
pub trait BusRequests<C: Client> {
fn get_arrival<'a, S>(client: &C, bus_stop_code: u32, service_no: S) -> LTAResult<BusArrivalResp>
where
S: Into<Option<&'a str>>;
fn get_bus_services<S>(client: &C, skip: S) -> LTAResult<Vec<BusService>>
where
S: Into<Option<u32>>;
fn get_bus_routes<S>(client: &C, skip: S) -> LTAResult<Vec<BusRoute>>
where
S: Into<Option<u32>>;
fn get_bus_stops<S>(client: &C, skip: S) -> LTAResult<Vec<BusStop>>
where
S: Into<Option<u32>>;
}
impl BusRequests<LTAClient> for Bus {
fn get_arrival<'a, S>(
client: <AClient,
bus_stop_code: u32,
service_no: S,
) -> LTAResult<BusArrivalResp>
where
S: Into<Option<&'a str>>,
{
let url = api_url!("/BusArrivalv2");
match service_no.into() {
Some(srv_no) => build_req_with_query::<RawBusArrivalResp, _, _, _>(client, url, |rb| {
let srv_no = srv_no.as_ref();
rb.query(&[
("BusStopCode", bus_stop_code.to_string().as_str()),
("ServiceNo", srv_no),
])
}),
None => build_req_with_query::<RawBusArrivalResp, _, _, _>(client, url, |rb| {
rb.query(&[("BusStopCode", bus_stop_code.to_string())])
}),
}
}
fn get_bus_services<S>(client: <AClient, skip: S) -> LTAResult<Vec<BusService>>
where
S: Into<Option<u32>>,
{
build_req_with_skip::<BusServiceResp, _, _>(client, api_url!("/BusServices"), skip.into())
}
fn get_bus_routes<S>(client: <AClient, skip: S) -> LTAResult<Vec<BusRoute>>
where
S: Into<Option<u32>>,
{
build_req_with_skip::<BusRouteResp, _, _>(client, api_url!("/BusRoutes"), skip.into())
}
fn get_bus_stops<S>(client: <AClient, skip: S) -> LTAResult<Vec<BusStop>>
where
S: Into<Option<u32>>,
{
build_req_with_skip::<BusStopsResp, _, _>(client, api_url!("/BusStops"), skip.into())
}
}