Refresh the GTFS bundle every time the program starts.
This commit is contained in:
@@ -1,6 +1,33 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
|
||||
use log::error;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use zip::result::ZipError;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
pub(crate) message: String,
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(value: std::io::Error) -> Self {
|
||||
return Error { message: value.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for Error {
|
||||
fn from(value: reqwest::Error) -> Self {
|
||||
return Error { message: value.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ZipError> for Error {
|
||||
fn from(value: ZipError) -> Self {
|
||||
return Error { message: value.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// This is to store the preferences for the GTFS(-R) side of the code.
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
@@ -27,6 +54,23 @@ pub struct Preferences {
|
||||
pub refresh_seconds: u64,
|
||||
}
|
||||
|
||||
// Utility functions
|
||||
impl Preferences {
|
||||
pub fn gtfs_file_name(&self) -> Result<String, Error> {
|
||||
let file_name_part = self.gtfs_url.split("/").last();
|
||||
if file_name_part.is_none() {
|
||||
error!("The config for gtfs-url is not a valid URL: {}", self.gtfs_url);
|
||||
return Err(Error {message: String::from("Failed to refresh GTFS data")});
|
||||
}
|
||||
return Ok(String::from(file_name_part.unwrap()));
|
||||
}
|
||||
|
||||
pub fn gtfs_file_path(&self) -> Result<String, Error> {
|
||||
return Ok(format!("{}/{}", self.data_folder, self.gtfs_file_name()?));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// The main GTFS struct. This is similar to (but not exactly) gtfs-structures::Gtfs because we don't need everything
|
||||
#[derive(Debug)]
|
||||
|
||||
Reference in New Issue
Block a user