hrdf-parser 0.9.3

This library is dedicated to the parsing of the HRDF format. For the moment, it can only parse the Swiss version of the HRDF format.
Documentation
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::path::Path;

/// # Through Service parser
///
/// - List of ride pairs that form a contiguous run. Travellers can remain seated.
///
/// - This construct is used, among other things, for the formation of the wing trains. File contains:
///     - Journey no. 1
///     - TU code 1
///     - Last stop 1
///     - Journey no. 2
///     - TU code 2
///     - Traffic days (see BITFELD file)
///     - First stop 2
///
/// ## Example (excerpt):
///
/// `
/// ...
/// 000001 000871 8576671 024064 000871 000010 8576671 % Fahrt 1, TU 871, letzte HS 8576671, Fahrt 24064, TU 871, Bitfeld 10, erste HS 8576671
/// 000001 000882 8581701 000041 000882 063787 8581701 % ...
/// 000002 000181 8530625 000003 000181 000000 8530625 % Fahrt 2, TU 181, letzte HS 8530625, Fahrt 3,     TU 181, Bitfeld 0,  erste HS 8530625
/// 000002 000194 8503674 000004 000194 000001 8503674 % ...
/// 000002 000812 8591817 000003 000812 000000 8591817 % ...
/// 000002 000882 8581701 000042 000882 063786 8581701 % ...
/// 000003 000181 8530625 000004 000181 000000 8530625 % Fahrt 3, TU 181, letzte HS 8530625, Fahrt 4, TU 181, Bitfeld 0, erste HS 8530625
/// 000003 000801 8507230 000004 000801 000000 8507230 % ... % Rivera, Passo del Ceneri
/// 000003 000812 8591817 000004 000812 000000 8591817 % ...
/// ...
/// `
///
/// 1 file(s).
/// File(s) read by the parser:
/// DURCHBI
use nom::{IResult, Parser, character::char, combinator::map, sequence::preceded};
use rustc_hash::{FxHashMap, FxHashSet};

use crate::{
    JourneyId,
    error::{HResult, HrdfError},
    models::{Model, ThroughService},
    parsing::{
        error::PResult,
        helpers::{i32_from_n_digits_parser, read_lines, string_from_n_chars_parser},
    },
    storage::ResourceStorage,
    utils::AutoIncrement,
};

enum ThroughServiceLine {
    ThroughService {
        journey_1_id: i32,
        journey_1_administration: String,
        journey_1_stop_id: i32,
        journey_2_id: i32,
        journey_2_administration: String,
        bit_field_id: i32,
        journey_2_stop_id: i32,
    },
}

fn through_service_combinator(input: &str) -> IResult<&str, ThroughServiceLine> {
    map(
        (
            i32_from_n_digits_parser(6),
            preceded(char(' '), string_from_n_chars_parser(6)),
            preceded(char(' '), i32_from_n_digits_parser(7)),
            preceded(char(' '), i32_from_n_digits_parser(6)),
            preceded(char(' '), string_from_n_chars_parser(6)),
            preceded(char(' '), i32_from_n_digits_parser(6)), // Should be INT16 according to the standard. The standard contains an error. The correct type is INT32.
            preceded(char(' '), i32_from_n_digits_parser(7)), // No indication
                                                              // this should be optional
        ),
        |(
            journey_1_id,
            journey_1_administration,
            journey_1_stop_id,
            journey_2_id,
            journey_2_administration,
            bit_field_id,
            journey_2_stop_id,
        )| ThroughServiceLine::ThroughService {
            journey_1_id,
            journey_1_administration,
            journey_1_stop_id,
            journey_2_id,
            journey_2_administration,
            bit_field_id,
            journey_2_stop_id,
        },
    )
    .parse(input)
}

fn parse_line(
    line: &str,
    data: &mut FxHashMap<i32, ThroughService>,
    journeys_pk_type_converter: &FxHashSet<JourneyId>,
    auto_increment: &AutoIncrement,
) -> PResult<()> {
    let (_, through_service_line) = through_service_combinator(line)?;

    match through_service_line {
        ThroughServiceLine::ThroughService {
            journey_1_id,
            journey_1_administration,
            journey_1_stop_id,
            journey_2_id,
            journey_2_administration,
            bit_field_id,
            journey_2_stop_id,
        } => {
            let journey_1 =
                journeys_pk_type_converter.get(&(journey_1_id, journey_1_administration.clone()));
            if journey_1.is_none() {
                log::warn!(
                    "Unknown legacy ID for journey_1: {journey_1_id}, {journey_1_administration}"
                );
            }

            let journey_2 =
                journeys_pk_type_converter.get(&(journey_2_id, journey_2_administration.clone()));
            if journey_2.is_none() {
                log::warn!(
                    "Unknown legacy ID for journey_2: {journey_2_id}, {journey_2_administration}"
                );
            }

            if journey_1_stop_id != journey_2_stop_id {
                log::warn!(
                    "Journey 1 last stop does not match journey 2 first stop: {journey_1_stop_id}, {journey_2_stop_id}"
                );
            }

            let ts = ThroughService::new(
                auto_increment.next(),
                (journey_1_id, journey_1_administration),
                journey_1_stop_id,
                (journey_2_id, journey_2_administration),
                journey_2_stop_id,
                bit_field_id,
            );
            data.insert(ts.id(), ts);
        }
    }
    Ok(())
}

