From 8a01fbbcb6f1003f00e88e95f96699b54422e13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Macio=C5=82ek?= Date: Fri, 19 Apr 2024 07:32:54 +0200 Subject: [PATCH] error handling part 2 --- src/main.rs | 67 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 52dcd47..520dcb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -use std::io; - use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind}; use ratatui::{ prelude::*, @@ -8,14 +6,14 @@ use ratatui::{ }; use color_eyre::{ - eyre::{bail, WrapErr}, + eyre::{bail, Ok, WrapErr}, Result, }; mod errors; mod tui; -fn main() -> io::Result<()> { +fn main() -> Result<()> { errors::install_hooks()?; let mut terminal = tui::init()?; let app_result = App::default().run(&mut terminal); @@ -30,10 +28,10 @@ pub struct App { } impl App { - pub fn run(&mut self, terminal: &mut tui::Tui) -> io::Result<()> { + pub fn run(&mut self, terminal: &mut tui::Tui) -> Result<()> { while !self.exit { terminal.draw(|frame| self.render_frame(frame))?; - self.handle_events()?; + self.handle_events().wrap_err("handle events failed")?; } Ok(()) } @@ -42,35 +40,40 @@ impl App { frame.render_widget(self, frame.size()); } - fn handle_events(&mut self) -> io::Result<()> { + fn handle_events(&mut self) -> Result<()> { match event::read()? { - Event::Key(key_event) if key_event.kind == KeyEventKind::Press => { - self.handle_key_event(key_event) - } - _ => {} - }; - Ok(()) + Event::Key(key_event) if key_event.kind == KeyEventKind::Press => self + .handle_key_event(key_event) + .wrap_err_with(|| format!("handling key event failed:\n{key_event:#?}")), + _ => Ok(()), + } } - fn handle_key_event(&mut self, key_event: KeyEvent) { + fn handle_key_event(&mut self, key_event: KeyEvent) -> Result<()> { match key_event.code { KeyCode::Char('q') => self.exit(), - KeyCode::Left => self.decrement_counter(), - KeyCode::Right => self.increment_counter(), + KeyCode::Left => self.decrement_counter()?, + KeyCode::Right => self.increment_counter()?, _ => {} } + Ok(()) } fn exit(&mut self) { self.exit = true; } - fn increment_counter(&mut self) { + fn increment_counter(&mut self) -> Result<()> { self.counter += 1; + if self.counter > 2 { + bail!("counter overflow!"); + } + Ok(()) } - fn decrement_counter(&mut self) { + fn decrement_counter(&mut self) -> Result<()> { self.counter -= 1; + Ok(()) } } @@ -139,18 +142,36 @@ mod tests { } #[test] - fn handle_key_event() -> io::Result<()> { + fn handle_key_event() { let mut app = App::default(); - app.handle_key_event(KeyCode::Right.into()); + app.handle_key_event(KeyCode::Right.into()).unwrap(); assert_eq!(app.counter, 1); - app.handle_key_event(KeyCode::Left.into()); + app.handle_key_event(KeyCode::Left.into()).unwrap(); assert_eq!(app.counter, 0); let mut app = App::default(); - app.handle_key_event(KeyCode::Char('q').into()); + app.handle_key_event(KeyCode::Char('q').into()).unwrap(); assert_eq!(app.exit, true); + } - Ok(()) + #[test] + #[should_panic(expected = "attempt to subtract with overflow")] + fn handle_key_event_panic() { + let mut app = App::default(); + let _ = app.handle_key_event(KeyCode::Left.into()); + } + + #[test] + fn handle_key_event_overflow() { + let mut app = App::default(); + assert!(app.handle_key_event(KeyCode::Right.into()).is_ok()); + assert!(app.handle_key_event(KeyCode::Right.into()).is_ok()); + assert_eq!( + app.handle_key_event(KeyCode::Right.into()) + .unwrap_err() + .to_string(), + "counter overflow!" + ); } }