use std::fmt::Display;
use jiff::Timestamp;
use serde::de::DeserializeOwned;
use crate::enums::{Direction, LocationType, Mode, RealtimeMode};
use crate::types::{Itinerary, Match, PlanResponse, StoptimesResponse};
pub trait Request {
const PATH: &'static str;
type Response: DeserializeOwned;
fn query(&self) -> Vec<(&'static str, String)>;
}
#[derive(Default)]
struct Query(Vec<(&'static str, String)>);
impl Query {
fn set(mut self, name: &'static str, value: impl Display) -> Self {
self.0.push((name, value.to_string()));
self
}
fn opt(self, name: &'static str, value: Option<impl Display>) -> Self {
match value {
Some(value) => self.set(name, value),
None => self,
}
}
fn list(self, name: &'static str, values: &[impl Display]) -> Self {
if values.is_empty() {
return self;
}
let joined = values
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(",");
self.set(name, joined)
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Geocode {
pub text: String,
pub language: Vec<String>,
pub types: Vec<LocationType>,
pub modes: Vec<Mode>,
pub place: Option<String>,
pub place_bias: Option<f64>,
pub num_results: Option<u32>,
}
impl Geocode {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
..Self::default()
}
}
}
impl Request for Geocode {
const PATH: &'static str = "api/v1/geocode";
type Response = Vec<Match>;
fn query(&self) -> Vec<(&'static str, String)> {
Query::default()
.set("text", &self.text)
.list("language", &self.language)
.list("type", &self.types)
.list("mode", &self.modes)
.opt("place", self.place.as_ref())
.opt("placeBias", self.place_bias)
.opt("numResults", self.num_results)
.0
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ReverseGeocode {
pub place: String,
pub types: Vec<LocationType>,
pub num_results: Option<u32>,
}
impl ReverseGeocode {
pub fn new(lat: f64, lon: f64) -> Self {
Self {
place: format!("{lat},{lon}"),
..Self::default()
}
}
}
impl Request for ReverseGeocode {
const PATH: &'static str = "api/v1/reverse-geocode";
type Response = Vec<Match>;
fn query(&self) -> Vec<(&'static str, String)> {
Query::default()
.set("place", &self.place)
.list("type", &self.types)
.opt("numResults", self.num_results)
.0
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Plan {
pub from_place: String,
pub to_place: String,
pub time: Option<Timestamp>,
pub arrive_by: Option<bool>,
pub num_itineraries: Option<u32>,
pub max_itineraries: Option<u32>,
pub page_cursor: Option<String>,
pub search_window: Option<u32>,
pub timetable_view: Option<bool>,
pub transit_modes: Vec<Mode>,
pub max_transfers: Option<u32>,
pub detailed_transfers: Option<bool>,
pub detailed_legs: Option<bool>,
pub join_interlined_legs: Option<bool>,
pub realtime_mode: Option<RealtimeMode>,
pub language: Vec<String>,
}
impl Plan {
pub fn new(from_place: impl Into<String>, to_place: impl Into<String>) -> Self {
Self {
from_place: from_place.into(),
to_place: to_place.into(),
..Self::default()
}
}
}
impl Request for Plan {
const PATH: &'static str = "api/v6/plan";
type Response = PlanResponse;
fn query(&self) -> Vec<(&'static str, String)> {
Query::default()
.set("fromPlace", &self.from_place)
.set("toPlace", &self.to_place)
.opt("time", self.time)
.opt("arriveBy", self.arrive_by)
.opt("numItineraries", self.num_itineraries)
.opt("maxItineraries", self.max_itineraries)
.opt("pageCursor", self.page_cursor.as_ref())
.opt("searchWindow", self.search_window)
.opt("timetableView", self.timetable_view)
.list("transitModes", &self.transit_modes)
.opt("maxTransfers", self.max_transfers)
.opt("detailedTransfers", self.detailed_transfers)
.opt("detailedLegs", self.detailed_legs)
.opt("joinInterlinedLegs", self.join_interlined_legs)
.opt("realtimeMode", self.realtime_mode.as_ref())
.list("language", &self.language)
.0
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Stoptimes {
pub stop_id: String,
pub time: Option<Timestamp>,
pub arrive_by: Option<bool>,
pub n: Option<u32>,
pub mode: Vec<Mode>,
pub window: Option<u32>,
pub page_cursor: Option<String>,
pub with_alerts: Option<bool>,
pub realtime_mode: Option<RealtimeMode>,
pub direction: Option<Direction>,
pub language: Vec<String>,
}
impl Stoptimes {
pub fn new(stop_id: impl Into<String>) -> Self {
Self {
stop_id: stop_id.into(),
..Self::default()
}
}
}
impl Request for Stoptimes {
const PATH: &'static str = "api/v6/stoptimes";
type Response = StoptimesResponse;
fn query(&self) -> Vec<(&'static str, String)> {
Query::default()
.set("stopId", &self.stop_id)
.opt("time", self.time)
.opt("arriveBy", self.arrive_by)
.opt("n", self.n)
.list("mode", &self.mode)
.opt("window", self.window)
.opt("pageCursor", self.page_cursor.as_ref())
.opt("withAlerts", self.with_alerts)
.opt("realtimeMode", self.realtime_mode.as_ref())
.opt("direction", self.direction.as_ref())
.list("language", &self.language)
.0
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Trip {
pub trip_id: String,
pub detailed_legs: Option<bool>,
pub join_interlined_legs: Option<bool>,
pub language: Vec<String>,
}
impl Trip {
pub fn new(trip_id: impl Into<String>) -> Self {
Self {
trip_id: trip_id.into(),
..Self::default()
}
}
}
impl Request for Trip {
const PATH: &'static str = "api/v6/trip";
type Response = Itinerary;
fn query(&self) -> Vec<(&'static str, String)> {
Query::default()
.set("tripId", &self.trip_id)
.opt("detailedLegs", self.detailed_legs)
.opt("joinInterlinedLegs", self.join_interlined_legs)
.list("language", &self.language)
.0
}
}