Skip to content

Commit

Permalink
Implement RawTranslations
Browse files Browse the repository at this point in the history
For now only in RawGtfs

This is the first step before getting building
more usable functions in Gtfs
  • Loading branch information
Tristramg committed Feb 23, 2024
1 parent 9d3169a commit f80c0d5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/gtfs_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,25 @@ impl fmt::Display for Route {
}
}

/// Raw structure to hold translations as defined in the GTFS file. See <https://gtfs.org/schedule/reference/#translationstxt>
#[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<String>,
/// Only for stop_times: the stop_sequence
pub record_sub_id: Option<String>,
/// Translate all values that match exactly, instead of specifying individual records
pub field_value: Option<String>,
}

/// A [Trip] where the relationships with other objects have not been checked
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct RawTrip {
Expand Down
6 changes: 6 additions & 0 deletions src/raw_gtfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub struct RawGtfs {
pub source_format: SourceFormat,
/// sha256 sum of the feed
pub sha256: Option<String>,
/// All translations, None if the file was absent as it is not mandatory
pub translations: Option<Result<Vec<RawTranslation>, Error>>,
}

impl RawGtfs {
Expand All @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = &gtfs.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");
Expand Down Expand Up @@ -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()));
}
Expand Down

0 comments on commit f80c0d5

Please sign in to comment.