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
+33 -26
View File
@@ -1,6 +1,6 @@
use log::debug;
use gtfs_structures::{Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
use serde::de::DeserializeOwned;
use serde::de::{DeserializeOwned, value::MapAccessDeserializer};
use std::{
collections::{HashMap, HashSet},
fs::File,
@@ -14,14 +14,14 @@ use crate::gtfs::{
};
trait Filter<T> {
fn accept(&self, v: &T) -> bool;
fn accept(&self, v: &T) -> Option<bool>;
}
// No filter on loaded records
struct LoadAll {}
impl<T> Filter<T> for LoadAll {
fn accept(&self, _: &T) -> bool {
return true;
fn accept(&self, _: &T) -> Option<bool> {
return Some(true);
}
}
@@ -29,9 +29,9 @@ struct LoadRoutes<'a> {
routes: &'a HashSet<String>,
}
impl Filter<Route> for LoadRoutes<'_> {
fn accept(&self, r: &Route) -> bool {
fn accept(&self, r: &Route) -> Option<bool> {
let short_name = &r.short_name;
return short_name.is_some() && self.routes.contains(short_name.as_ref().unwrap());
return Some(short_name.is_some() && self.routes.contains(short_name.as_ref()?));
}
}
@@ -39,9 +39,9 @@ struct LoadStops<'a> {
stops: &'a HashSet<String>,
}
impl Filter<Stop> for LoadStops<'_> {
fn accept(&self, s: &Stop) -> bool {
fn accept(&self, s: &Stop) -> Option<bool> {
let stop_code = &s.code;
return stop_code.is_some() && self.stops.contains(s.code.as_ref().unwrap());
return Some(stop_code.is_some() && self.stops.contains(s.code.as_ref()?));
}
}
@@ -49,9 +49,9 @@ struct LoadTrips<'a> {
route_ids: &'a HashSet<String>,
}
impl Filter<RawTrip> for LoadTrips<'_> {
fn accept(&self, t: &RawTrip) -> bool {
fn accept(&self, t: &RawTrip) -> Option<bool> {
let route_id = &t.route_id;
return self.route_ids.contains(route_id);
return Some(self.route_ids.contains(route_id));
}
}
@@ -60,8 +60,8 @@ struct LoadStopTimes<'a> {
stop_ids: &'a HashSet<String>,
}
impl Filter<RawStopTime> for LoadStopTimes<'_> {
fn accept(&self, st: &RawStopTime) -> bool {
return self.stop_ids.contains(&st.stop_id) && self.trip_ids.contains(&st.trip_id);
fn accept(&self, st: &RawStopTime) -> Option<bool> {
return Some(self.stop_ids.contains(&st.stop_id) && self.trip_ids.contains(&st.trip_id));
}
}
@@ -70,14 +70,15 @@ fn load_vector<T: serde::de::DeserializeOwned>(
destination: &mut Vec<T>,
zip_reader: &mut ZipArchive<File>,
table_name: &str,
) {
let file_reader = zip_reader.by_name(table_name).unwrap();
let mut rdr = csv::Reader::from_reader(file_reader);
) -> Option<bool> {
let file_reader = zip_reader.by_name(table_name);
let mut rdr = csv::Reader::from_reader(file_reader.ok()?);
for row in rdr.deserialize() {
let record: T = row.unwrap();
let record: T = row.ok()?;
destination.push(record);
}
return Some(true);
}
// Loads a HashMap of the selected type, using the provided index function as the key
@@ -87,27 +88,30 @@ fn load_map<K, V, IndexFn, FilterT>(
table_name: &str,
index: IndexFn,
filter: FilterT,
) where
) -> Option<bool>
where
K: Eq + Hash,
V: DeserializeOwned,
IndexFn: Fn(&V) -> K,
FilterT: Filter<V>,
FilterT: Filter<V>
{
let file_reader = zip_reader.by_name(table_name).unwrap();
let file_reader = (zip_reader.by_name(table_name)).ok()?;
let mut rdr = csv::Reader::from_reader(file_reader);
for row in rdr.deserialize() {
if row.is_ok() {
let record: V = row.unwrap();
if filter.accept(&record) {
let record: V = row.ok()?;
let accepted = filter.accept(&record);
if accepted.is_some() && accepted? {
let idx: K = index(&record);
destination.insert(idx, record);
}
} else {
print!("Row failed to deserialize row {:#?}", row.err());
panic!();
return None;
}
}
return Some(true);
}
// Loads a HashMap of a vector of the selected type, using the provided index function as the key
@@ -118,22 +122,25 @@ fn load_vector_map<'a, K, V, IndexFn, FilterT>(
table_name: &str,
index: IndexFn,
filter: FilterT,
) where
) -> Option<bool>
where
K: Eq + Hash,
V: DeserializeOwned,
IndexFn: Fn(&V) -> K,
FilterT: Filter<V>,
{
let file_reader = zip_reader.by_name(table_name).unwrap();
let file_reader = zip_reader.by_name(table_name).ok()?;
let mut rdr = csv::Reader::from_reader(file_reader);
for row in rdr.deserialize() {
let record: V = row.unwrap();
if filter.accept(&record) {
let record: V = row.ok()?;
let accepted = filter.accept(&record);
if accepted.is_some() && accepted? {
let idx = index(&record);
destination.entry(idx).or_insert_with(Vec::new).push(record);
}
}
return Some(true)
}
pub fn load_gtfs(