client_cli
This commit is contained in:
parent
2525dd3664
commit
92d5779929
6 changed files with 104 additions and 1 deletions
57
client_cli/src/app.rs
Normal file
57
client_cli/src/app.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// 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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue