diff --git a/go-sgf/src/go.rs b/go-sgf/src/go.rs index a4f87f9..f900167 100644 --- a/go-sgf/src/go.rs +++ b/go-sgf/src/go.rs @@ -133,7 +133,7 @@ pub struct GameInfo { pub event: Option, // Games can be played across multiple days, even multiple years. The format specifies // shortcuts. - pub date_time: Vec, + pub date: Vec, pub location: Option, // special rules for the round-number and type pub round: Option, @@ -141,7 +141,7 @@ pub struct GameInfo { pub source: Option, pub time_limits: Option, pub game_keeper: Option, - pub komi: Option, + pub komi: Option, pub game_name: Option, pub game_comments: Option, @@ -177,15 +177,15 @@ impl TryFrom<&str> for GameResult { Ok(GameResult::Annulled) } else { let parts = s.split("+").collect::>(); - let res = match parts[0] { - "B" => GameResult::Black, - "W" => GameResult::White, + let res = match parts[0].to_ascii_lowercase().as_str() { + "b" => GameResult::Black, + "w" => GameResult::White, _ => panic!("unknown result format"), }; - match parts[1] { - "R" | "Resign" => Ok(res(Win::Resignation)), - "T" | "Time" => Ok(res(Win::Time)), - "F" | "Forfeit" => Ok(res(Win::Forfeit)), + match parts[1].to_ascii_lowercase().as_str() { + "r" | "resign" => Ok(res(Win::Resignation)), + "t" | "time" => Ok(res(Win::Time)), + "f" | "forfeit" => Ok(res(Win::Forfeit)), _ => { let score = parts[1].parse::().unwrap(); Ok(res(Win::Score(score))) @@ -360,7 +360,7 @@ mod tests { Some(std::time::Duration::from_secs(28800)) ); assert_eq!( - tree.info.date_time, + tree.info.date, vec![ chrono::NaiveDate::from_ymd_opt(1996, 10, 18).unwrap(), chrono::NaiveDate::from_ymd_opt(1996, 10, 19).unwrap(), diff --git a/kifu/core/Cargo.lock b/kifu/core/Cargo.lock index 43e9276..2047aaa 100644 --- a/kifu/core/Cargo.lock +++ b/kifu/core/Cargo.lock @@ -60,6 +60,15 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "cool_asserts" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee9f254e53f61e2688d3677fa2cbe4e9b950afd56f48819c98817417cf6b28ec" +dependencies = [ + "indent_write", +] + [[package]] name = "core-foundation-sys" version = "0.8.4" @@ -152,6 +161,12 @@ dependencies = [ "cxx-build", ] +[[package]] +name = "indent_write" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cfe9645a18782869361d9c8732246be7b410ad4e919d3609ebabdac00ba12c3" + [[package]] name = "itoa" version = "1.0.6" @@ -171,6 +186,8 @@ dependencies = [ name = "kifu-core" version = "0.1.0" dependencies = [ + "chrono", + "cool_asserts", "go-sgf", "grid", "serde", diff --git a/kifu/core/Cargo.toml b/kifu/core/Cargo.toml index df1da7b..5972a7a 100644 --- a/kifu/core/Cargo.toml +++ b/kifu/core/Cargo.toml @@ -6,9 +6,13 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -go-sgf = { path = "../../go-sgf" } -grid = { version = "0.9" } -serde_json = { version = "1" } -serde = { version = "1", features = [ "derive" ] } -thiserror = { version = "1" } -typeshare = { version = "1" } +chrono = { version = "0.4" } +go-sgf = { path = "../../go-sgf" } +grid = { version = "0.9" } +serde_json = { version = "1" } +serde = { version = "1", features = [ "derive" ] } +thiserror = { version = "1" } +typeshare = { version = "1" } + +[dev-dependencies] +cool_asserts = { version = "2" } diff --git a/kifu/core/src/database.rs b/kifu/core/src/database.rs index 76ca20c..af8f576 100644 --- a/kifu/core/src/database.rs +++ b/kifu/core/src/database.rs @@ -57,6 +57,7 @@ impl Database { #[cfg(test)] mod test { use super::*; + use cool_asserts::assert_matches; #[test] fn it_reads_empty_database() { @@ -73,5 +74,14 @@ mod test { for game in db.all_games() { assert_eq!(game.game_type, GameType::Go); } + + assert_matches!(db.all_games().find(|g| g.info.black_player == Some("Steve".to_owned())), + Some(game) => { + assert_eq!(game.info.black_player, Some("Steve".to_owned())); + assert_eq!(game.info.white_player, Some("Savanni".to_owned())); + assert_eq!(game.info.date, vec![chrono::NaiveDate::from_ymd_opt(2023, 4, 19).unwrap()]); + assert_eq!(game.info.komi, Some(6.5)); + } + ); } }