Skip to content

Commit

Permalink
error handling part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Apr 19, 2024
1 parent 551ad51 commit 8a01fbb
Showing 1 changed file with 44 additions and 23 deletions.
67 changes: 44 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io;

use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
use ratatui::{
prelude::*,
Expand All @@ -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);
Expand All @@ -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(())
}
Expand All @@ -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(())
}
}

Expand Down Expand Up @@ -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!"
);
}
}

0 comments on commit 8a01fbb

Please sign in to comment.