aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/consts.rs1
-rw-r--r--src/main.rs65
-rw-r--r--src/utils.rs44
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 {
9} 9}
10 10
11pub const FONT_PATH: &'static str = "./assets/NerdInput-Regular.ttf"; 11pub const FONT_PATH: &'static str = "./assets/NerdInput-Regular.ttf";
12pub 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;
4mod command; 4mod command;
5mod consts; 5mod consts;
6mod dither; 6mod dither;
7mod error;
7mod lisp; 8mod lisp;
8mod message; 9mod message;
9mod symmetry; 10mod symmetry;
@@ -11,6 +12,7 @@ mod undo;
11mod utils; 12mod utils;
12 13
13use app::AppState; 14use app::AppState;
15use error::{AppError, SdlTTFError};
14 16
15use std::{ 17use std::{
16 env, 18 env,
@@ -22,52 +24,47 @@ use std::{
22use log::{error, info}; 24use log::{error, info};
23use obi::Image; 25use obi::Image;
24 26
25pub fn main() { 27pub fn error_sink() -> Result<(), AppError> {
26 env_logger::init(); 28 env_logger::init();
27 29
28 let sdl_context = sdl2::init(); 30 let sdl_context = sdl2::init().map_err(AppError::Sdl)?;
29 if sdl_context.is_err() {
30 error!("Unable to find libsdl2 ... Exiting");
31 return;
32 }
33 let sdl_context = sdl_context.unwrap();
34 info!("Initialized SDL context"); 31 info!("Initialized SDL context");
35 32
36 let ttf_context = sdl2::ttf::init(); 33 let ttf_context = sdl2::ttf::init().map_err(|e| AppError::SdlTTF(SdlTTFError::Init(e)))?;
37 if ttf_context.is_err() {
38 error!("Unable to find SDL2_ttf ... Exiting");
39 return;
40 }
41 let ttf_context = ttf_context.unwrap();
42 info!("Initialized SDL_ttf context"); 34 info!("Initialized SDL_ttf context");
43 35
44 let args: Vec<_> = env::args().collect(); 36 let args: Vec<_> = env::args().collect();
45 if args.len() < 2 { 37 if args.len() < 2 {
46 AppState::init(160, 160, &sdl_context, &ttf_context, None, None).run(); 38 AppState::init(160, 160, &sdl_context, &ttf_context, None, None)?.run();
47 return; 39 return Ok(());
48 } else { 40 } else {
49 let path = PathBuf::from(args.get(1).unwrap()); 41 let path = PathBuf::from(args.get(1).unwrap());
50 let image_src = OpenOptions::new() 42 let mut image = OpenOptions::new()
51 .read(true) 43 .read(true)
52 .write(false) 44 .write(false)
53 .create(false) 45 .create(false)
54 .open(&path); 46 .open(&path)
55 if let Ok(mut image) = image_src { 47 .map_err(AppError::File)?;
56 let mut buf = Vec::new(); 48 let mut buf = Vec::new();
57 image.read_to_end(&mut buf).unwrap(); 49 image.read_to_end(&mut buf).map_err(AppError::File)?;
58 let decoded = Image::decode(&mut (Cursor::new(buf))).unwrap(); 50 let decoded = Image::decode(&mut (Cursor::new(buf))).unwrap(); // TODO: obi error
59 let (width, height) = (decoded.width(), decoded.height()); 51 let (width, height) = (decoded.width(), decoded.height());
60 AppState::init( 52 AppState::init(
61 width, 53 width,
62 height, 54 height,
63 &sdl_context, 55 &sdl_context,
64 &ttf_context, 56 &ttf_context,
65 Some(decoded.data), 57 Some(decoded.data),
66 Some(&path), 58 Some(&path),
67 ) 59 )?
68 .run(); 60 .run();
69 } else { 61 return Ok(());
70 AppState::init(500, 500, &sdl_context, &ttf_context, None, Some(&path)).run() 62 }
71 } 63}
64
65pub fn main() {
66 match error_sink() {
67 Err(e) => error!("{}", e),
68 _ => (),
72 } 69 }
73} 70}
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 @@
1use crate::{consts::FONT_PATH, lisp::error::LispError, message::Message}; 1use crate::{
2 app::AppState,
3 consts::FONT_PATH,
4 lisp::{
5 error::{EvalError, LispError, ParseError},
6 eval::eval,
7 expr::LispExpr,
8 lex::Lexer,
9 parse::Parser,
10 },
11 message::Message,
12};
13
14use std::{fs::OpenOptions, io::Read, path::Path};
15
2use sdl2::{ 16use sdl2::{
3 keyboard::{Keycode, Mod}, 17 keyboard::{Keycode, Mod},
4 pixels::Color, 18 pixels::Color,
@@ -47,11 +61,29 @@ pub fn is_paste_event(keycode: Option<Keycode>, keymod: Mod) -> bool {
47 keycode == Some(Keycode::V) && (keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD) 61 keycode == Some(Keycode::V) && (keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD)
48} 62}
49 63
50pub fn handle_error(err: LispError, src: &str) -> Message { 64pub fn handle_error(err: ParseError, src: &str, file_name: &str) -> Message {
51 let mut message = Message::new(); 65 let mut message = Message::new();
52 match err { 66 message.set_error(err.display(&src, file_name));
53 LispError::Parse(p) => message.set_error(p.display(&src)),
54 eval_err => message.set_error(eval_err.to_string()),
55 }
56 message 67 message
57} 68}
69
70pub fn load_script<P: AsRef<Path>>(path: P, app: &mut AppState) -> Result<(), LispError> {
71 let mut script = OpenOptions::new()
72 .read(true)
73 .write(false)
74 .create(false)
75 .open(path.as_ref())
76 .map_err(EvalError::ScriptLoadError)?;
77 let mut buf = String::new();
78 script
79 .read_to_string(&mut buf)
80 .map_err(EvalError::ScriptLoadError)?;
81
82 let mut parser = Parser::new(Lexer::new(&buf, 0));
83 for expr in parser.parse_exprs().map_err(|err| {
84 LispError::Stringified(err.display(&buf, path.as_ref().to_str().unwrap_or("<unknown>")))
85 })? {
86 eval(&expr, app)?;
87 }
88 return Ok(());
89}