Get rid of a bunch of unwrap()s

This commit is contained in:
2026-05-17 22:34:51 +01:00
parent 1ecf31b6fa
commit 59c13dc46a
3 changed files with 58 additions and 45 deletions
+11 -11
View File
@@ -5,8 +5,7 @@ pub mod structs;
use chrono::{DateTime, Local, NaiveTime, Timelike};
use log::{debug};
use std::{
collections::{HashMap, HashSet},
fs::File,
collections::{HashMap, HashSet}, fs::File, io::Error
};
use gtfs_structures::{Exception, RawTrip};
@@ -15,7 +14,7 @@ use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Gtfs, Preferences}};
impl Gtfs {
pub fn get_next_arrivals_for(&self, target_datetime: &DateTime<Local>) -> Box<Vec<Arrival<'_>>> {
pub fn get_next_arrivals_for(&self, target_datetime: &DateTime<Local>) -> Option<Vec<Arrival<'_>>> {
let naive_target = target_datetime.naive_local();
let target_date = naive_target.date();
@@ -66,13 +65,13 @@ impl Gtfs {
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();
let stop_timestamp = stop_time.departure_time.or(stop_time.arrival_time)?;
debug!("Stop timestamp {} current timestamp {}", stop_timestamp, current_timestamp);
if current_timestamp < stop_timestamp.into() {
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: NaiveTime::from_num_seconds_from_midnight_opt(stop_timestamp, 0).unwrap()
route: self.routes.get(&self.trips.get(&stop_time.trip_id)?.route_id)?,
stop: self.stops.get(&stop_time.stop_id)?,
departure_time: NaiveTime::from_num_seconds_from_midnight_opt(stop_timestamp, 0)?
};
arrivals.push(arrival);
}
@@ -80,14 +79,15 @@ impl Gtfs {
}
debug!("Found {} arrivals", arrivals.len());
return Box::from(arrivals);
return Some(arrivals);
}
/// Load a GTFS structure from a zip file
pub fn load(src_file: &str, prefs: &Preferences) -> Gtfs {
pub fn load(src_file: &str, prefs: &Preferences) -> Result<Gtfs, Error> {
// Open zip file
let mut zip_reader = zip::ZipArchive::new(File::open(src_file).unwrap()).unwrap();
let zip_file = File::open(src_file)?;
let mut zip_reader = zip::ZipArchive::new(zip_file)?;
let mut gtfs: Gtfs = Gtfs {
agencies: Vec::new(),
@@ -102,6 +102,6 @@ impl Gtfs {
load_gtfs(&mut gtfs, &mut zip_reader, &prefs.route_numbers, &prefs.stop_codes);
return gtfs;
return Ok(gtfs);
}
}