Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RawTranslations #159

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fixtures/basic/translations.txt
Original file line number Diff line number Diff line change
@@ -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",,
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
Loading