1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::io::{BufReader, Read, Seek};
use std::result;
use std::num::ParseIntError;
use zip::ZipArchive;
use zip::result::ZipError;
use quick_csv::{Csv, Row};
use quick_csv::error::{Error as CsvError};
use chrono::{UTC, ParseError as ParseDateTimeError};
use transit::{Transit, Agency, Stop, StopTime, TimeOffset, Route, Trip, Shape};
error_chain! {
types {
Error, ErrorKind, ChainErr, Result;
}
links {
}
foreign_links {
ZipError, Zip, "Zip file error";
CsvError, Csv, "CSV parsing error";
ParseDateTimeError, ParseDateTime, "DateTime parsing error";
ParseIntError, ParseInt, "Int parsing error";
}
errors {
EmptyCsv {
description("empty csv file")
display("empty csv file")
}
ParseTimeError {
description("invalid time string")
display("invalid time string")
}
}
}
pub struct GTFS {
transit: Transit,
agencies: Vec<Agency>,
stops: Vec<Stop>,
stop_times: Vec<StopTime>,
routes: Vec<Route>,
trips: Vec<Trip>,
shapes: Vec<Shape>,
}
impl GTFS {
pub fn from_reader<R: Read + Seek>(reader: R) -> Result<GTFS> {
let mut archive = try!(ZipArchive::new(reader));
let agencies_reader = try!(archive.by_name("agencies.txt"));
let agencies = try!(GTFS::decode_agencies(agencies_reader));
Ok(GTFS {
transit: Transit {
id: 0,
sha512: String::from(""),
name: String::from("unnamed"),
created: UTC::now(),
},
agencies: agencies,
stops: Vec::new(),
stop_times: Vec::new(),
routes: Vec::new(),
trips: Vec::new(),
shapes: Vec::new(),
})
}
fn decode_agencies<R: Read>(reader: R) -> Result<Vec<Agency>> {
Ok(vec!())
}
fn decode_stops<R: Read>(reader: R) -> Result<Vec<Stop>> {
Ok(vec!())
}
fn decode_stop_times<R: Read>(reader: R) -> Result<Vec<StopTime>> {
let buf_reader = BufReader::new(reader);
let mut csv = Csv::from_reader(buf_reader);
let decoder = try!(StopTimeDecoder::new(&mut csv));
let mut stop_times: Vec<StopTime> = Vec::new();
for result in decoder {
let stop_time = try!(result);
stop_times.push(stop_time);
}
Ok(stop_times)
}
fn decode_routes<R: Read>(reader: R) -> Result<Vec<Route>> {
Ok(vec!())
}
fn decode_trips<R: Read>(reader: R) -> Result<Vec<Trip>> {
Ok(vec!())
}
fn decode_shapes<R: Read>(reader: R) -> Result<Vec<Shape>> {
Ok(vec!())
}
}
fn csv_headers(csv: &mut Iterator<Item=result::Result<Row, CsvError>>) -> Result<Vec<String>> {
match csv.next() {
Some(row_result) => match row_result {
Ok(row) => {
let columns = try!(row.columns());
Ok(columns.into_iter().map(|header| String::from(header)).collect())
}
Err(err) => Err(err.into()),
},
None => Err(ErrorKind::EmptyCsv.into()),
}
}
fn decode_timeoffset(tm: &str) -> Result<TimeOffset> {
let mut parts = tm.trim().split(':');
let parse_part = |part: Option<&str>| -> Result<u32> {
match part {
Some(val) => Ok(try!(val.parse::<u32>())),
None => Err(ErrorKind::ParseTimeError.into()),
}
};
let hours = try!(parse_part(parts.next()));
let minutes = try!(parse_part(parts.next()));
let seconds = try!(parse_part(parts.next()));
Ok(TimeOffset::from_hms(hours, minutes, seconds))
}
pub struct StopTimeDecoder<'a> {
csv: &'a mut Iterator<Item=result::Result<Row, CsvError>>,
headers: Vec<String>,
}
impl<'a> StopTimeDecoder<'a> {
pub fn new(csv: &'a mut Iterator<Item=result::Result<Row, CsvError>>) -> Result<StopTimeDecoder<'a>> {
let headers = { try!(csv_headers(csv)) };
Ok(StopTimeDecoder {
csv: csv,
headers: headers,
})
}
}
impl<'a> Iterator for StopTimeDecoder<'a> {
type Item = Result<StopTime>;
fn next(&mut self) -> Option<Result<StopTime>> {
match self.csv.next() {
None => None,
Some(res) => match res {
Ok(row) => match row.columns() {
Ok(columns) => {
let mut transit_id = 0;
let mut trip_id = String::new();
let mut departure_time = TimeOffset::from_hms(0, 0, 0);
let mut arrival_time = TimeOffset::from_hms(0, 0, 0);
let mut stop_id = String::new();
let mut stop_sequence = 0;
let mut stop_headsign = None;
for (header, column) in self.headers.iter().zip(columns) {
match &header[..] {
"trip_id" => { trip_id = String::from(column); },
"departure_time" => { departure_time = match decode_timeoffset(column) { Ok(naive_time) => naive_time, Err(err) => return Some(Err(err.into())) }; },
"arrival_time" => { arrival_time = match decode_timeoffset(column) { Ok(naive_time) => naive_time, Err(err) => return Some(Err(err.into())) }; },
"stop_id" => { stop_id = String::from(column); },
"stop_sequence" => { let parse_res: result::Result<u64, ParseIntError> = column.parse(); stop_sequence = match parse_res { Ok(stop_seq) => stop_seq, Err(err) => return Some(Err(err.into())) }; },
"stop_headsign" => { stop_headsign = Some(String::from(column)); },
_ => (),
}
}
let stop_time = StopTime {
transit_id: transit_id,
trip_id: trip_id,
departure_time: departure_time,
arrival_time: arrival_time,
stop_id: stop_id,
stop_sequence: stop_sequence,
stop_headsign: stop_headsign,
};
Some(Ok(stop_time))
},
Err(err) => Some(Err(err.into())),
},
Err(err) => Some(Err(err.into())),
}
}
}
}
#[test]
fn decode_timeoffset_test() {
assert_eq!(decode_timeoffset("01:01:01").unwrap(), TimeOffset::from_hms(1, 1, 1));
assert_eq!(decode_timeoffset("1:01:01").unwrap(), TimeOffset::from_hms(1, 1, 1));
assert_eq!(decode_timeoffset("01:01:01 ").unwrap(), TimeOffset::from_hms(1, 1, 1));
assert_eq!(decode_timeoffset(" 01:01:01 ").unwrap(), TimeOffset::from_hms(1, 1, 1));
assert!(decode_timeoffset(":01:01").is_err());
assert!(decode_timeoffset("ab:01:01").is_err());
assert!(decode_timeoffset("01::01").is_err());
}