From 07ae09ee91182e88fe548b71703c445fe9a1e28a Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 30 Mar 2021 16:52:43 +0530 Subject: use Display trait instead of Debug to show errors on main --- src/main.rs | 65 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 34 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 31f64f1..1b6e93a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod brush; mod command; mod consts; mod dither; +mod error; mod lisp; mod message; mod symmetry; @@ -11,6 +12,7 @@ mod undo; mod utils; use app::AppState; +use error::{AppError, SdlTTFError}; use std::{ env, @@ -22,52 +24,47 @@ use std::{ use log::{error, info}; use obi::Image; -pub fn main() { +pub fn error_sink() -> Result<(), AppError> { env_logger::init(); - let sdl_context = sdl2::init(); - if sdl_context.is_err() { - error!("Unable to find libsdl2 ... Exiting"); - return; - } - let sdl_context = sdl_context.unwrap(); + let sdl_context = sdl2::init().map_err(AppError::Sdl)?; info!("Initialized SDL context"); - let ttf_context = sdl2::ttf::init(); - if ttf_context.is_err() { - error!("Unable to find SDL2_ttf ... Exiting"); - return; - } - let ttf_context = ttf_context.unwrap(); + let ttf_context = sdl2::ttf::init().map_err(|e| AppError::SdlTTF(SdlTTFError::Init(e)))?; info!("Initialized SDL_ttf context"); let args: Vec<_> = env::args().collect(); if args.len() < 2 { - AppState::init(160, 160, &sdl_context, &ttf_context, None, None).run(); - return; + AppState::init(160, 160, &sdl_context, &ttf_context, None, None)?.run(); + return Ok(()); } else { let path = PathBuf::from(args.get(1).unwrap()); - let image_src = OpenOptions::new() + let mut image = OpenOptions::new() .read(true) .write(false) .create(false) - .open(&path); - if let Ok(mut image) = image_src { - let mut buf = Vec::new(); - image.read_to_end(&mut buf).unwrap(); - let decoded = Image::decode(&mut (Cursor::new(buf))).unwrap(); - let (width, height) = (decoded.width(), decoded.height()); - AppState::init( - width, - height, - &sdl_context, - &ttf_context, - Some(decoded.data), - Some(&path), - ) - .run(); - } else { - AppState::init(500, 500, &sdl_context, &ttf_context, None, Some(&path)).run() - } + .open(&path) + .map_err(AppError::File)?; + let mut buf = Vec::new(); + image.read_to_end(&mut buf).map_err(AppError::File)?; + let decoded = Image::decode(&mut (Cursor::new(buf))).unwrap(); // TODO: obi error + let (width, height) = (decoded.width(), decoded.height()); + AppState::init( + width, + height, + &sdl_context, + &ttf_context, + Some(decoded.data), + Some(&path), + )? + .run(); + return Ok(()); + } +} + +pub fn main() { + match error_sink() { + Err(e) => error!("{}", e), + _ => (), } } -- cgit v1.2.3