Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] improve examples #692

Merged
merged 3 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ This repository regroups crates that offer enabler-libraries and binaries to
convert and enrich transit data.

Additionally, `transit_model` is itself a library providing various
functionalities. Please refer to the code and documentation to discover them.
functionalities. Please refer to the code, [examples](examples) and
[documentation](https://docs.rs/transit_model) to discover them.

Please check documentation attached to each crate:

Expand Down
15 changes: 15 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# `transit_model` library usage examples

This directory provides simple examples about the usage of the `transit_model` library.

To check output, launch examples in the desired data-directory:

```sh
cd ../tests/fixtures/ntfs
cargo run --example ntfs_crawler
```

```sh
cd ../tests/fixtures/gtfs
cargo run --example gtfs_reader
```
6 changes: 4 additions & 2 deletions examples/gtfs_reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2017 Kisio Digital and/or its affiliates.
// Copyright (C) 2020 Kisio Digital and/or its affiliates.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
Expand Down Expand Up @@ -29,9 +29,11 @@ fn run() -> Result<()> {
on_demand_transport: false,
on_demand_transport_comment: None,
};
// read GTFS from current directory
let objects = gtfs::read_from_path(".", configuration)?;
// output internal model as JSON
let json_objs = json!(objects);
println!("{:?}", json_objs.to_string());
println!("{}", json_objs.to_string());
Ok(())
}

Expand Down
32 changes: 23 additions & 9 deletions examples/test.rs → examples/ntfs_crawler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2017 Kisio Digital and/or its affiliates.
// Copyright (C) 2020 Kisio Digital and/or its affiliates.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
Expand Down Expand Up @@ -30,16 +30,30 @@ where
}

fn run() -> Result<()> {
let objects = transit_model::ntfs::read(".")?;
// load ntfs from current directory
let transit_objects = transit_model::ntfs::read(".")?;

for (from, stop_area) in &objects.stop_areas {
let cms = get(from, &objects.commercial_modes, &objects);
let pms = get(from, &objects.physical_modes, &objects);
let ns = get(from, &objects.networks, &objects);
let cs = get(from, &objects.contributors, &objects);
// stop_area by stop_area, print PT objects related to it
for (idx, stop_area) in &transit_objects.stop_areas {
// retrieve idx from id
assert_eq!(
transit_objects.stop_areas.get_idx(&stop_area.id).unwrap(),
idx
);

// lines passing by stop
let lines = get(idx, &transit_objects.lines, &transit_objects);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completly minor, but don't you think that for the purpose of an example it would be more straightforward to directly use get_corresponding? (at least for the first ones)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True (especially if it goes in main readme), I tried to mix existing and suggestions 👯

// physical_modes stopping at stop
let pms = get(idx, &transit_objects.physical_modes, &transit_objects);
// networks using stop
let ns = get(idx, &transit_objects.networks, &transit_objects);
// contributors providing the data for stop
let cs = get(idx, &transit_objects.contributors, &transit_objects);
// access stop_area through its idx to get name
let stop_name = &transit_objects.stop_areas[idx].name;
println!(
"{}: cms: {:?}, pms: {:?}, ns: {:?}, cs: {:?}, codes: {:?}",
stop_area.id, cms, pms, ns, cs, stop_area.codes
"stop_area {} ({}): lines: {:?}, physical_modes: {:?}, networks: {:?}, contributors: {:?}, codes: {:?}",
stop_area.id, stop_name, lines, pms, ns, cs, stop_area.codes
);
}
Ok(())
Expand Down
5 changes: 3 additions & 2 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use relational_types::{GetCorresponding, IdxSet, ManyToMany, OneToMany, Relation
use serde::{Deserialize, Serialize};
use skip_error::skip_error_and_log;
use std::{
cmp::{self, Ordering},
cmp::{self, Ordering, Reverse},
collections::{BTreeMap, HashMap, HashSet},
convert::TryFrom,
iter::FromIterator,
Expand Down Expand Up @@ -655,8 +655,9 @@ impl Collections {
// [[9,10],[12,13,14],[23,0,1]] --> [[12,13,14],[23,0,1],[9,10]]
// *** then, in case of equal width, sorts by taking the segment early in the morning (smallest index)
// --> [[23,0,1],[12,13,14],[9,10]]
#[allow(clippy::unnecessary_sort_by)] // key borrows, so lint is "wrong"
holes.sort_unstable_by(|l, r| l.iter().min().cmp(&r.iter().min()));
holes.sort_unstable_by(|l, r| r.len().cmp(&l.len()));
holes.sort_by_key(|v| Reverse(v.len()));
holes.first().and_then(|mh| {
let first_idx = mh.first();
let last_idx = mh.last();
Expand Down
2 changes: 0 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ where
}

// The signature of the function must pass by reference for 'serde' to be able to use the function
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn ser_from_bool<S>(v: &bool, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand All @@ -104,7 +103,6 @@ where
}

// The signature of the function must pass by reference for 'serde' to be able to use the function
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn ser_from_naive_date<S>(date: &Date, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand Down
4 changes: 2 additions & 2 deletions src/vptranslator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ mod tests {
date: Date::from_ymd(2012, 7, 13),
exception_type: ExceptionType::Remove,
},
res.exceptions.iter().next().unwrap()
res.exceptions.get(0).unwrap()
);
assert_eq!(
ValidityPeriod {
Expand All @@ -462,7 +462,7 @@ mod tests {
date: Date::from_ymd(2012, 7, 12),
exception_type: ExceptionType::Add,
},
res.exceptions.iter().next().unwrap()
res.exceptions.get(0).unwrap()
);
assert_eq!(
ValidityPeriod {
Expand Down
1 change: 0 additions & 1 deletion tests/read_ntfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ where
}

#[test]
#[allow(clippy::cognitive_complexity)]
fn minimal() {
let ntm = transit_model::ntfs::read("tests/fixtures/minimal_ntfs/").unwrap();

Expand Down