Skip to content
Snippets Groups Projects
Commit 2754f752 authored by Florian Burgener's avatar Florian Burgener
Browse files

Added UMSTEIGZ parser

parent 4b009576
No related branches found
No related tags found
No related merge requests found
......@@ -5,8 +5,8 @@ use serde::{Deserialize, Serialize};
use crate::{
models::{
AdministrationTransferTime, Attribute, BitField, Direction, Holiday, InformationText,
Journey, JourneyPlatform, Line, LineTransferTime, Platform, Stop, StopConnection,
ThroughService, TimetableMetadataEntry, TransportCompany, TransportType,
Journey, JourneyPlatform, JourneyTransferTime, Line, LineTransferTime, Platform, Stop,
StopConnection, ThroughService, TimetableMetadataEntry, TransportCompany, TransportType,
},
parsing,
storage::SimpleResourceStorage,
......@@ -40,6 +40,7 @@ pub struct Hrdf {
// Transfer times.
administration_transfer_times: SimpleResourceStorage<AdministrationTransferTime>,
journey_transfer_times: SimpleResourceStorage<JourneyTransferTime>,
line_transfer_times: SimpleResourceStorage<LineTransferTime>,
}
......@@ -74,6 +75,8 @@ impl Hrdf {
// Transfer times.
let administration_transfer_times = parsing::load_administration_transfer_times()?;
let journey_transfer_times =
parsing::load_journey_transfer_times(&journeys_legacy_pk_index)?;
let line_transfer_times =
parsing::load_line_transfer_times(&transport_types_legacy_pk_index)?;
......@@ -99,6 +102,7 @@ impl Hrdf {
through_service,
// Transfer times.
administration_transfer_times,
journey_transfer_times,
line_transfer_times,
});
......
......@@ -621,6 +621,76 @@ impl JourneyPlatform {
}
}
// ------------------------------------------------------------------------------------------------
// --- JourneyTransferTime
// ------------------------------------------------------------------------------------------------
#[derive(Debug, Serialize, Deserialize)]
pub struct JourneyTransferTime {
id: i32,
stop_id: i32,
journey_id_1: i32,
journey_id_2: i32,
duration: i16, // Transfer time from journey 1 to journey 2 is in minutes.
is_guaranteed: bool,
bit_field_id: Option<i32>,
}
impl Model<JourneyTransferTime> for JourneyTransferTime {
type K = i32;
fn id(&self) -> Self::K {
self.id
}
}
#[allow(unused)]
impl JourneyTransferTime {
pub fn new(
id: i32,
stop_id: i32,
journey_id_1: i32,
journey_id_2: i32,
duration: i16,
is_guaranteed: bool,
bit_field_id: Option<i32>,
) -> Self {
Self {
id,
stop_id,
journey_id_1,
journey_id_2,
duration,
is_guaranteed,
bit_field_id,
}
}
pub fn stop_id(&self) -> i32 {
self.stop_id
}
pub fn journey_id_1(&self) -> i32 {
self.journey_id_1
}
pub fn journey_id_2(&self) -> i32 {
self.journey_id_2
}
pub fn duration(&self) -> i16 {
self.duration
}
pub fn is_guaranteed(&self) -> bool {
self.is_guaranteed
}
pub fn bit_field_id(&self) -> &Option<i32> {
&self.bit_field_id
}
}
// ------------------------------------------------------------------------------------------------
// --- Language
// ------------------------------------------------------------------------------------------------
......
......@@ -5,6 +5,7 @@ mod direction_parser;
mod holiday_parser;
mod information_text_parser;
mod journey_parser;
mod journey_transfer_parser;
mod line_parser;
mod line_transfer_parser;
mod platform_parser;
......@@ -22,6 +23,7 @@ pub use direction_parser::parse as load_directions;
pub use holiday_parser::parse as load_holidays;
pub use information_text_parser::parse as load_information_texts;
pub use journey_parser::parse as load_journeys;
pub use journey_transfer_parser::parse as load_journey_transfer_times;
pub use line_parser::parse as load_lines;
pub use line_transfer_parser::parse as load_line_transfer_times;
pub use platform_parser::parse as load_platforms;
......
// 1 file(s).
// File(s) read by the parser:
// UMSTEIGZ
use std::{error::Error, rc::Rc};
use crate::{
models::{AutoIncrement, Journey, JourneyTransferTime, Model, ResourceIndex},
parsing::{ColumnDefinition, ExpectedType, FileParser, RowDefinition, RowParser},
storage::SimpleResourceStorage,
};
use super::ParsedValue;
pub fn parse(
journeys_legacy_pk_index: &ResourceIndex<Journey, (i32, String)>,
) -> Result<SimpleResourceStorage<JourneyTransferTime>, Box<dyn Error>> {
println!("Parsing UMSTEIGZ...");
#[rustfmt::skip]
let row_parser = RowParser::new(vec![
// This row is used to create a JourneyTransferTime instance.
RowDefinition::from(vec![
ColumnDefinition::new(1, 7, ExpectedType::Integer32),
ColumnDefinition::new(9, 14, ExpectedType::Integer32),
ColumnDefinition::new(16, 21, ExpectedType::String),
ColumnDefinition::new(23, 28, ExpectedType::Integer32),
ColumnDefinition::new(30, 35, ExpectedType::String),
ColumnDefinition::new(37, 39, ExpectedType::Integer16),
ColumnDefinition::new(40, 40, ExpectedType::String),
ColumnDefinition::new(42, 47, ExpectedType::OptionInteger32),
]),
]);
let parser = FileParser::new("data/UMSTEIGZ", row_parser)?;
let auto_increment = AutoIncrement::new();
let rows = parser
.parse()
.filter_map(|(_, _, values)| {
Some(create_instance(
values,
&auto_increment,
journeys_legacy_pk_index,
))
})
.collect();
Ok(SimpleResourceStorage::new(rows))
}
// ------------------------------------------------------------------------------------------------
// --- Data Processing Functions
// ------------------------------------------------------------------------------------------------
fn create_instance(
mut values: Vec<ParsedValue>,
auto_increment: &AutoIncrement,
journeys_legacy_pk_index: &ResourceIndex<Journey, (i32, String)>,
) -> Rc<JourneyTransferTime> {
let stop_id: i32 = values.remove(0).into();
let journey_id_1: i32 = values.remove(0).into();
let administration_1: String = values.remove(0).into();
let journey_id_2: i32 = values.remove(0).into();
let administration_2: String = values.remove(0).into();
let duration: i16 = values.remove(0).into();
let is_guaranteed: String = values.remove(0).into();
let bit_field_id: Option<i32> = values.remove(0).into();
let journey_id_1 = journeys_legacy_pk_index
.get(&(journey_id_1, administration_1))
.unwrap()
.id();
let journey_id_2 = journeys_legacy_pk_index
.get(&(journey_id_2, administration_2))
.unwrap()
.id();
let is_guaranteed = is_guaranteed == "!";
Rc::new(JourneyTransferTime::new(
auto_increment.next(),
stop_id,
journey_id_1,
journey_id_2,
duration,
is_guaranteed,
bit_field_id,
))
}
......@@ -39,19 +39,19 @@ pub fn parse() -> Result<SimpleResourceStorage<TimetableMetadataEntry>, Box<dyn
let start_date: String = data.remove(0).into();
let end_date: String = data.remove(0).into();
let metadata: String = data.remove(0).into();
let other_data: String = data.remove(0).into();
let start_date = NaiveDate::parse_from_str(&start_date, "%d.%m.%Y")?;
let end_date = NaiveDate::parse_from_str(&end_date, "%d.%m.%Y")?;
let metadata: Vec<String> = metadata.split('$').map(String::from).collect();
let other_data: Vec<String> = other_data.split('$').map(String::from).collect();
let rows = vec![
("start_date", start_date.to_string()),
("end_date", end_date.to_string()),
("name", metadata[0].to_owned()),
("created_at", metadata[1].to_owned()),
("version", metadata[2].to_owned()),
("provider", metadata[3].to_owned()),
("name", other_data[0].to_owned()),
("created_at", other_data[1].to_owned()),
("version", other_data[2].to_owned()),
("provider", other_data[3].to_owned()),
];
let auto_increment = AutoIncrement::new();
......@@ -59,13 +59,11 @@ pub fn parse() -> Result<SimpleResourceStorage<TimetableMetadataEntry>, Box<dyn
let rows = rows
.iter()
.map(|(key, value)| {
let instance = Rc::new(TimetableMetadataEntry::new(
Rc::new(TimetableMetadataEntry::new(
auto_increment.next(),
key.to_string(),
value.to_owned(),
));
instance
))
})
.collect();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment