client_cli

This commit is contained in:
Henri Bourcereau 2024-02-09 17:47:34 +01:00
parent 2525dd3664
commit 92d5779929
6 changed files with 104 additions and 1 deletions

11
Cargo.lock generated
View file

@ -1236,6 +1236,17 @@ dependencies = [
"libloading 0.7.4", "libloading 0.7.4",
] ]
[[package]]
name = "client_cli"
version = "0.1.0"
dependencies = [
"anyhow",
"bincode",
"pico-args",
"renet",
"store",
]
[[package]] [[package]]
name = "client_tui" name = "client_tui"
version = "0.1.0" version = "0.1.0"

View file

@ -4,6 +4,7 @@ resolver="2"
members = [ members = [
"client", "client",
"client_tui", "client_tui",
"client_cli",
"server", "server",
"store" "store"
] ]

View file

@ -1,4 +1,4 @@
# trictrac # Trictrac
Game of [Trictrac](https://en.wikipedia.org/wiki/Trictrac) in rust. Game of [Trictrac](https://en.wikipedia.org/wiki/Trictrac) in rust.

13
client_cli/Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "client_cli"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.75"
bincode = "1.3.3"
pico-args = "0.5.0"
renet = "0.0.13"
store = { path = "../store" }

57
client_cli/src/app.rs Normal file
View 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);
}
}

21
client_cli/src/main.rs Normal file
View file

@ -0,0 +1,21 @@
// Application.
pub mod app;
use anyhow::Result;
use app::App;
use std::io;
fn main() -> Result<()> {
// Create an application.
let mut app = App::new();
// Start the main loop.
while !app.should_quit {
println!("what?>");
let mut input = String::new();
let _bytecount = io::stdin().read_line(&mut input)?;
app.input(input.trim());
}
Ok(())
}