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

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

manifest.rs92 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(), PathBuf::from(dir), parts.next().unwrap_or("").trim().to_string())
25 }
26 _ => {
27 let dir = PathBuf::from(s);
28 let name = dir
29 .file_name()
30 .map(|n| n.to_string_lossy().into_owned())
31 .unwrap_or_else(|| s.to_string());
32 (name, dir, String::new())
33 }
34 }
35}
36
37/// Read a manifest of feed-spec lines (`#` comments / blanks ignored).
38pub fn read_manifest(path: &Path) -> Result<Vec<FeedSpec>, String> {
39 let text = std::fs::read_to_string(path)
40 .map_err(|e| format!("cannot read manifest {}: {e}", path.display()))?;
41 let mut out = Vec::new();
42 for line in text.lines() {
43 let line = line.trim();
44 if line.is_empty() || line.starts_with('#') {
45 continue;
46 }
47 out.push(parse_feed_spec(line));
48 }
49 Ok(out)
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn a_two_field_spec_carries_no_motis_prefix() {
58 // build_world_transit.ps1 emits these; its packs must keep building.
59 let (name, dir, prefix) = parse_feed_spec("sfmuni=/tmp/gtfs/sfmuni");
60 assert_eq!(name, "sfmuni");
61 assert_eq!(dir, PathBuf::from("/tmp/gtfs/sfmuni"));
62 assert_eq!(prefix, "");
63 }
64
65 #[test]
66 fn a_three_field_spec_carries_the_motis_prefix() {
67 let (name, dir, prefix) =
68 parse_feed_spec("sf_bayarea=/tmp/gtfs/sf_bayarea=us-ca-SF-bayarea");
69 assert_eq!(name, "sf_bayarea");
70 assert_eq!(dir, PathBuf::from("/tmp/gtfs/sf_bayarea"));
71 // The unmangled Transitous source name, which Get-SafeName destroys in
72 // the feed name beside it.
73 assert_eq!(prefix, "us-ca-SF-bayarea");
74 }
75
76 #[test]
77 fn a_bare_dir_names_itself() {
78 let (name, dir, prefix) = parse_feed_spec("/tmp/gtfs/sfmuni");
79 assert_eq!(name, "sfmuni");
80 assert_eq!(dir, PathBuf::from("/tmp/gtfs/sfmuni"));
81 assert_eq!(prefix, "");
82 }
83
84 #[test]
85 fn a_windows_dir_survives_the_split() {
86 // Drive letters use `:`, not `=`, so they are not split points.
87 let (name, dir, prefix) = parse_feed_spec(r"sf=C:\work\gtfs\sf=us-ca-SFMTA");
88 assert_eq!(name, "sf");
89 assert_eq!(dir, PathBuf::from(r"C:\work\gtfs\sf"));
90 assert_eq!(prefix, "us-ca-SFMTA");
91 }
92}