1- use crate :: { objects:: * , Error , RawGtfs } ;
1+ use crate :: { collection :: CollectionWithID , objects:: * , Error , RawGtfs } ;
22use chrono:: prelude:: NaiveDate ;
33use chrono:: Duration ;
44use std:: collections:: { HashMap , HashSet } ;
55use std:: convert:: TryFrom ;
6- use std:: sync:: Arc ;
76
87/// Data structure with all the GTFS objects
98///
@@ -27,8 +26,8 @@ pub struct Gtfs {
2726 pub calendar : HashMap < String , Calendar > ,
2827 /// All calendar dates grouped by service_id
2928 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 > ,
3231 /// All routes by `route_id`
3332 pub routes : HashMap < String , Route > ,
3433 /// All trips by `trip_id`
@@ -49,7 +48,7 @@ impl TryFrom<RawGtfs> for Gtfs {
4948 ///
5049 /// It might fail if some mandatory files couldn’t be read or if there are references to other objects that are invalid.
5150 fn try_from ( raw : RawGtfs ) -> Result < Gtfs , Error > {
52- let stops = to_stop_map (
51+ let stops = to_stop_collection (
5352 raw. stops ?,
5453 raw. transfers . unwrap_or_else ( || Ok ( Vec :: new ( ) ) ) ?,
5554 raw. pathways . unwrap_or ( Ok ( Vec :: new ( ) ) ) ?,
@@ -176,10 +175,9 @@ impl Gtfs {
176175
177176 /// Gets a [Stop] by its `stop_id`
178177 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 ( ) ) )
183181 }
184182
185183 /// Gets a [Trip] by its `trip_id`
@@ -232,37 +230,34 @@ fn to_map<O: Id>(elements: impl IntoIterator<Item = O>) -> HashMap<String, O> {
232230 . collect ( )
233231}
234232
235- fn to_stop_map (
233+ fn to_stop_collection (
236234 stops : Vec < Stop > ,
237235 raw_transfers : Vec < RawTransfer > ,
238236 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 ( ) ;
242239
243240 for transfer in raw_transfers {
244- stop_map
245- . get ( & transfer. to_stop_id )
241+ stops
242+ . get_by_id ( & transfer. to_stop_id )
246243 . 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) ) ;
250248 }
251249
252250 for pathway in raw_pathways {
253- stop_map
254- . get ( & pathway. to_stop_id )
251+ stops
252+ . get_by_id ( & pathway. to_stop_id )
255253 . 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) ) ;
259258 }
260259
261- let res = stop_map
262- . into_iter ( )
263- . map ( |( i, s) | ( i, Arc :: new ( s) ) )
264- . collect ( ) ;
265- Ok ( res)
260+ Ok ( stops)
266261}
267262
268263fn to_shape_map ( shapes : Vec < Shape > ) -> HashMap < String , Vec < Shape > > {
@@ -292,7 +287,7 @@ fn create_trips(
292287 raw_trips : Vec < RawTrip > ,
293288 raw_stop_times : Vec < RawStopTime > ,
294289 raw_frequencies : Vec < RawFrequency > ,
295- stops : & HashMap < String , Arc < Stop > > ,
290+ stops : & CollectionWithID < Stop > ,
296291) -> Result < HashMap < String , Trip > , Error > {
297292 let mut trips = to_map ( raw_trips. into_iter ( ) . map ( |rt| Trip {
298293 id : rt. id ,
@@ -313,9 +308,9 @@ fn create_trips(
313308 . get_mut ( & s. trip_id )
314309 . ok_or_else ( || Error :: ReferenceError ( s. trip_id . to_string ( ) ) ) ?;
315310 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_else ( ||Error :: ReferenceError ( s. stop_id . to_string ( ) ) ) ?;
313+ trip. stop_times . push ( StopTime :: from ( & s, * stop) ) ;
319314 }
320315
321316 for trip in & mut trips. values_mut ( ) {
0 commit comments