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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! Transit-specific costing options
use serde::Serialize;
#[serde_with::skip_serializing_none]
#[derive(Serialize, Debug, Clone, Default, PartialEq)]
pub(crate) struct TransitCostingOptionsInner {
use_bus: Option<f32>,
use_rail: Option<f32>,
use_transfers: Option<f32>,
filters: Option<Filters>,
}
#[derive(Serialize, Debug, Clone, Default, PartialEq)]
/// Transit costing options
pub struct TransitCostingOptions {
pub(crate) transit: TransitCostingOptionsInner,
}
impl TransitCostingOptions {
#[must_use]
/// Builder for [`TransitCostingOptions`]
pub fn builder() -> Self {
Self::default()
}
/// User's desire to use buses.
///
/// Range of values from
/// - `0` (try to avoid buses) to
/// - `1` (strong preference for riding buses).
pub fn use_bus(mut self, use_bus: f32) -> Self {
self.transit.use_bus = Some(use_bus);
self
}
/// User's desire to use rail/subway/metro.
///
/// Range of values from
/// - `0` (try to avoid rail) to
/// - `1` (strong preference for riding rail).
pub fn use_rail(mut self, use_rail: f32) -> Self {
self.transit.use_rail = Some(use_rail);
self
}
/// User's desire to favor transfers.
///
/// Range of values from
/// - `0` (try to avoid transfers) to
/// - `1` (totally comfortable with transfers).
pub fn use_transfers(mut self, use_transfers: f32) -> Self {
self.transit.use_transfers = Some(use_transfers);
self
}
/// Sets a filter for one or more ~~`stops`~~ (TODO: need to re-enable)
///
/// Filters must contain a list of so-called Onestop IDs, which is (supposed to be) a
/// unique identifier for GTFS data, and an [`Action`].
/// The OneStop ID is simply the feeds's directory name and the object's GTFS ID separated
/// by an underscore.
///
/// Example:
/// A route with `route_id: AUR` in `routes.txt` from the feed `NYC` would have
/// the OneStop ID `NYC_AUR`, similar with operators/agencies
///
/// **Tip**: Can be combined with [`Self::filter_routes`] and/or [`Self::filter_operators`]
#[doc(hidden)] // TODO: enable once this works in valhalla
pub fn filter_stops<S>(mut self, ids: impl IntoIterator<Item = S>, action: Action) -> Self
where
S: Into<String>,
{
let new_filter = Filter {
ids: ids.into_iter().map(Into::into).collect(),
action,
};
if let Some(ref mut filters) = self.transit.filters {
filters.stops = Some(new_filter);
} else {
self.transit.filters = Some(Filters {
stops: Some(new_filter),
..Default::default()
});
}
self
}
/// Sets a filter for one or more `routes`
///
/// Filters must contain a list of so-called Onestop IDs, which is (supposed to be) a
/// unique identifier for GTFS data, and an [`Action`].
/// The OneStop ID is simply the feeds's directory name and the object's GTFS ID separated
/// by an underscore.
///
/// Example:
/// A route with `route_id: AUR` in `routes.txt` from the feed `NYC` would have
/// the OneStop ID `NYC_AUR`, similar with operators/agencies
///
/// **Tip**: Can be combined with [`Self::filter_stops`] and/or [`Self::filter_operators`]
pub fn filter_routes<S>(mut self, ids: impl IntoIterator<Item = S>, action: Action) -> Self
where
S: Into<String>,
{
let new_filter = Filter {
ids: ids.into_iter().map(Into::into).collect(),
action,
};
if let Some(ref mut filters) = self.transit.filters {
filters.routes = Some(new_filter);
} else {
self.transit.filters = Some(Filters {
routes: Some(new_filter),
..Default::default()
});
}
self
}
/// Sets a filter for one or more `operators`.
///
/// Filters must contain a list of so-called Onestop IDs, which is (supposed to be) a
/// unique identifier for GTFS data, and an [`Action`].
/// The OneStop ID is simply the feeds's directory name and the object's GTFS ID separated
/// by an underscore.
///
/// Example:
/// A route with `route_id: AUR` in `routes.txt` from the feed `NYC` would have
/// the OneStop ID `NYC_AUR`, similar with operators/agencies
///
/// **Tip**: Can be combined with [`Self::filter_stops`] and/or [`Self::filter_routes`]
pub fn filter_operators<S>(mut self, ids: impl IntoIterator<Item = S>, action: Action) -> Self
where
S: Into<String>,
{
let new_filter = Filter {
ids: ids.into_iter().map(Into::into).collect(),
action,
};
if let Some(ref mut filters) = self.transit.filters {
filters.operators = Some(new_filter);
} else {
self.transit.filters = Some(Filters {
operators: Some(new_filter),
..Default::default()
});
}
self
}
}
#[derive(Serialize, Debug, Clone, Copy, Default, PartialEq, Eq)]
/// Action to take when filtering
pub enum Action {
/// Include only the `ids` listed in the filter
#[default]
Include,
/// Exclude all the `ids` listed in the filter
Exclude,
}
#[serde_with::skip_serializing_none]
#[derive(Serialize, Debug, Clone, Default, PartialEq)]
struct Filters {
routes: Option<Filter>,
operators: Option<Filter>,
stops: Option<Filter>,
}
#[derive(Serialize, Debug, Clone, Default, PartialEq, Eq)]
struct Filter {
ids: Vec<String>,
action: Action,
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn serialisation() {
assert_eq!(
serde_json::to_value(TransitCostingOptions::default()).unwrap(),
serde_json::json!({"transit":{}})
);
}
#[test]
fn builder_returns_default() {
assert_eq!(
TransitCostingOptions::builder(),
TransitCostingOptions::default()
);
}
#[test]
fn use_bus_sets_value() {
let opts = TransitCostingOptions::builder().use_bus(0.8);
assert_eq!(opts.transit.use_bus, Some(0.8));
}
#[test]
fn use_rail_sets_value() {
let opts = TransitCostingOptions::builder().use_rail(0.9);
assert_eq!(opts.transit.use_rail, Some(0.9));
}
#[test]
fn use_transfers_sets_value() {
let opts = TransitCostingOptions::builder().use_transfers(0.5);
assert_eq!(opts.transit.use_transfers, Some(0.5));
}
#[test]
fn filter_routes_sets_value() {
let opts = TransitCostingOptions::builder()
.filter_routes(vec!["NYC_AUR", "NYC_BX"], Action::Include);
assert!(opts.transit.filters.is_some());
let filters = opts.transit.filters.unwrap();
assert!(filters.routes.is_some());
let route_filter = filters.routes.unwrap();
assert_eq!(route_filter.ids, vec!["NYC_AUR", "NYC_BX"]);
assert_eq!(route_filter.action, Action::Include);
}
#[test]
fn filter_operators_sets_value() {
let opts =
TransitCostingOptions::builder().filter_operators(vec!["NYC_MTA"], Action::Exclude);
assert!(opts.transit.filters.is_some());
let filters = opts.transit.filters.unwrap();
assert!(filters.operators.is_some());
let op_filter = filters.operators.unwrap();
assert_eq!(op_filter.ids, vec!["NYC_MTA"]);
assert_eq!(op_filter.action, Action::Exclude);
}
#[test]
fn chaining_works() {
let opts = TransitCostingOptions::builder()
.use_bus(0.7)
.use_rail(0.9)
.use_transfers(0.4);
assert_eq!(opts.transit.use_bus, Some(0.7));
assert_eq!(opts.transit.use_rail, Some(0.9));
assert_eq!(opts.transit.use_transfers, Some(0.4));
}
}