diff options
| author | Kevin Hoerr <kjhoerr@submelon.dev> | 2026-02-25 18:31:00 +0000 |
|---|---|---|
| committer | Kevin Hoerr <kjhoerr@submelon.dev> | 2026-02-25 18:31:00 +0000 |
| commit | 8fdb982644d204609484fc6417f4affec938ed98 (patch) | |
| tree | 916abc227dd1d6c3d59940bcc484caf7ec69a434 /src/main | |
| parent | cfc21d9826fa7d958a67027a86b71bf2b83852e3 (diff) | |
| download | equity-tracker-8fdb982644d204609484fc6417f4affec938ed98.tar.gz equity-tracker-8fdb982644d204609484fc6417f4affec938ed98.tar.bz2 equity-tracker-8fdb982644d204609484fc6417f4affec938ed98.zip | |
main.rs: Add overflow check for u8::MAX
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/rust/main.rs | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/src/main/rust/main.rs b/src/main/rust/main.rs index 5b13d2a..9248661 100644 --- a/src/main/rust/main.rs +++ b/src/main/rust/main.rs @@ -21,7 +21,8 @@ pub struct App { #[error("An overflow error occurred")] #[diagnostic()] pub struct OverflowError { - msg: String, + #[help] + _msg: String, } fn main() -> Result<()> { @@ -71,7 +72,7 @@ impl App { fn decrement_counter(&mut self) -> Result<(), OverflowError> { if self.counter == 0 { Err(OverflowError { - msg: "Overflow error occurred!".into(), + _msg: "Cannot count below 0".into(), }) } else { self.counter -= 1; @@ -79,9 +80,15 @@ impl App { } } - fn increment_counter(&mut self) -> Result<()> { - self.counter += 1; - Ok(()) + fn increment_counter(&mut self) -> Result<(), OverflowError> { + if self.counter == u8::MAX { + Err(OverflowError { + _msg: "Cannot count above u8::MAX".into(), + }) + } else { + self.counter += 1; + Ok(()) + } } } @@ -175,12 +182,34 @@ mod tests { .downcast_ref::<OverflowError>() .expect("something bad occurred"); - let expected_err_msg: String = "Overflow error occurred!".into(); - assert_eq!(casted_val.msg, expected_err_msg); + let expected_err_msg: String = "Cannot count below 0".into(); + assert_eq!(casted_val._msg, expected_err_msg); + assert_eq!( + *casted_val, + OverflowError { + _msg: expected_err_msg, + }, + ); + } + + #[test] + fn handle_key_event_inc_error() { + let mut app = App::default(); + app.counter = u8::MAX; + let res = app.handle_key_event(KeyCode::Right.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 = "Cannot count above u8::MAX".into(); + assert_eq!(casted_val._msg, expected_err_msg); assert_eq!( *casted_val, OverflowError { - msg: expected_err_msg, + _msg: expected_err_msg, }, ); } |
