Skip to content

Commit

Permalink
Merge pull request #709 from patochectp/default_transfer_time
Browse files Browse the repository at this point in the history
[feature] default value for transfer time
  • Loading branch information
ArnaudOggy authored Oct 1, 2020
2 parents 6fc68a7 + d1192e0 commit ddf5490
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Kisio Digital <[email protected]>", "Guillaume Pinot <[email protected]>"]
name = "transit_model"
version = "0.31.3"
version = "0.31.4"
license = "AGPL-3.0-only"
description = "Transit data management"
repository = "https://github.com/CanalTP/transit_model"
Expand Down
27 changes: 25 additions & 2 deletions src/ntfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ mod tests {

#[test]
fn transfers_serialization_deserialization() {
test_serialize_deserialize_collection(vec![
let transfers = vec![
Transfer {
from_stop_id: "st_1".to_string(),
to_stop_id: "st_1".to_string(),
Expand All @@ -783,7 +783,30 @@ mod tests {
real_min_transfer_time: None,
equipment_id: Some("eq_1".to_string()),
},
]);
];
let expected_transfers = vec![
Transfer {
from_stop_id: "st_1".to_string(),
to_stop_id: "st_1".to_string(),
min_transfer_time: Some(20),
real_min_transfer_time: Some(30),
equipment_id: Some("eq_1".to_string()),
},
Transfer {
from_stop_id: "st_1".to_string(),
to_stop_id: "st_2".to_string(),
min_transfer_time: Some(0),
real_min_transfer_time: Some(0),
equipment_id: Some("eq_1".to_string()),
},
];
let collection = Collection::new(transfers);
let expected_collection = Collection::new(expected_transfers);
test_in_tmp_dir(|path| {
write_collection(path, "file.txt", &collection).unwrap();
let des_collection = make_opt_collection(path, "file.txt").unwrap();
assert_eq!(expected_collection, des_collection);
});
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,9 @@ impl Equipment {
pub struct Transfer {
pub from_stop_id: String,
pub to_stop_id: String,
#[serde(serialize_with = "ser_option_u32_with_default")]
pub min_transfer_time: Option<u32>,
#[serde(serialize_with = "ser_option_u32_with_default")]
pub real_min_transfer_time: Option<u32>,
pub equipment_id: Option<String>,
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ where
Option::<T>::deserialize(de).map(|opt| opt.unwrap_or_else(Default::default))
}

pub fn ser_option_u32_with_default<S>(value: &Option<u32>, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_u32(value.unwrap_or_default())
}

pub fn de_with_invalid_option<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down

0 comments on commit ddf5490

Please sign in to comment.