use std::fmt::Display;
use serde::de::DeserializeOwned;
use crate::enums::RtMode;
use crate::types::{ArrivalList, DepartureList, Journey, LocationList, TripList};
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 flag(self, name: &'static str, value: Option<bool>) -> Self {
self.opt(name, value.map(u8::from))
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct LocationName {
pub input: String,
pub max_no: Option<u32>,
pub kind: Option<String>,
pub products: Option<u32>,
pub near: Option<(f64, f64)>,
}
impl LocationName {
pub fn new(input: impl Into<String>) -> Self {
Self {
input: input.into(),
..Self::default()
}
}
}
impl Request for LocationName {
const PATH: &'static str = "location.name";
type Response = LocationList;
fn query(&self) -> Vec<(&'static str, String)> {
Query::default()
.set("input", &self.input)
.opt("maxNo", self.max_no)
.opt("type", self.kind.as_ref())
.opt("products", self.products)
.opt("coordLat", self.near.map(|(lat, _)| lat))
.opt("coordLong", self.near.map(|(_, lon)| lon))
.0
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct NearbyStops {
pub lat: f64,
pub lon: f64,
pub r: Option<u32>,
pub max_no: Option<u32>,
pub kind: Option<String>,
pub products: Option<u32>,
}
impl NearbyStops {
pub fn new(lat: f64, lon: f64) -> Self {
Self {
lat,
lon,
..Self::default()
}
}
}
impl Request for NearbyStops {
const PATH: &'static str = "location.nearbystops";
type Response = LocationList;
fn query(&self) -> Vec<(&'static str, String)> {
Query::default()
.set("originCoordLat", self.lat)
.set("originCoordLong", self.lon)
.opt("r", self.r)
.opt("maxNo", self.max_no)
.opt("type", self.kind.as_ref())
.opt("products", self.products)
.0
}
}
macro_rules! board {
($(#[$meta:meta])* $name:ident, $path:literal, $response:ty) => {
$(#[$meta])*
#[derive(Debug, Clone, Default, PartialEq)]
pub struct $name {
/// The stop, as a full or a short ID.
pub id: String,
pub date: Option<String>,
pub time: Option<String>,
pub duration: Option<u32>,
pub max_journeys: Option<u32>,
pub products: Option<u32>,
pub direction: Option<String>,
pub rt_mode: Option<RtMode>,
}
impl $name {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
..Self::default()
}
}
}
impl Request for $name {
const PATH: &'static str = $path;
type Response = $response;
fn query(&self) -> Vec<(&'static str, String)> {
Query::default()
.set("id", &self.id)
.opt("date", self.date.as_ref())
.opt("time", self.time.as_ref())
.opt("duration", self.duration)
.opt("maxJourneys", self.max_journeys)
.opt("products", self.products)
.opt("direction", self.direction.as_ref())
.opt("rtMode", self.rt_mode.as_ref())
.0
}
}
};
}
board!(
DepartureBoard,
"departureBoard",
DepartureList
);
board!(
ArrivalBoard,
"arrivalBoard",
ArrivalList
);
#[derive(Debug, Clone, PartialEq)]
pub enum Place {
Id(String),
Coord { lat: f64, lon: f64 },
}
impl Default for Place {
fn default() -> Self {
Self::Id(String::new())
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TripSearch {
pub origin: Place,
pub destination: Place,
pub date: Option<String>,
pub time: Option<String>,
pub search_for_arrival: Option<bool>,
pub num_f: Option<u32>,
pub num_b: Option<u32>,
pub context: Option<String>,
pub max_change: Option<u32>,
pub products: Option<u32>,
pub rt_mode: Option<RtMode>,
pub passlist: Option<bool>,
}
impl TripSearch {
pub fn new(origin: Place, destination: Place) -> Self {
Self {
origin,
destination,
..Self::default()
}
}
}
fn place(query: Query, prefix: Prefix, place: &Place) -> Query {
match place {
Place::Id(id) => query.set(prefix.id, id),
Place::Coord { lat, lon } => query.set(prefix.lat, lat).set(prefix.lon, lon),
}
}
#[derive(Clone, Copy)]
struct Prefix {
id: &'static str,
lat: &'static str,
lon: &'static str,
}
const ORIGIN: Prefix = Prefix {
id: "originId",
lat: "originCoordLat",
lon: "originCoordLong",
};
const DESTINATION: Prefix = Prefix {
id: "destId",
lat: "destCoordLat",
lon: "destCoordLong",
};
impl Request for TripSearch {
const PATH: &'static str = "trip";
type Response = TripList;
fn query(&self) -> Vec<(&'static str, String)> {
let query = place(Query::default(), ORIGIN, &self.origin);
place(query, DESTINATION, &self.destination)
.opt("date", self.date.as_ref())
.opt("time", self.time.as_ref())
.flag("searchForArrival", self.search_for_arrival)
.opt("numF", self.num_f)
.opt("numB", self.num_b)
.opt("context", self.context.as_ref())
.opt("maxChange", self.max_change)
.opt("products", self.products)
.opt("rtMode", self.rt_mode.as_ref())
.flag("passlist", self.passlist)
.0
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct JourneyDetail {
pub id: String,
pub date: Option<String>,
pub rt_mode: Option<RtMode>,
}
impl JourneyDetail {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
..Self::default()
}
}
}
impl Request for JourneyDetail {
const PATH: &'static str = "journeyDetail";
type Response = Journey;
fn query(&self) -> Vec<(&'static str, String)> {
Query::default()
.set("id", &self.id)
.opt("date", self.date.as_ref())
.opt("rtMode", self.rt_mode.as_ref())
.0
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Recon {
pub ctx: String,
pub rt_mode: Option<RtMode>,
pub passlist: Option<bool>,
}
impl Recon {
pub fn new(ctx: impl Into<String>) -> Self {
Self {
ctx: ctx.into(),
..Self::default()
}
}
}
impl Request for Recon {
const PATH: &'static str = "recon";
type Response = TripList;
fn query(&self) -> Vec<(&'static str, String)> {
Query::default()
.set("ctx", &self.ctx)
.opt("rtMode", self.rt_mode.as_ref())
.flag("passlist", self.passlist)
.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sends_places_as_ids_or_positions() {
let trip = TripSearch {
search_for_arrival: Some(true),
passlist: Some(false),
..TripSearch::new(
Place::Id("3000010".to_owned()),
Place::Coord {
lat: 50.1,
lon: 8.6,
},
)
};
assert_eq!(
trip.query(),
[
("originId", "3000010".to_owned()),
("destCoordLat", "50.1".to_owned()),
("destCoordLong", "8.6".to_owned()),
("searchForArrival", "1".to_owned()),
("passlist", "0".to_owned()),
]
);
}
}