pub fn parse(
    path: &Path,
    journeys_pk_type_converter: &FxHashSet<JourneyId>,
) -> HResult<ResourceStorage<ThroughService>> {
    log::info!("Parsing DURCHBI...");
    let auto_increment = AutoIncrement::new();
    let mut through_services = FxHashMap::default();

    let file = path.join("DURCHBI");
    let through_service_lines = read_lines(&file, 0)?;
    through_service_lines
        .into_iter()
        .enumerate()
        .filter(|(_, line)| !line.trim().is_empty())
        .try_for_each(|(line_number, line)| {
            parse_line(
                &line,
                &mut through_services,
                journeys_pk_type_converter,
                &auto_increment,
            )
            .map_err(|e| HrdfError::Parsing {
                error: e,
                file: String::from(file.to_string_lossy()),
                line,
                line_number,
            })
        })?;
    Ok(ResourceStorage::new(through_services))
}

#[cfg(test)]
mod tests {
    use crate::parsing::tests::get_json_values;

    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn test_through_service_combinator_basic() {
        let input = "000001 000871 8576671 024064 000871 000010 8576671";
        let result = through_service_combinator(input);
        assert!(result.is_ok());
        let (_, ts_line) = result.unwrap();
        match ts_line {
            ThroughServiceLine::ThroughService {
                journey_1_id,
                journey_1_administration,
                journey_1_stop_id,
                journey_2_id,
                journey_2_administration,
                bit_field_id,
                journey_2_stop_id,
            } => {
                assert_eq!(journey_1_id, 1);
                assert_eq!(journey_1_administration, "000871");
                assert_eq!(journey_1_stop_id, 8576671);
                assert_eq!(journey_2_id, 24064);
                assert_eq!(journey_2_administration, "000871");
                assert_eq!(bit_field_id, 10);
                assert_eq!(journey_2_stop_id, 8576671);
            }
        }
    }

    #[test]
    fn test_through_service_combinator_zero_bitfield() {
        let input = "000002 000181 8530625 000003 000181 000000 8530625";
        let result = through_service_combinator(input);
        assert!(result.is_ok());
        let (_, ts_line) = result.unwrap();
        match ts_line {
            ThroughServiceLine::ThroughService {
                journey_1_id,
                journey_1_administration,
                journey_1_stop_id,
                journey_2_id,
                journey_2_administration,
                bit_field_id,
                journey_2_stop_id,
            } => {
                assert_eq!(journey_1_id, 2);
                assert_eq!(journey_1_administration, "000181");
                assert_eq!(journey_1_stop_id, 8530625);
                assert_eq!(journey_2_id, 3);
                assert_eq!(journey_2_administration, "000181");
                assert_eq!(bit_field_id, 0);
                assert_eq!(journey_2_stop_id, 8530625);
            }
        }
    }

    #[test]
    fn test_through_service_combinator_different_stops() {
        let input = "000002 000194 8503674 000004 000194 000001 8503674";
        let result = through_service_combinator(input);
        assert!(result.is_ok());
        let (_, ts_line) = result.unwrap();
        match ts_line {
            ThroughServiceLine::ThroughService {
                journey_1_id,
                journey_2_id,
                bit_field_id,
                ..
            } => {
                assert_eq!(journey_1_id, 2);
                assert_eq!(journey_2_id, 4);
                assert_eq!(bit_field_id, 1);
            }
        }
    }

    #[test]
    fn test_through_service_combinator_large_bitfield() {
        let input = "000001 000882 8581701 000041 000882 063787 8581701";
        let result = through_service_combinator(input);
        assert!(result.is_ok());
        let (_, ts_line) = result.unwrap();
        match ts_line {
            ThroughServiceLine::ThroughService {
                journey_1_id,
                journey_2_id,
                bit_field_id,
                ..
            } => {
                assert_eq!(journey_1_id, 1);
                assert_eq!(journey_2_id, 41);
                assert_eq!(bit_field_id, 63787);
            }
        }
    }

