use crate::models::chrono::NaiveDate;
use crate::models::crowd::crowd_density::CrowdDensityForecast;
use crate::models::crowd::passenger_vol;
use crate::models::crowd::prelude::*;
use crate::r#async::client::LTAClient;
use crate::r#async::{build_req_with_query, build_req_with_skip};
use crate::{vol_type_to_url, Client, Crowd, LTAResult};
use async_trait::async_trait;
use lta_models::crowd::crowd_density::CrowdDensityForecastRawResp;
use lta_models::prelude::MrtLine;
#[async_trait]
pub trait CrowdRequests<C: Client> {
async fn get_passenger_vol_by<S, D>(
client: &C,
vol_type: passenger_vol::VolType,
date: D,
skip: S,
) -> LTAResult<Vec<String>>
where
S: Into<Option<u32>> + Send,
D: Into<Option<NaiveDate>> + Send;
async fn get_crowd_density_rt(
client: &C,
train_line: MrtLine,
) -> LTAResult<Vec<StationCrowdLevel>>;
async fn get_crowd_density_forecast(
client: &C,
train_line: MrtLine,
) -> LTAResult<CrowdDensityForecast>;
}
#[async_trait]
impl CrowdRequests<LTAClient> for Crowd {
async fn get_passenger_vol_by<S, D>(
client: <AClient,
vol_type: VolType,
date: D,
skip: S,
) -> LTAResult<Vec<String>>
where
S: Into<Option<u32>> + Send,
D: Into<Option<NaiveDate>> + Send,
{
let fmt_date = date
.into()
.map(|f| f.format(passenger_vol::FORMAT).to_string());
let url = vol_type_to_url(vol_type)?;
match fmt_date {
Some(nd) => {
build_req_with_query::<passenger_vol::PassengerVolRawResp, _, _, _>(
client,
url,
|rb| rb.query(&[("Date", nd)]),
)
.await
}
None => {
build_req_with_skip::<passenger_vol::PassengerVolRawResp, _, _>(
client,
url,
skip.into(),
)
.await
}
}
}
async fn get_crowd_density_rt(
client: <AClient,
train_line: MrtLine,
) -> LTAResult<Vec<StationCrowdLevel>> {
build_req_with_query::<StationCrowdLevelRawResp, _, _, _>(
client,
api_url!("/PCDRealTime"),
|rb| rb.query(&[("TrainLine", train_line)]),
)
.await
}
async fn get_crowd_density_forecast(
client: <AClient,
train_line: MrtLine,
) -> LTAResult<CrowdDensityForecast> {
build_req_with_query::<CrowdDensityForecastRawResp, _, _, _>(
client,
api_url!("/PCDForecast"),
|rb| rb.query(&[("TrainLine", train_line)]),
)
.await
}
}