Better error handling

This commit is contained in:
Nahuel Lofeudo 2026-05-24 07:36:43 +01:00
parent 74aa9e8a84
commit 08c028bfa8
6 changed files with 55 additions and 20 deletions

View File

@ -10,8 +10,8 @@ csv = "1.4"
gtfs-structures = "0.47" gtfs-structures = "0.47"
log = "0.4" log = "0.4"
sdl3 = {version = "0.17", features = ["ttf"]} sdl3 = {version = "0.17", features = ["ttf"]}
serde = { "1.0", features = ["derive"]} serde = { version = "1.0", features = ["derive"]}
serde_yaml = "0.8" yaml_serde = "0.10"
zip = "8.3" zip = "8.3"
[profile.dev] [profile.dev]

View File

@ -1,14 +1,11 @@
use crate::{gtfs, renderer}; use crate::{gtfs, renderer};
use std::collections::HashSet; use serde::{Serialize, Deserialize};
extern crate serde_derive; use std::fs;
extern crate serde_yaml; use yaml_serde;
struct Error {
message: String
}
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Config { pub struct Config {
#[serde(rename = "gtfs")] #[serde(rename = "gtfs")]
pub gtfs_prefs: gtfs::structs::Preferences, pub gtfs_prefs: gtfs::structs::Preferences,
@ -16,7 +13,39 @@ struct Config {
pub screen_prefs: renderer::structs::Prefs, pub screen_prefs: renderer::structs::Prefs,
} }
#[derive (Debug)]
pub struct Error {
pub message: String
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
return Error {
message: value.to_string(),
};
}
}
impl From<yaml_serde::Error> for Error {
fn from(value: yaml_serde::Error) -> Self {
return Error {
message: value.to_string(),
}
}
}
impl From<sdl3::Error> for Error {
fn from(value: sdl3::Error) -> Self {
return Error {
message: value.to_string(),
}
}
}
pub fn load_config(config_name: String) -> Result<Config, Error> { pub fn load_config(config_name: String) -> Result<Config, Error> {
return Err(Error { message: String::from("Foo") }); let config_text = fs::read_to_string(config_name)?;
let config = yaml_serde::from_str(config_text.as_ref())?;
return Ok(config);
} }

View File

@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop}; use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
use serde::{Serialize, Deserialize};
// This is to store the preferences for the GTFS(-R) side of the code. // This is to store the preferences for the GTFS(-R) side of the code.
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]

View File

@ -88,8 +88,9 @@ fn main() {
process::exit(-1); process::exit(-1);
} }
// Create preferences structures from config // Create preferences structures from config
let gtfs_prefs = config_result.unwrap().gtfs_prefs; let config = config_result.unwrap();
let screen_prefs = config_result.unwrap().screen_prefs; let gtfs_prefs = config.gtfs_prefs;
let screen_prefs = config.screen_prefs;
// Init GTFS static info // Init GTFS static info
info!("Loading GTFS data..."); info!("Loading GTFS data...");
@ -103,7 +104,12 @@ fn main() {
// Init screen // Init screen
info!("Initializing screen..."); info!("Initializing screen...");
let mut screen = Screen::init(&screen_prefs); let screen_result = Screen::init(&screen_prefs);
if let Err(e) = screen_result {
error!("Error initializing video: {}", e.message);
process::exit(-1);
}
let mut screen = screen_result.unwrap();
info!("Startup done."); info!("Startup done.");
// Register our custom event and obtain the event-related objects to interact with the event loop // Register our custom event and obtain the event-related objects to interact with the event loop

View File

@ -5,7 +5,7 @@ use log::{debug, error, warn};
use sdl3::{Sdl, pixels::Color, rect::Rect}; use sdl3::{Sdl, pixels::Color, rect::Rect};
use structs::{Prefs, DisplayData}; use structs::{Prefs, DisplayData};
use crate::renderer::structs::Screen; use crate::{config::Error, renderer::structs::Screen};
const LINE_COUNT: i32 = 5; const LINE_COUNT: i32 = 5;
const COLOR_LCD_AMBER : Color = Color::RGB(0xf4, 0xcb, 0x60); const COLOR_LCD_AMBER : Color = Color::RGB(0xf4, 0xcb, 0x60);
@ -131,10 +131,10 @@ impl Screen<'_> {
/// Initialize video, allocate buffers and load fonts /// Initialize video, allocate buffers and load fonts
/// Based on https://github.com/vhspace/sdl3-rs/blob/master/examples/ttf-demo.rs /// Based on https://github.com/vhspace/sdl3-rs/blob/master/examples/ttf-demo.rs
pub fn init(prefs: &Prefs) -> Screen<'_> { pub fn init(prefs: &Prefs) -> Result<Screen<'_>, Error> {
// Initialize the screen // Initialize the screen
let sdl_context = sdl3::init().unwrap(); let sdl_context = sdl3::init().unwrap();
let video_subsys = sdl_context.video().unwrap(); let video_subsys = sdl_context.video()?;
let window = video_subsys let window = video_subsys
.window("Dublin Bus", prefs.screen_width, prefs.screen_height) .window("Dublin Bus", prefs.screen_width, prefs.screen_height)
@ -145,7 +145,7 @@ impl Screen<'_> {
// Load font // Load font
let ttf_context = sdl3::ttf::init().unwrap(); let ttf_context = sdl3::ttf::init().unwrap();
let font = ttf_context.load_font(&prefs.font_path, TEXT_SIZE).unwrap(); let font = ttf_context.load_font(&prefs.font_path, TEXT_SIZE)?;
let mut screen: Screen = Screen { let mut screen: Screen = Screen {
canvas: Box::new(window.into_canvas()), canvas: Box::new(window.into_canvas()),
@ -155,6 +155,6 @@ impl Screen<'_> {
}; };
screen.clear(); screen.clear();
return screen; return Ok(screen);
} }
} }

View File

@ -1,6 +1,5 @@
use sdl3::{Sdl, pixels::Color, render::Canvas, ttf::Font, video::Window}; use sdl3::{Sdl, pixels::Color, render::Canvas, ttf::Font, video::Window};
extern crate serde; use serde::{Serialize, Deserialize};
extern crate serde_derive;
pub struct Screen<'a> { pub struct Screen<'a> {
pub(crate) canvas: Box<Canvas<Window>>, pub(crate) canvas: Box<Canvas<Window>>,