1
- use crate :: { objects:: * , Error , RawGtfs } ;
1
+ use crate :: { collection :: CollectionWithID , objects:: * , Error , RawGtfs } ;
2
2
use chrono:: prelude:: NaiveDate ;
3
3
use chrono:: Duration ;
4
4
use std:: collections:: { HashMap , HashSet } ;
5
5
use std:: convert:: TryFrom ;
6
- use std:: sync:: Arc ;
7
6
8
7
/// Data structure with all the GTFS objects
9
8
///
@@ -27,8 +26,8 @@ pub struct Gtfs {
27
26
pub calendar : HashMap < String , Calendar > ,
28
27
/// All calendar dates grouped by service_id
29
28
pub calendar_dates : HashMap < String , Vec < CalendarDate > > ,
30
- /// All stop by `stop_id`. Stops are in an [Arc] because they are also referenced by each [StopTime]
31
- pub stops : HashMap < String , Arc < Stop > > ,
29
+ /// All stop by `stop_id`
30
+ pub stops : CollectionWithID < Stop > ,
32
31
/// All routes by `route_id`
33
32
pub routes : HashMap < String , Route > ,
34
33
/// All trips by `trip_id`
@@ -49,7 +48,7 @@ impl TryFrom<RawGtfs> for Gtfs {
49
48
///
50
49
/// It might fail if some mandatory files couldn’t be read or if there are references to other objects that are invalid.
51
50
fn try_from ( raw : RawGtfs ) -> Result < Gtfs , Error > {
52
- let stops = to_stop_map (
51
+ let stops = to_stop_collection (
53
52
raw. stops ?,
54
53
raw. transfers . unwrap_or_else ( || Ok ( Vec :: new ( ) ) ) ?,
55
54
raw. pathways . unwrap_or ( Ok ( Vec :: new ( ) ) ) ?,
@@ -176,10 +175,9 @@ impl Gtfs {
176
175
177
176
/// Gets a [Stop] by its `stop_id`
178
177
pub fn get_stop < ' a > ( & ' a self , id : & str ) -> Result < & ' a Stop , Error > {
179
- match self . stops . get ( id) {
180
- Some ( stop) => Ok ( stop) ,
181
- None => Err ( Error :: ReferenceError ( id. to_owned ( ) ) ) ,
182
- }
178
+ self . stops
179
+ . get_by_id ( id)
180
+ . ok_or_else ( || Error :: ReferenceError ( id. to_owned ( ) ) )
183
181
}
184
182
185
183
/// Gets a [Trip] by its `trip_id`
@@ -232,37 +230,34 @@ fn to_map<O: Id>(elements: impl IntoIterator<Item = O>) -> HashMap<String, O> {
232
230
. collect ( )
233
231
}
234
232
235
- fn to_stop_map (
233
+ fn to_stop_collection (
236
234
stops : Vec < Stop > ,
237
235
raw_transfers : Vec < RawTransfer > ,
238
236
raw_pathways : Vec < RawPathway > ,
239
- ) -> Result < HashMap < String , Arc < Stop > > , Error > {
240
- let mut stop_map: HashMap < String , Stop > =
241
- stops. into_iter ( ) . map ( |s| ( s. id . clone ( ) , s) ) . collect ( ) ;
237
+ ) -> Result < CollectionWithID < Stop > , Error > {
238
+ let mut stops: CollectionWithID < Stop > = stops. into_iter ( ) . collect ( ) ;
242
239
243
240
for transfer in raw_transfers {
244
- stop_map
245
- . get ( & transfer. to_stop_id )
241
+ stops
242
+ . get_by_id ( & transfer. to_stop_id )
246
243
. ok_or_else ( || Error :: ReferenceError ( transfer. to_stop_id . to_string ( ) ) ) ?;
247
- stop_map
248
- . entry ( transfer. from_stop_id . clone ( ) )
249
- . and_modify ( |stop| stop. transfers . push ( StopTransfer :: from ( transfer) ) ) ;
244
+ let s = stops
245
+ . get_mut_by_id ( & transfer. from_stop_id )
246
+ . ok_or_else ( || Error :: ReferenceError ( transfer. from_stop_id . to_string ( ) ) ) ?;
247
+ s. transfers . push ( StopTransfer :: from ( transfer) ) ;
250
248
}
251
249
252
250
for pathway in raw_pathways {
253
- stop_map
254
- . get ( & pathway. to_stop_id )
251
+ stops
252
+ . get_by_id ( & pathway. to_stop_id )
255
253
. ok_or_else ( || Error :: ReferenceError ( pathway. to_stop_id . to_string ( ) ) ) ?;
256
- stop_map
257
- . entry ( pathway. from_stop_id . clone ( ) )
258
- . and_modify ( |stop| stop. pathways . push ( Pathway :: from ( pathway) ) ) ;
254
+ let s = stops
255
+ . get_mut_by_id ( & pathway. from_stop_id )
256
+ . ok_or_else ( || Error :: ReferenceError ( pathway. to_stop_id . to_string ( ) ) ) ?;
257
+ s. pathways . push ( Pathway :: from ( pathway) ) ;
259
258
}
260
259
261
- let res = stop_map
262
- . into_iter ( )
263
- . map ( |( i, s) | ( i, Arc :: new ( s) ) )
264
- . collect ( ) ;
265
- Ok ( res)
260
+ Ok ( stops)
266
261
}
267
262
268
263
fn to_shape_map ( shapes : Vec < Shape > ) -> HashMap < String , Vec < Shape > > {
@@ -292,7 +287,7 @@ fn create_trips(
292
287
raw_trips : Vec < RawTrip > ,
293
288
raw_stop_times : Vec < RawStopTime > ,
294
289
raw_frequencies : Vec < RawFrequency > ,
295
- stops : & HashMap < String , Arc < Stop > > ,
290
+ stops : & CollectionWithID < Stop > ,
296
291
) -> Result < HashMap < String , Trip > , Error > {
297
292
let mut trips = to_map ( raw_trips. into_iter ( ) . map ( |rt| Trip {
298
293
id : rt. id ,
@@ -313,9 +308,9 @@ fn create_trips(
313
308
. get_mut ( & s. trip_id )
314
309
. ok_or_else ( || Error :: ReferenceError ( s. trip_id . to_string ( ) ) ) ?;
315
310
let stop = stops
316
- . get ( & s. stop_id )
317
- . ok_or_else ( || Error :: ReferenceError ( s. stop_id . to_string ( ) ) ) ?;
318
- trip. stop_times . push ( StopTime :: from ( & s, Arc :: clone ( stop ) ) ) ;
311
+ . get_index ( & s. stop_id )
312
+ . ok_or ( Error :: ReferenceError ( s. stop_id . to_string ( ) ) ) ?;
313
+ trip. stop_times . push ( StopTime :: from ( & s, stop . clone ( ) ) ) ;
319
314
}
320
315
321
316
for trip in & mut trips. values_mut ( ) {
0 commit comments