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

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

reader.rs99 lines · 3.1 KiBRaw
1//! A host-side reader for the `.transit` (TRX2) packs [`crate::index`] writes.
2//!
3//! This is a **deliberate reimplementation** of the on-device reader
4//! `maps/src/main/rust/src/transit.rs`, not shared code. Two independent
5//! decoders of the same bytes must agree; keeping them separate is what makes a
6//! layout mistake in either one show up as a failing test or a dump diff instead
7//! of a pack the device silently mis-reads. Anything learned here must be
8//! mirrored there by hand — that is the point.
9//!
10//! Used by `index`'s tests and by `src/bin/transit_dump.rs`.
11
12use crate::index::{
13 HEADER_LEN, MAGIC, NONE, SECTION_COUNT, SECTION_COUNT_V6, VERSION, VERSION_MIN,
14};
15
16// Section slot numbers. Slot `i` must describe section `i`; the device reader
17// hardcodes the same numbers.
18pub const SEC_STRINGS: usize = 0;
19pub const SEC_STOPS: usize = 1;
20pub const SEC_ROUTES: usize = 2;
21pub const SEC_ROUTE_STOPS: usize = 3;
22pub const SEC_ROUTE_TRIPS: usize = 4;
23pub const SEC_PROFILES: usize = 5;
24pub const SEC_PROFILES_IDX: usize = 6;
25pub const SEC_STOP_ROUTES: usize = 7;
26pub const SEC_STOP_ROUTES_IDX: usize = 8;
27pub const SEC_TRANSFERS: usize = 9;
28pub const SEC_TRANSFERS_IDX: usize = 10;
29pub const SEC_SERVICES: usize = 11;
30pub const SEC_EXCEPTIONS: usize = 12;
31pub const SEC_GRID_CELL_IDS: usize = 13;
32pub const SEC_GRID_CELL_OFF: usize = 14;
33pub const SEC_GRID_STOPS: usize = 15;
34pub const SEC_FEEDS: usize = 16;
35pub const SEC_FEED_TZ: usize = 17;
36pub const SEC_EXCEPTIONS_IDX: usize = 18;
37pub const SEC_STOP_ROUTE_POS: usize = 19;
38pub const SEC_SHAPE_COORDS: usize = 20;
39pub const SEC_ROUTE_SHAPE_IDX: usize = 21;
40pub const SEC_ROUTE_STOP_SHAPE: usize = 22;
41pub const SEC_FEED_MOTIS_PREFIX: usize = 23;
42pub const SEC_STOP_GTFS_ID: usize = 24;
43pub const SEC_ROUTE_TRIP_RECS: usize = 25;
44pub const SEC_ROUTE_TRIP_OFF: usize = 26;
45pub const SEC_RAIL_LINES: usize = 27;
46
47/// Bytes per v6 trip record: `u32 start_time, profile_id, service_idx, headsign_off`.
48pub const TRIP_REC_BYTES: usize = 16;
49pub use crate::reader_extra::read_uvarint;
50use crate::reader_extra::{ri32, ru32, ru64, zigzag};
51
52/// One ROUTES entry (a 32-byte stride).
53#[derive(Clone, Copy, Debug)]
54pub struct RouteRec {
55 pub name_off: u32,
56 pub color: u32,
57 pub route_type: u32,
58 pub feed_idx: u32,
59 pub n_stops: u32,
60 pub first_route_stop: u32,
61 pub n_trips: u32,
62 /// Byte offset into ROUTE_TRIPS.
63 pub trips_off: u32,
64}
65
66/// One trip decoded out of a route's ROUTE_TRIPS varint block.
67#[derive(Clone, Copy, Debug)]
68pub struct TripRec {
69 pub start_time: u32,
70 pub profile_id: u32,
71 pub service_idx: u32,
72 pub headsign_off: u32,
73}
74
75/// One SERVICES entry (a 12-byte stride).
76#[derive(Clone, Copy, Debug)]
77pub struct ServiceRec {
78 /// Bit `b` set = runs on day `b`, Monday = 0.
79 pub weekday_mask: u8,
80 pub start_date: u32,
81 pub end_date: u32,
82}
83
84/// The header's grid parameters.
85#[derive(Clone, Copy, Debug)]
86pub struct GridParams {
87 pub lat0_e7: i32,
88 pub lon0_e7: i32,
89 pub cell_e7: u32,
90 pub cols: u32,
91 pub rows: u32,
92}
93
94pub struct Reader {
95 buf: Vec<u8>,
96 sec: Vec<(usize, usize)>,
97}
98
99include!("reader_s1.rs");