1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use chrono::Utc;
use crate::Request;
use crate::resrobot::{Language, Location, RouteResponse, RouteResponseRaw};
pub struct RouteRequest {
access_id: String,
origin: Location,
destination: Location,
language: Language,
search_for_arrival: bool,
pass_list: bool,
date_time: chrono::DateTime<Utc>,
count_before: u8,
count_after: u8,
max_transfer: Option<u8>,
}
impl RouteRequest {
/// Creates a new [`RouteRequest`] with the required fields.
///
/// Defaults:
/// - `language`: [`Language::Swedish`]
/// - `search_for_arrival`: `false`
/// - `pass_list`: `false`
/// - `date_time`: current time
/// - `count_after`: `5`
/// - `count_before`: `0`
/// - `max_transfer`: `None`
pub fn new(access_id: String, origin: Location, destination: Location) -> Self {
Self {
access_id, // ✓
origin, // ✓
destination, // ✓
language: Default::default(), // ✓
search_for_arrival: false, // ✓
pass_list: false, // ✓
date_time: chrono::Utc::now(), // ✓
count_after: 5, // ✓
count_before: 0, // ✓
max_transfer: None, // ✓
}
}
/// Sets the language of the API response.
///
/// Defaults to [`Language::Swedish`].
///
/// Affects both data (e.g., transport type names) and error messages.
///
/// Chainable.
pub fn with_language(mut self, value: Language) -> Self {
self.language = value;
self
}
/// Includes a list of intermediate stops in the response.
///
/// Defaults to `false`.
///
/// Chainable.
pub fn with_pass_list(mut self, value: bool) -> Self {
self.pass_list = value;
self
}
/// Searches for the *latest arrival time* instead of the default
/// *earliest departure time*.
///
/// When set to `true`, [`with_count_after`] and [`with_count_before`]
/// are ignored.
///
/// Chainable.
pub fn with_search_for_arrival(mut self, value: bool) -> Self {
self.search_for_arrival = value;
self
}
/// Sets the time to search around.
///
/// Uses Central European Standard Time (GMT+1).
///
/// Combine with [`with_search_for_arrival`] to interpret the time as
/// an arrival time instead of a departure time.
///
/// Chainable.
pub fn with_time(mut self, value: chrono::DateTime<Utc>) -> Self {
self.date_time = value;
self
}
/// Sets how many routes should be returned *after* the given time.
///
/// Defaults to `5`.
///
/// Ignored when [`with_search_for_arrival`] is `true`.
///
/// The sum of `count_after` and `count_before` must not exceed **6**.
///
/// # Panics
/// Panics if `self.count_before + value > 6`.
///
/// Chainable.
pub fn with_count_after(mut self, value: u8) -> Self {
assert!(self.count_before + value <= 6);
self.count_after = value;
self
}
/// Sets how many routes should be returned *before* the given time.
///
/// Defaults to `0`.
///
/// Ignored when [`with_search_for_arrival`] is `true`.
///
/// The sum of `count_after` and `count_before` must not exceed **6**.
///
/// # Panics
/// Panics if `value + self.count_after > 6`.
///
/// Chainable.
pub fn with_count_before(mut self, value: u8) -> Self {
assert!(value + self.count_after <= 6);
self.count_before = value;
self
}
/// Limits the maximum number of transfers.
///
/// Valid values are `1` through `3`.
/// Use `None` for unlimited transfers.
///
/// Defaults to `None`.
///
/// # Panics
/// Panics if the value is not in the range `1..=3`.
///
/// Chainable.
pub fn with_max_transfers(mut self, value: u8) -> Self {
// has to be between 1 and 3
assert!((1..4).contains(&value));
self.max_transfer = Some(value);
self
}
}
impl Request for RouteRequest {
type Output = RouteResponse;
async fn send(self) -> Result<Self::Output, crate::Error> {
let url = self.build_url()?;
let res = reqwest::get(url).await?;
let raw: RouteResponseRaw = res.json().await?;
Ok(raw.into())
}
fn build_url(&self) -> Result<reqwest::Url, crate::Error> {
// See https://www.trafiklab.se/api/our-apis/resrobot-v21/route-planner/
const ORIGIN: &str = "origin";
const DEST: &str = "dest";
const PARAM_COUNT: usize = 14;
let mut params: Vec<(String, String)> = Vec::with_capacity(PARAM_COUNT);
params.push(("format".into(), "json".into()));
params.push(("accessId".into(), self.access_id.clone()));
params.push(("lang".into(), self.language.to_string()));
params.append(&mut self.origin.as_query_params(ORIGIN));
params.append(&mut self.destination.as_query_params(DEST));
params.push(("numF".into(), self.count_after.to_string()));
params.push(("numB".into(), self.count_before.to_string()));
params.push(("passlist".into(), self.pass_list.to_string()));
params.push((
"searchForArrival".into(),
self.search_for_arrival.to_string(),
));
if let Some(value) = self.max_transfer {
params.push(("maxChange".into(), value.to_string()))
}
let url = reqwest::Url::parse_with_params("https://api.resrobot.se/v2.1/trip", params)?;
Ok(url)
}
}