From 2154e62fe6ec89a030034b37ab8b974907a76cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristram=20Gr=C3=A4bener?= Date: Fri, 19 Jan 2024 17:24:28 +0100 Subject: [PATCH] Implement RawTranslations For now only in RawGtfs This is the first step before getting building more usable functions in Gtfs --- fixtures/basic/translations.txt | 3 +++ src/gtfs_reader.rs | 2 ++ src/objects.rs | 19 +++++++++++++++++++ src/raw_gtfs.rs | 6 ++++++ src/tests.rs | 13 ++++++++++++- 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 fixtures/basic/translations.txt diff --git a/fixtures/basic/translations.txt b/fixtures/basic/translations.txt new file mode 100644 index 0000000..6251593 --- /dev/null +++ b/fixtures/basic/translations.txt @@ -0,0 +1,3 @@ +table_name,field_name,language,translation,field_value,record_id,record_sub_id +stops,stop_name,nl,Stop Gebied,,stop1, +stops,stop_name,fr,Arrêt Région,"Stop Area",, \ No newline at end of file diff --git a/src/gtfs_reader.rs b/src/gtfs_reader.rs index f35f0fb..a5341ff 100644 --- a/src/gtfs_reader.rs +++ b/src/gtfs_reader.rs @@ -182,6 +182,7 @@ impl RawGtfsReader { pathways: self.read_objs_from_optional_path(p, "pathways.txt"), feed_info: self.read_objs_from_optional_path(p, "feed_info.txt"), read_duration: Utc::now().signed_duration_since(now).num_milliseconds(), + translations: self.read_objs_from_optional_path(p, "translations.txt"), files, source_format: crate::SourceFormat::Directory, sha256: None, @@ -309,6 +310,7 @@ impl RawGtfsReader { } else { Some(Ok(Vec::new())) }, + translations: self.read_optional_file(&file_mapping, &mut archive, "translations.txt"), read_duration: Utc::now().signed_duration_since(now).num_milliseconds(), files, source_format: crate::SourceFormat::Zip, diff --git a/src/objects.rs b/src/objects.rs index 00d59f7..2bda0cc 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -380,6 +380,25 @@ impl fmt::Display for Route { } } +/// Raw structure to hold translations as defined in the GTFS file. See +#[derive(Debug, Serialize, Deserialize, Default)] +pub struct RawTranslation { + /// To which table does the translation apply + pub table_name: String, + /// To which field does the translation apply + pub field_name: String, + /// Language of the translation + pub language: String, + /// Translated value + pub translation: String, + /// The record identifier to translate. For stop_times, it’s the trip_id + pub record_id: Option, + /// Only for stop_times: the stop_sequence + pub record_sub_id: Option, + /// Translate all values that match exactly, instead of specifying individual records + pub field_value: Option, +} + /// A [Trip] where the relationships with other objects have not been checked #[derive(Debug, Serialize, Deserialize, Default)] pub struct RawTrip { diff --git a/src/raw_gtfs.rs b/src/raw_gtfs.rs index 12e6695..ef78053 100644 --- a/src/raw_gtfs.rs +++ b/src/raw_gtfs.rs @@ -45,6 +45,8 @@ pub struct RawGtfs { pub source_format: SourceFormat, /// sha256 sum of the feed pub sha256: Option, + /// All translations, None if the file was absent as it is not mandatory + pub translations: Option, Error>>, } impl RawGtfs { @@ -66,6 +68,10 @@ impl RawGtfs { println!(" Transfers: {}", optional_file_summary(&self.transfers)); println!(" Pathways: {}", optional_file_summary(&self.pathways)); println!(" Feed info: {}", optional_file_summary(&self.feed_info)); + println!( + " Translations: {}", + optional_file_summary(&self.translations) + ); } /// Reads from an url (if starts with http), or a local path (either a directory or zipped file) diff --git a/src/tests.rs b/src/tests.rs index f5d9fbe..42f3b42 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -238,6 +238,17 @@ fn read_pathways() { assert_eq!(None, pathways[0].min_width); } +#[test] +fn read_translations() { + let gtfs = RawGtfs::from_path("fixtures/basic").expect("impossible to read gtfs"); + let translation = >fs.translations.unwrap().unwrap()[0]; + assert_eq!(translation.table_name, "stops"); + assert_eq!(translation.field_name, "stop_name"); + assert_eq!(translation.language, "nl"); + assert_eq!(translation.translation, "Stop Gebied"); + assert_eq!(translation.field_value, None); +} + #[test] fn read_feed_info() { let gtfs = Gtfs::from_path("fixtures/basic").expect("impossible to read gtfs"); @@ -342,7 +353,7 @@ fn display() { #[test] fn path_files() { let gtfs = RawGtfs::from_path("fixtures/basic").expect("impossible to read gtfs"); - assert_eq!(gtfs.files.len(), 13); + assert_eq!(gtfs.files.len(), 14); assert_eq!(gtfs.source_format, SourceFormat::Directory); assert!(gtfs.files.contains(&"agency.txt".to_owned())); }