// Application. #[derive(Debug, Default)] pub struct App { // should the application exit? pub should_quit: bool, // counter pub counter: u8, } impl App { // Constructs a new instance of [`App`]. pub fn new() -> Self { Self::default() } pub fn input(&mut self, input: &str) { println!("'{}'", input); if input == "quit" { self.quit(); } } // Set running to false to quit the application. pub fn quit(&mut self) { self.should_quit = true; } pub fn increment_counter(&mut self) { if let Some(res) = self.counter.checked_add(1) { self.counter = res; } } pub fn decrement_counter(&mut self) { if let Some(res) = self.counter.checked_sub(1) { self.counter = res; } } } #[cfg(test)] mod tests { use super::*; #[test] fn test_app_increment_counter() { let mut app = App::default(); app.increment_counter(); assert_eq!(app.counter, 1); } #[test] fn test_app_decrement_counter() { let mut app = App::default(); app.decrement_counter(); assert_eq!(app.counter, 0); } }