Record actions in file
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
8a5ba57a42
commit
2776c312b0
3 changed files with 37 additions and 2 deletions
|
@ -7,6 +7,7 @@ authors = ["Maxime “pep” Buquet <pep@bouah.net>"]
|
|||
description = "Keep track of your Hanabi plays and analyse them"
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.20"
|
||||
clap = { version = "3.2.16", features = ["derive"] }
|
||||
nom = "7.1.1"
|
||||
rustyline = "10.0.0"
|
||||
|
|
|
@ -17,9 +17,11 @@ use nom;
|
|||
use rustyline::error::ReadlineError;
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
use std::io::Error as IOError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
IOError(IOError),
|
||||
ParseColorError(String),
|
||||
ParseDigitError(String),
|
||||
NomError(nom::Err<nom::error::Error<String>>),
|
||||
|
@ -31,6 +33,7 @@ impl StdError for Error {}
|
|||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Error::IOError(err) => write!(f, "IO error: {}", err),
|
||||
Error::ParseDigitError(err) => write!(f, "Parse digit error: {}", err),
|
||||
Error::ParseColorError(err) => write!(f, "Parse color error: {}", err),
|
||||
Error::NomError(err) => write!(f, "Nom error: {}", err),
|
||||
|
@ -56,6 +59,12 @@ impl<'a> From<nom::Err<nom::error::Error<&'a str>>> for Error {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<IOError> for Error {
|
||||
fn from(err: IOError) -> Error {
|
||||
Error::IOError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ReadlineError> for Error {
|
||||
fn from(err: ReadlineError) -> Error {
|
||||
Error::ReadlineError(err)
|
||||
|
|
29
src/main.rs
29
src/main.rs
|
@ -23,9 +23,23 @@ use crate::error::Error;
|
|||
use crate::parser::parse_line;
|
||||
use crate::types::{Action, GameAction};
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use clap::Parser;
|
||||
use rustyline::{error::ReadlineError, Editor};
|
||||
|
||||
fn record_actions(filename: &str, actions: Vec<GameAction>) -> Result<(), Error> {
|
||||
let mut file = File::options().create(true).append(true).open(filename)?;
|
||||
println!("Foo: {:?}", actions);
|
||||
actions
|
||||
.iter()
|
||||
.map(|action| action.to_string())
|
||||
.try_for_each(|action| file.write(action.as_bytes()).map(|_| ()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
let args = Args::parse();
|
||||
println!(
|
||||
|
@ -33,8 +47,16 @@ fn main() -> Result<(), Error> {
|
|||
args.players, args.variant
|
||||
);
|
||||
|
||||
let mut actions: Vec<GameAction> = vec![];
|
||||
let dt: DateTime<Utc> = Utc::now();
|
||||
let filename = format!(
|
||||
"hanabi_{}p_{}.txt",
|
||||
args.players,
|
||||
dt.format("%Y-%m-%d_%H:%M:%S")
|
||||
);
|
||||
println!("Recording at: {}", filename);
|
||||
|
||||
let mut rl = Editor::<()>::new()?;
|
||||
let mut actions: Vec<GameAction> = vec![];
|
||||
|
||||
loop {
|
||||
let readline = rl.readline(">> ");
|
||||
|
@ -44,7 +66,10 @@ fn main() -> Result<(), Error> {
|
|||
if let Ok(action) = parse_line(line.as_str()) {
|
||||
println!("Action: {:?}", action);
|
||||
match action {
|
||||
Action::Game(action) => actions.push(action),
|
||||
Action::Game(action) => {
|
||||
actions.push(action);
|
||||
record_actions(filename.clone().as_str(), actions.clone())?
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue