Skip to content

Commit 3b54e4b

Browse files
authored
Merge pull request #201 from datanel/sanitize-transfers
Sanitize transfers
2 parents 08faa31 + c989476 commit 3b54e4b

6 files changed

Lines changed: 40 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
authors = ["Kisio Digital <guillaume.pinot@kisio.org>"]
33
name = "navitia_model"
4-
version = "0.3.0"
4+
version = "0.3.1"
55
edition = "2018"
66

77
[dependencies]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from_stop_id,to_stop_id,min_transfer_time,real_min_transfer_time,equipment_id
2+
CDGR,CDGR,0,60,
3+
DEFR,DEFR,0,60,
4+
GDLB,GDLB,0,60,
5+
NATM,NATM,0,60,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from_stop_id,to_stop_id,min_transfer_time,real_min_transfer_time,equipment_id
2+
GDLB,GDLB,0,60,
3+
NATM,NATM,0,60,

src/collection.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,32 @@ impl<T> Collection<T> {
273273
pub fn is_empty(&self) -> bool {
274274
self.objects.is_empty()
275275
}
276+
277+
/// Retains the elements matching predicate parameter from the current `CollectionWithId` object
278+
///
279+
/// # Examples
280+
///
281+
/// ```
282+
/// # use navitia_model::collection::*;
283+
/// # use std::collections::HashSet;
284+
/// # fn run() -> navitia_model::Result<()> {
285+
/// # #[derive(PartialEq, Debug)] struct Obj(&'static str);
286+
/// let mut c = Collection::new(vec![Obj("foo"), Obj("bar"), Obj("qux")]);
287+
/// let mut ids_to_keep: HashSet<String> = HashSet::new();
288+
/// ids_to_keep.insert("foo".to_string());
289+
/// ids_to_keep.insert("qux".to_string());
290+
/// c.retain(|item| ids_to_keep.contains(item.0));
291+
/// assert_eq!(c.len(), 2);
292+
/// assert_eq!(c.values().map(|obj| obj.0).collect::<Vec<&str>>(), ["foo", "qux"]);
293+
/// # Ok(())
294+
/// # }
295+
/// # fn main() { run().unwrap() }
296+
/// ```
297+
pub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F) {
298+
let mut purged = self.take();
299+
purged.retain(f);
300+
*self = Self::new(purged);
301+
}
276302
}
277303

278304
/// The type returned by `Collection::iter`.

src/model.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ impl Collections {
442442
.retain(|commercial_mode| commercial_modes_used.contains(&commercial_mode.id));
443443
self.physical_modes
444444
.retain(|physical_mode| physical_modes_used.contains(&physical_mode.id));
445+
self.transfers.retain(|t| {
446+
stop_points_used.contains(&t.from_stop_id) && stop_points_used.contains(&t.to_stop_id)
447+
});
448+
445449
Ok(())
446450
}
447451
}

tests/restrict_validity_period.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ fn test_restrict_global() {
5353
"trips.txt",
5454
"object_codes.txt",
5555
"object_properties.txt",
56+
"transfers.txt",
5657
]),
5758
"./fixtures/restrict-validity-period/output/",
5859
);

0 commit comments

Comments
 (0)