From 3741e29b8dc1bb2b7d2a48d66ed4189e6d2ae8de Mon Sep 17 00:00:00 2001 From: carrascomj Date: Mon, 20 Jan 2025 13:37:28 +0100 Subject: [PATCH] style: report errors during CLI handling --- src/cli.rs | 23 ++++++++++++++++------- src/main.rs | 8 +++++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 5a9e40c..a5e5ce3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,13 +2,23 @@ use bevy::prelude::{App, Entity, FileDragAndDrop}; use bevy::window::PrimaryWindow; use std::env; +use std::io; use std::path::PathBuf; +use thiserror::Error; pub struct CliArgs { pub map_path: Option, pub data_path: Option, } +#[derive(Error, Debug)] +pub enum InitCliError { + #[error("supplied path is invalid.")] + InvalidPathError(#[from] io::Error), + #[error("window not initialized.")] + UninitWindow, +} + pub fn parse_args() -> CliArgs { let args: Vec = env::args().collect(); // the last args take priority @@ -29,28 +39,27 @@ pub fn parse_args() -> CliArgs { /// Generate `FileDragAndDrop` such that the map and/or data /// if supplied as CLI args are later loaded. -pub fn handle_cli_args(app: &mut App, cli_args: CliArgs) { - let Some((win, _)) = app +pub fn handle_cli_args(app: &mut App, cli_args: CliArgs) -> Result<(), InitCliError> { + let (win, _) = app .world_mut() .query::<(Entity, &PrimaryWindow)>() .iter(app.world()) .next() - else { - return; - }; + .ok_or(InitCliError::UninitWindow)?; // paths are canonicalized so that they are not interpreted // to be in the assets directory by bevy's `AssetLoader`. if let Some(map_path) = cli_args.map_path { app.world_mut().send_event(FileDragAndDrop::DroppedFile { window: win, - path_buf: map_path.canonicalize().unwrap(), + path_buf: map_path.canonicalize()?, }); } if let Some(data_path) = cli_args.data_path { app.world_mut().send_event(FileDragAndDrop::DroppedFile { window: win, - path_buf: data_path.canonicalize().unwrap(), + path_buf: data_path.canonicalize()?, }); } + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 9bd34b2..63aa225 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,13 @@ fn main() { .add_plugins(legend::LegendPlugin); let cli_args = cli::parse_args(); - cli::handle_cli_args(app, cli_args); + if let Err(e) = cli::handle_cli_args(app, cli_args) { + use cli::InitCliError::*; + match e { + InvalidPathError(error) => error!("Supplied path as arg is invalid: {error}"), + UninitWindow => error!("Window was not initialized!"), + } + } app.run(); }