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/consts.rs | 1 + src/main.rs | 65 ++++++++++++++++++++++++++++------------------------------- src/utils.rs | 44 ++++++++++++++++++++++++++++++++++------ 3 files changed, 70 insertions(+), 40 deletions(-) diff --git a/src/consts.rs b/src/consts.rs index 2f7576e..52ab580 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -9,3 +9,4 @@ pub mod colors { } pub const FONT_PATH: &'static str = "./assets/NerdInput-Regular.ttf"; +pub const STDLIB_PATH: &'static str = "./src/lisp/std.lisp"; 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), + _ => (), } } diff --git a/src/utils.rs b/src/utils.rs index 3e5ff55..fc96615 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,18 @@ -use crate::{consts::FONT_PATH, lisp::error::LispError, message::Message}; +use crate::{ + app::AppState, + consts::FONT_PATH, + lisp::{ + error::{EvalError, LispError, ParseError}, + eval::eval, + expr::LispExpr, + lex::Lexer, + parse::Parser, + }, + message::Message, +}; + +use std::{fs::OpenOptions, io::Read, path::Path}; + use sdl2::{ keyboard::{Keycode, Mod}, pixels::Color, @@ -47,11 +61,29 @@ pub fn is_paste_event(keycode: Option, keymod: Mod) -> bool { keycode == Some(Keycode::V) && (keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD) } -pub fn handle_error(err: LispError, src: &str) -> Message { +pub fn handle_error(err: ParseError, src: &str, file_name: &str) -> Message { let mut message = Message::new(); - match err { - LispError::Parse(p) => message.set_error(p.display(&src)), - eval_err => message.set_error(eval_err.to_string()), - } + message.set_error(err.display(&src, file_name)); message } + +pub fn load_script>(path: P, app: &mut AppState) -> Result<(), LispError> { + let mut script = OpenOptions::new() + .read(true) + .write(false) + .create(false) + .open(path.as_ref()) + .map_err(EvalError::ScriptLoadError)?; + let mut buf = String::new(); + script + .read_to_string(&mut buf) + .map_err(EvalError::ScriptLoadError)?; + + let mut parser = Parser::new(Lexer::new(&buf, 0)); + for expr in parser.parse_exprs().map_err(|err| { + LispError::Stringified(err.display(&buf, path.as_ref().to_str().unwrap_or(""))) + })? { + eval(&expr, app)?; + } + return Ok(()); +} -- cgit v1.2.3