|
| 1 | +use chrono::NaiveDate; |
| 2 | +use csv::ReaderBuilder; |
| 3 | +use serde::{self, Deserialize, Deserializer}; |
| 4 | +use std::{fs::File, io, path::Path}; |
| 5 | +use zip::ZipArchive; |
| 6 | + |
| 7 | +/// a single row from calendar.txt in a GTFS-Flex feed |
| 8 | +#[derive(Debug, Deserialize)] |
| 9 | +pub struct Calendar { |
| 10 | + /// unique service identifier |
| 11 | + pub service_id: String, |
| 12 | + |
| 13 | + /// service availability by day (0 or 1) |
| 14 | + pub monday: u8, |
| 15 | + pub tuesday: u8, |
| 16 | + pub wednesday: u8, |
| 17 | + pub thursday: u8, |
| 18 | + pub friday: u8, |
| 19 | + pub saturday: u8, |
| 20 | + pub sunday: u8, |
| 21 | + |
| 22 | + /// service start date (YYYYMMDD) |
| 23 | + #[serde(deserialize_with = "gtfs_flex_date")] |
| 24 | + pub start_date: NaiveDate, |
| 25 | + |
| 26 | + /// service end date (YYYYMMDD) |
| 27 | + #[serde(deserialize_with = "gtfs_flex_date")] |
| 28 | + pub end_date: NaiveDate, |
| 29 | +} |
| 30 | + |
| 31 | +/// deserialize GTFS-Flex dates in YYYYMMDD format |
| 32 | +fn gtfs_flex_date<'de, D>(deserializer: D) -> Result<NaiveDate, D::Error> |
| 33 | +where |
| 34 | + D: Deserializer<'de>, |
| 35 | +{ |
| 36 | + let s = String::deserialize(deserializer)?; |
| 37 | + NaiveDate::parse_from_str(&s, "%Y%m%d").map_err(serde::de::Error::custom) |
| 38 | +} |
| 39 | + |
| 40 | +/// read calendar.txt from a single GTFS-Flex ZIP file |
| 41 | +/// |
| 42 | +/// streams data directly from the ZIP |
| 43 | +/// returns None if calendar.txt is missing or duplicated |
| 44 | +/// returns typed Calendar rows on success |
| 45 | +pub fn read_calendar_from_flex(zip_path: &Path) -> io::Result<Option<Vec<Calendar>>> { |
| 46 | + // open the zip file |
| 47 | + let file = File::open(zip_path)?; |
| 48 | + let mut archive = ZipArchive::new(file)?; |
| 49 | + |
| 50 | + // locate calendar.txt |
| 51 | + let mut calendar_name: Option<String> = None; |
| 52 | + |
| 53 | + for i in 0..archive.len() { |
| 54 | + let file_in_zip = archive.by_index(i)?; |
| 55 | + |
| 56 | + if file_in_zip.name().ends_with("calendar.txt") { |
| 57 | + // donot allow multiple calendar.txt files in a zip |
| 58 | + if calendar_name.is_some() { |
| 59 | + eprintln!( |
| 60 | + "WARNING: Multiple calendar.txt found in {:?}. Skipping ZIP.", |
| 61 | + zip_path |
| 62 | + ); |
| 63 | + return Ok(None); |
| 64 | + } |
| 65 | + |
| 66 | + calendar_name = Some(file_in_zip.name().to_string()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // handle missing calendar.txt |
| 71 | + let calendar_name = match calendar_name { |
| 72 | + Some(name) => name, |
| 73 | + None => { |
| 74 | + println!("No calendar.txt found in {:?}", zip_path); |
| 75 | + return Ok(None); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + // open calendar.txt as a streaming reader |
| 80 | + let file_in_zip = archive.by_name(&calendar_name)?; |
| 81 | + |
| 82 | + // create a CSV reader |
| 83 | + let mut rdr = ReaderBuilder::new() |
| 84 | + .has_headers(true) |
| 85 | + .from_reader(file_in_zip); |
| 86 | + |
| 87 | + // deserialize each row into Calendar |
| 88 | + let mut calendars = Vec::new(); |
| 89 | + |
| 90 | + for result in rdr.deserialize::<Calendar>() { |
| 91 | + let calendar = result.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; |
| 92 | + |
| 93 | + calendars.push(calendar); |
| 94 | + } |
| 95 | + |
| 96 | + Ok(Some(calendars)) |
| 97 | +} |
0 commit comments