Moved GTFS struct back to structs, refresh works now.

This commit is contained in:
Nahuel Lofeudo 2026-05-02 08:16:13 +01:00
parent 4d272c31c5
commit ca0003edfd
4 changed files with 38 additions and 31 deletions

View File

@ -9,7 +9,7 @@ use std::{
use zip::ZipArchive;
use crate::gtfs::{
Gtfs,
structs::Gtfs,
utils::{route_ids_from_numbers, stop_ids_from_codes},
};

View File

@ -1,35 +1,15 @@
mod loader;
mod utils;
pub mod structs;
use chrono::{DateTime, Local};
use log::{debug, info, warn};
use sdl3::libc::stpncpy;
use chrono::{DateTime, Local, NaiveTime, Timelike};
use log::{debug};
use std::{
collections::{HashMap, HashSet},
fs::File,
};
use gtfs_structures::{Agency, Calendar, CalendarDate, Exception, RawStopTime, RawTrip, Route, Stop};
use gtfs_structures::{Exception, RawTrip};
use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Preferences}};
// The main GTFS struct. This is similar to (but not exactly) gtfs-structures::Gtfs because we don't need everything
#[derive(Debug)]
pub struct Gtfs {
/// All agencies. They can not be read by `agency_id`, as it is not a required field
pub agencies: Vec<Agency>,
/// All Calendar by `service_id`
pub calendar: HashMap<String, Calendar>,
/// All calendar dates grouped by service_id
pub calendar_dates: HashMap<String, Vec<CalendarDate>>,
/// All routes by `route_id`
pub routes: HashMap<String, Route>,
/// All stop by `stop_id`.
pub stops: HashMap<String, Stop>,
/// All trips by trip_id
pub trips: HashMap<String, RawTrip>,
/// Stop times for the chosen stops and the chosen routes
pub stop_times: HashMap<(String, u32), RawStopTime>,
}
use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Gtfs, Preferences}};
impl Gtfs {
@ -77,7 +57,12 @@ impl Gtfs {
// Finally, find the arrivals for the active trips on the chosen stops
let mut arrivals: Vec<Arrival> = Vec::new();
let current_timestamp = target_datetime.timestamp();
// Stop times are parsed as the number of seconds since midnight on the current day
let current_timestamp = target_datetime.time().hour() * 3600
+ target_datetime.time().minute() * 60
+ target_datetime.time().second();
for (_id, stop_time) in self.stop_times.iter() {
if trips.contains_key(&stop_time.trip_id) {
let stop_timestamp = stop_time.departure_time.or(stop_time.arrival_time).unwrap();
@ -86,7 +71,7 @@ impl Gtfs {
let arrival: Arrival = Arrival {
route: self.routes.get(&self.trips.get(&stop_time.trip_id).unwrap().route_id).unwrap(),
stop: self.stops.get(&stop_time.stop_id).unwrap(),
departure_time: stop_timestamp
departure_time: NaiveTime::from_num_seconds_from_midnight_opt(stop_timestamp, 0).unwrap()
};
arrivals.push(arrival);
}

View File

@ -1,6 +1,7 @@
use std::collections::{HashSet};
use std::collections::{HashMap, HashSet};
use gtfs_structures::{Route, Stop};
use chrono::NaiveTime;
use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
// This is to store the preferences for the GTFS(-R) side of the code.
pub struct Preferences {
@ -8,10 +9,31 @@ pub struct Preferences {
pub stop_codes: HashSet<String>,
}
// The main GTFS struct. This is similar to (but not exactly) gtfs-structures::Gtfs because we don't need everything
#[derive(Debug)]
pub struct Gtfs {
/// All agencies. They can not be read by `agency_id`, as it is not a required field
pub agencies: Vec<Agency>,
/// All Calendar by `service_id`
pub calendar: HashMap<String, Calendar>,
/// All calendar dates grouped by service_id
pub calendar_dates: HashMap<String, Vec<CalendarDate>>,
/// All routes by `route_id`
pub routes: HashMap<String, Route>,
/// All stop by `stop_id`.
pub stops: HashMap<String, Stop>,
/// All trips by trip_id
pub trips: HashMap<String, RawTrip>,
/// Stop times for the chosen stops and the chosen routes
pub stop_times: HashMap<(String, u32), RawStopTime>,
}
#[derive(Debug)]
pub struct Arrival<'a> {
pub route: &'a Route,
pub stop: &'a Stop,
pub departure_time: u32,
pub departure_time: NaiveTime,
}

View File

@ -5,7 +5,7 @@ use chrono::{DateTime, Local};
use sdl3::{event::Event, libc::statx_timestamp};
use log::{Level, Metadata, Record, debug, error, info, warn};
use crate::{gtfs::Gtfs, renderer::Screen};
use crate::{gtfs::structs::Gtfs, renderer::Screen};
const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip";