Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / scripts / maps / gtfs_ingest / src / manifest.rs

manifest.rs94 lines · 3.4 KiBRaw
1//! Feed-spec parsing shared by the crate's binaries.
2//!
3//! A spec names one GTFS feed and, optionally, the Transitous id namespace its
4//! stops live in. `gtfs_ingest` bakes that namespace into the `.transit` pack and
5//! `transit_stops` writes it into the basemap layer, so both read it the same way.
6
7use std::path::{Path, PathBuf};
8
9/// One feed: `(feed_name, gtfs_dir, motis_prefix)`. The prefix is empty when the
10/// caller does not know it.
11pub type FeedSpec = (String, PathBuf, String);
12
13/// Parse `feed_name=dir`, `feed_name=dir=motis_prefix`, or a bare `dir` (name then
14/// defaults to the dir's base name, with no prefix).
15///
16/// The third field is the feed's Transitous id namespace, which composes into a
17/// MOTIS stop id (`<prefix>_<gtfs stop_id>`). Two-field specs stay valid, so
18/// `build_world_transit.ps1` is unaffected and its packs simply carry no ids.
19/// Fields split on `=`, so neither the directory nor the prefix may contain one.
20pub fn parse_feed_spec(s: &str) -> FeedSpec {
21 let mut parts = s.splitn(3, '=');
22 match (parts.next(), parts.next()) {
23 (Some(name), Some(dir)) => (
24 name.to_string(),
25 PathBuf::from(dir),
26 parts.next().unwrap_or("").trim().to_string(),
27 ),
28 _ => {
29 let dir = PathBuf::from(s);
30 let name = dir
31 .file_name()
32 .map(|n| n.to_string_lossy().into_owned())
33 .unwrap_or_else(|| s.to_string());
34 (name, dir, String::new())
35 }
36 }
37}
38
39/// Read a manifest of feed-spec lines (`#` comments / blanks ignored).
40pub fn read_manifest(path: &Path) -> Result<Vec<FeedSpec>, String> {
41 let text = std::fs::read_to_string(path)
42 .map_err(|e| format!("cannot read manifest {}: {e}", path.display()))?;
43 let mut out = Vec::new();
44 for line in text.lines() {
45 let line = line.trim();
46 if line.is_empty() || line.starts_with('#') {
47 continue;
48 }
49 out.push(parse_feed_spec(line));
50 }
51 Ok(out)
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn a_two_field_spec_carries_no_motis_prefix() {
60 // build_world_transit.ps1 emits these; its packs must keep building.
61 let (name, dir, prefix) = parse_feed_spec("sfmuni=/tmp/gtfs/sfmuni");
62 assert_eq!(name, "sfmuni");
63 assert_eq!(dir, PathBuf::from("/tmp/gtfs/sfmuni"));
64 assert_eq!(prefix, "");
65 }
66
67 #[test]
68 fn a_three_field_spec_carries_the_motis_prefix() {
69 let (name, dir, prefix) =
70 parse_feed_spec("sf_bayarea=/tmp/gtfs/sf_bayarea=us-ca-SF-bayarea");
71 assert_eq!(name, "sf_bayarea");
72 assert_eq!(dir, PathBuf::from("/tmp/gtfs/sf_bayarea"));
73 // The unmangled Transitous source name, which Get-SafeName destroys in
74 // the feed name beside it.
75 assert_eq!(prefix, "us-ca-SF-bayarea");
76 }
77
78 #[test]
79 fn a_bare_dir_names_itself() {
80 let (name, dir, prefix) = parse_feed_spec("/tmp/gtfs/sfmuni");
81 assert_eq!(name, "sfmuni");
82 assert_eq!(dir, PathBuf::from("/tmp/gtfs/sfmuni"));
83 assert_eq!(prefix, "");
84 }
85
86 #[test]
87 fn a_windows_dir_survives_the_split() {
88 // Drive letters use `:`, not `=`, so they are not split points.
89 let (name, dir, prefix) = parse_feed_spec(r"sf=C:\work\gtfs\sf=us-ca-SFMTA");
90 assert_eq!(name, "sf");
91 assert_eq!(dir, PathBuf::from(r"C:\work\gtfs\sf"));
92 assert_eq!(prefix, "us-ca-SFMTA");
93 }
94}