diff --git a/Cargo.toml b/Cargo.toml index 1cf149c..5d76b77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ authors = ["Maxime “pep” Buquet "] 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" diff --git a/src/error.rs b/src/error.rs index 077599e..dd06d78 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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>), @@ -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>> for Error { } } +impl From for Error { + fn from(err: IOError) -> Error { + Error::IOError(err) + } +} + impl From for Error { fn from(err: ReadlineError) -> Error { Error::ReadlineError(err) diff --git a/src/main.rs b/src/main.rs index 5c0d747..5f043bb 100644 --- a/src/main.rs +++ b/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) -> 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 = vec![]; + let dt: DateTime = 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 = 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 {