    #[test]
    fn test_parse_line_creates_through_service() {
        let mut data = FxHashMap::default();
        let mut journeys = FxHashSet::default();
        journeys.insert((1, "000871".to_string()));
        journeys.insert((24064, "000871".to_string()));
        let auto_increment = AutoIncrement::new();

        parse_line(
            "000001 000871 8576671 024064 000871 000010 8576671",
            &mut data,
            &journeys,
            &auto_increment,
        )
        .unwrap();

        assert_eq!(data.len(), 1);
        let ts = data.get(&1).unwrap();

        let reference = r#"{
            "id":1,
            "journey_1_id":[1,"000871"],
            "journey_1_stop_id":8576671,
            "journey_2_id":[24064,"000871"],
            "journey_2_stop_id":8576671,
            "bit_field_id":10
        }"#;
        let (ts, reference) = get_json_values(ts, reference).unwrap();
        assert_eq!(ts, reference);
    }

    #[test]
    fn test_parse_line_missing_journey_logs_warning() {
        let mut data = FxHashMap::default();
        let journeys = FxHashSet::default(); // Empty - journeys not found
        let auto_increment = AutoIncrement::new();

        // Should still succeed but log warnings
        parse_line(
            "000001 000871 8576671 024064 000871 000010 8576671",
            &mut data,
            &journeys,
            &auto_increment,
        )
        .unwrap();

        // Still creates the through service despite missing journeys
        assert_eq!(data.len(), 1);
        let ts = data.get(&1).unwrap();
        let reference = r#"{
            "id":1,
            "journey_1_id":[1,"000871"],
            "journey_1_stop_id":8576671,
            "journey_2_id":[24064,"000871"],
            "journey_2_stop_id":8576671,
            "bit_field_id":10
        }"#;
        let (ts, reference) = get_json_values(ts, reference).unwrap();
        assert_eq!(ts, reference);
    }

    #[test]
    fn test_parse_line_multiple_through_services() {
        let mut data = FxHashMap::default();
        let mut journeys = FxHashSet::default();
        journeys.insert((1, "000871".to_string()));
        journeys.insert((24064, "000871".to_string()));
        journeys.insert((2, "000181".to_string()));
        journeys.insert((3, "000181".to_string()));
        let auto_increment = AutoIncrement::new();

        parse_line(
            "000001 000871 8576671 024064 000871 000010 8576671",
            &mut data,
            &journeys,
            &auto_increment,
        )
        .unwrap();

        parse_line(
            "000002 000181 8530625 000003 000181 000000 8530625",
            &mut data,
            &journeys,
            &auto_increment,
        )
        .unwrap();

        assert_eq!(data.len(), 2);
        let ts = data.get(&1).unwrap();
        let reference = r#"{
            "id":1,
            "journey_1_id":[1,"000871"],
            "journey_1_stop_id":8576671,
            "journey_2_id":[24064,"000871"],
            "journey_2_stop_id":8576671,
            "bit_field_id":10
        }"#;
        let (ts, reference) = get_json_values(ts, reference).unwrap();
        assert_eq!(ts, reference);
        let ts = data.get(&2).unwrap();
        let reference = r#"{
            "id":2,
            "journey_1_id":[2,"000181"],
            "journey_1_stop_id":8530625,
            "journey_2_id":[3,"000181"],
            "journey_2_stop_id":8530625,
            "bit_field_id":0
        }"#;
        let (ts, reference) = get_json_values(ts, reference).unwrap();
        assert_eq!(ts, reference);
    }

    #[test]
    fn test_parse_line_matching_stops() {
        let mut data = FxHashMap::default();
        let journeys = FxHashSet::default();
        let auto_increment = AutoIncrement::new();

        // Same stop ID for journey 1 last stop and journey 2 first stop
        parse_line(
            "000002 000181 8530625 000003 000181 000000 8530625",
            &mut data,
            &journeys,
            &auto_increment,
        )
        .unwrap();

        let reference = r#"{
            "id":1,
            "journey_1_id":[2,"000181"],
            "journey_1_stop_id":8530625,
            "journey_2_id":[3,"000181"],
            "journey_2_stop_id":8530625,
            "bit_field_id":0
        }"#;
        let ts = data.get(&1).unwrap();
        // Both stops should be the same
        assert_eq!(ts.journey_1_stop_id(), 8530625);
        assert_eq!(ts.journey_2_stop_id(), 8530625);
        // Check the rest as well
        let (ts, reference) = get_json_values(ts, reference).unwrap();
        assert_eq!(ts, reference);
    }
}