aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/rust/main.rs82
1 files changed, 63 insertions, 19 deletions
diff --git a/src/main/rust/main.rs b/src/main/rust/main.rs
index 4796c46..5b13d2a 100644
--- a/src/main/rust/main.rs
+++ b/src/main/rust/main.rs
@@ -1,4 +1,5 @@
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
+use miette::{Diagnostic, IntoDiagnostic, Result};
use ratatui::{
DefaultTerminal, Frame,
buffer::Buffer,
@@ -8,7 +9,7 @@ use ratatui::{
text::{Line, Text},
widgets::{Block, Paragraph, Widget},
};
-use std::io::Result;
+use thiserror::Error;
#[derive(Debug, Default)]
pub struct App {
@@ -16,6 +17,13 @@ pub struct App {
exit: bool,
}
+#[derive(Diagnostic, Debug, Error, PartialEq)]
+#[error("An overflow error occurred")]
+#[diagnostic()]
+pub struct OverflowError {
+ msg: String,
+}
+
fn main() -> Result<()> {
ratatui::run(|term| App::default().run(term))
}
@@ -24,7 +32,7 @@ impl App {
/// runs the application's main loop until the user quits
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> Result<()> {
while !self.exit {
- terminal.draw(|frame| self.draw(frame))?;
+ terminal.draw(|frame| self.draw(frame)).into_diagnostic()?;
self.handle_events()?;
}
Ok(())
@@ -35,36 +43,45 @@ impl App {
}
fn handle_events(&mut self) -> Result<()> {
- match event::read()? {
+ match event::read().into_diagnostic()? {
// it's important to check that the event is a key press event as
// crossterm also emits key release and repeat events on Windows.
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
self.handle_key_event(key_event)
}
- _ => {}
- };
- Ok(())
+ _ => 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::Char('q') => self.exit()?,
+ KeyCode::Left => self.decrement_counter()?,
+ KeyCode::Right => self.increment_counter()?,
_ => {}
- }
+ };
+ Ok(())
}
- fn exit(&mut self) {
+ fn exit(&mut self) -> Result<()> {
self.exit = true;
+ Ok(())
}
- fn decrement_counter(&mut self) {
- self.counter -= 1;
+ fn decrement_counter(&mut self) -> Result<(), OverflowError> {
+ if self.counter == 0 {
+ Err(OverflowError {
+ msg: "Overflow error occurred!".into(),
+ })
+ } else {
+ self.counter -= 1;
+ Ok(())
+ }
}
- fn increment_counter(&mut self) {
+ fn increment_counter(&mut self) -> Result<()> {
self.counter += 1;
+ Ok(())
}
}
@@ -128,16 +145,43 @@ mod tests {
}
#[test]
- fn handle_key_event() {
+ fn handle_key_event_inc_dec() {
let mut app = App::default();
- app.handle_key_event(KeyCode::Right.into());
+ let res = app.handle_key_event(KeyCode::Right.into());
+ assert!(res.is_ok());
assert_eq!(app.counter, 1);
- app.handle_key_event(KeyCode::Left.into());
+ let res = app.handle_key_event(KeyCode::Left.into());
+ assert!(res.is_ok());
assert_eq!(app.counter, 0);
+ }
+ #[test]
+ fn handle_key_event_exit() {
let mut app = App::default();
- app.handle_key_event(KeyCode::Char('q').into());
+ let res = app.handle_key_event(KeyCode::Char('q').into());
+ assert!(res.is_ok());
assert!(app.exit);
}
+
+ #[test]
+ fn handle_key_event_dec_error() {
+ let mut app = App::default();
+ let res = app.handle_key_event(KeyCode::Left.into());
+
+ assert!(res.is_err());
+ let res_err = res.unwrap_err();
+ let casted_val = res_err
+ .downcast_ref::<OverflowError>()
+ .expect("something bad occurred");
+
+ let expected_err_msg: String = "Overflow error occurred!".into();
+ assert_eq!(casted_val.msg, expected_err_msg);
+ assert_eq!(
+ *casted_val,
+ OverflowError {
+ msg: expected_err_msg,
+ },
+ );
+ }
}