First set of changes to use config file instead of hard-coded settings.

This commit is contained in:
Nahuel Lofeudo 2026-05-23 11:25:41 +01:00
parent 391380040e
commit 74aa9e8a84
6 changed files with 85 additions and 19 deletions

View File

@ -5,13 +5,14 @@ edition = "2024"
host = "x86_64-unknown-linux-gnu"
[dependencies]
sdl3 = {version = "0.17", features = ["ttf"]}
serde = "1.0"
gtfs-structures = "0.47"
chrono = "0.4"
log = "0.4"
zip = "8.3"
csv = "1.4"
gtfs-structures = "0.47"
log = "0.4"
sdl3 = {version = "0.17", features = ["ttf"]}
serde = { "1.0", features = ["derive"]}
serde_yaml = "0.8"
zip = "8.3"
[profile.dev]
opt-level = 3

18
resources/dublinbus.yaml Normal file
View File

@ -0,0 +1,18 @@
screen:
width: 1920
height: 720
font-path: "resources/jd_lcd_rounded.ttf"
gtfs:
routes:
- 15A
- F1
- F2
stops:
- 1114
- 2410
data-folder: "/home/nahuel/Downloads"
gtfs-url: "https://www.transportforireland.ie/transitData/Data/GTFS_Realtime.zip"
realtime-url: "https://api.nationaltransport.ie/gtfsr/v2/gtfsr"
realtime-api-key: "Not defined"
refresh-seconds: 61

22
src/config.rs Normal file
View File

@ -0,0 +1,22 @@
use crate::{gtfs, renderer};
use std::collections::HashSet;
extern crate serde_derive;
extern crate serde_yaml;
struct Error {
message: String
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Config {
#[serde(rename = "gtfs")]
pub gtfs_prefs: gtfs::structs::Preferences,
#[serde(rename = "screen")]
pub screen_prefs: renderer::structs::Prefs,
}
pub fn load_config(config_name: String) -> Result<Config, Error> {
return Err(Error { message: String::from("Foo") });
}

View File

@ -1,11 +1,29 @@
use std::collections::{HashMap, HashSet};
use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
// This is to store the preferences for the GTFS(-R) side of the code.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Preferences {
#[serde(rename = "routes")]
pub route_numbers: HashSet<String>,
#[serde(rename = "stops")]
pub stop_codes: HashSet<String>,
#[serde(rename = "data-folder")]
pub data_folder: String,
#[serde(rename = "gtfs-url")]
pub gtfs_url: String,
#[serde(rename = "realtime-url")]
pub realtime_url: String,
#[serde(rename = "realtime-api-key")]
pub realtime_api_key: String,
#[serde(rename = "refresh-seconds")]
pub refresh_seconds: u32,
}

View File

@ -1,10 +1,11 @@
mod gtfs;
mod renderer;
use std::{collections::HashSet, ops::Add, process, thread::Builder, time::SystemTime};
mod config;
use std::{ops::Add, process, thread::Builder, time::SystemTime};
use chrono::{DateTime, Duration, Local, NaiveTime};
use log::{Metadata, Record, error, info};
use sdl3::event::Event;
use crate::{gtfs::structs::{Arrival, Gtfs}, renderer::structs::{DisplayData, DisplayEntry, Screen}};
use crate::{config::load_config, gtfs::structs::{Arrival, Gtfs}, renderer::structs::{DisplayData, DisplayEntry, Screen}};
const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip";
const NUM_ARRIVALS: usize = 4;
@ -80,17 +81,15 @@ fn main() {
}
log::set_max_level(log::LevelFilter::Trace);
// Create preferences structures from config
let gtfs_prefs = gtfs::structs::Preferences {
route_numbers: HashSet::from([String::from("15A"), String::from("F1"), String::from("F2"), String::from("F3")]),
stop_codes: HashSet::from([String::from("1114")])
};
let screen_prefs = renderer::structs::Prefs {
font_path: String::from("resources/jd_lcd_rounded/jd-lcd-rounded.ttf"),
screen_width: 1920,
screen_height: 720,
};
let config_result = load_config(String::from("resources/dublinbus.yaml"));
if config_result.is_err() {
error!("Error loading the config file: {:#?}", config_result.err());
process::exit(-1);
}
// Create preferences structures from config
let gtfs_prefs = config_result.unwrap().gtfs_prefs;
let screen_prefs = config_result.unwrap().screen_prefs;
// Init GTFS static info
info!("Loading GTFS data...");

View File

@ -1,5 +1,6 @@
use sdl3::{Sdl, pixels::Color, render::Canvas, ttf::Font, video::Window};
extern crate serde;
extern crate serde_derive;
pub struct Screen<'a> {
pub(crate) canvas: Box<Canvas<Window>>,
@ -8,9 +9,16 @@ pub struct Screen<'a> {
pub(crate) context: Box<Sdl>
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Prefs {
#[serde(rename = "font-path")]
pub font_path: String,
#[serde(rename = "width")]
pub screen_width: u32,
#[serde(rename = "height")]
pub screen_height: u32,
}