Record actions in file
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
5a057a49e6
commit
ebabf90ba6
3 changed files with 36 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)
|
||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -23,9 +23,22 @@ 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::create(filename)?;
|
||||
actions
|
||||
.iter()
|
||||
.map(|action| format!("{}\n", action))
|
||||
.try_for_each(|action| file.write(action.as_bytes()).map(|_| ()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
let args = Args::parse();
|
||||
println!(
|
||||
|
@ -33,8 +46,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 +65,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