Skip to content
Open
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
54 changes: 40 additions & 14 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Serialize for LocationType {

/// Describes the kind of [Route]. See <https://gtfs.org/reference/static/#routestxt> `route_type`
///
/// -ome route types are extended GTFS (<https://developers.google.com/transit/gtfs/reference/extended-route-types)>
/// Some route types are extended GTFS (<https://developers.google.com/transit/gtfs/reference/extended-route-types)>
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
pub enum RouteType {
/// Tram, Streetcar, Light rail. Any light rail or street level system within a metropolitan area
Expand Down Expand Up @@ -116,26 +116,52 @@ pub enum RouteType {
Other(i16),
}

impl RouteType {
/// Simplify [`RouteType::Other`] into the named variants of the enum.
/// This may lose precision for extended route types.
pub fn simplify(&self) -> RouteType {
match self {
RouteType::Other(extended) => {
let hundreds = extended / 100;
match (*extended, hundreds) {
(0, _) | (_, 9) => RouteType::Tramway,
(1, _) | (_, 4) => RouteType::Subway,
(2, _) | (_, 1) => RouteType::Rail,
(3, _) | (_, 7) | (_, 8) => RouteType::Bus,
(4, _) | (_, 10) | (_, 12) => RouteType::Ferry,
(5, _) => RouteType::CableCar,
(6, _) | (_, 13) => RouteType::Gondola,
(7, _) | (_, 14) => RouteType::Funicular,
(_, 2) => RouteType::Coach,
(_, 11) => RouteType::Air,
(_, 15) => RouteType::Taxi,
_ => RouteType::Other(*extended),
}
}
route_type => *route_type,
}
}
}

impl<'de> Deserialize<'de> for RouteType {
fn deserialize<D>(deserializer: D) -> Result<RouteType, D::Error>
where
D: Deserializer<'de>,
{
let i = i16::deserialize(deserializer)?;

let hundreds = i / 100;
Ok(match (i, hundreds) {
(0, _) | (_, 9) => RouteType::Tramway,
(1, _) | (_, 4) => RouteType::Subway,
(2, _) | (_, 1) => RouteType::Rail,
(3, _) | (_, 7) | (_, 8) => RouteType::Bus,
(4, _) | (_, 10) | (_, 12) => RouteType::Ferry,
(5, _) => RouteType::CableCar,
(6, _) | (_, 13) => RouteType::Gondola,
(7, _) | (_, 14) => RouteType::Funicular,
(_, 2) => RouteType::Coach,
(_, 11) => RouteType::Air,
(_, 15) => RouteType::Taxi,
Ok(match i {
0 => RouteType::Tramway,
1 => RouteType::Subway,
2 => RouteType::Rail,
3 => RouteType::Bus,
4 => RouteType::Ferry,
5 => RouteType::CableCar,
6 => RouteType::Gondola,
7 => RouteType::Funicular,
200 => RouteType::Coach,
1100 => RouteType::Air,
1500 => RouteType::Taxi,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

How exactly?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For example for Taxi, this version would only match 1500 exactly and not all variant listed above. Note that maybe I'm missing something.

_ => RouteType::Other(i),
})
}
Expand Down
Loading