Set up SGF reading and start on the game database #47

Merged
savanni merged 20 commits from kifu/sgf into main 2023-07-26 13:54:28 +00:00
2 changed files with 39 additions and 4 deletions
Showing only changes of commit 741f963606 - Show all commits

View File

@ -63,7 +63,7 @@ fn parse_numbers(s: &str) -> Result<Vec<i32>, Error> {
.collect::<Result<Vec<i32>, Error>>()
}
fn parse_date_field(s: &str) -> Result<Vec<Date>, Error> {
pub fn parse_date_field(s: &str) -> Result<Vec<Date>, Error> {
let date_elements = s.split(",");
let mut dates = Vec::new();

View File

@ -69,7 +69,7 @@
// VW
use crate::{
date::Date,
date::{self, parse_date_field, Date},
tree::{parse_collection, ParseSizeError, Size},
};
@ -172,7 +172,6 @@ pub enum GameResult {
impl TryFrom<&str> for GameResult {
type Error = String;
fn try_from(s: &str) -> Result<GameResult, Self::Error> {
println!("Result try_from: {:?}", s);
if s == "0" {
Ok(GameResult::Draw)
} else if s == "Void" {
@ -279,6 +278,42 @@ pub fn parse_sgf<'a>(input: &'a str) -> Result<Vec<GameTree>, Error<'a>> {
.and_then(|prop| prop.values[0].parse::<u64>().ok())
.and_then(|seconds| Some(std::time::Duration::from_secs(seconds)));
info.date = tree.sequence[0]
.find_prop("DT")
.and_then(|prop| {
let v = prop
.values
.iter()
.map(|val| parse_date_field(val))
.fold(Ok(vec![]), |acc, v| match (acc, v) {
(Ok(mut acc), Ok(mut values)) => {
acc.append(&mut values);
Ok(acc)
}
(Ok(_), Err(err)) => Err(err),
(Err(err), _) => Err(err),
})
.ok()?;
Some(v)
})
.unwrap_or(vec![]);
info.event = tree.sequence[0]
.find_prop("EV")
.map(|prop| prop.values.join(", "));
info.round = tree.sequence[0]
.find_prop("RO")
.map(|prop| prop.values.join(", "));
info.source = tree.sequence[0]
.find_prop("SO")
.map(|prop| prop.values.join(", "));
info.game_keeper = tree.sequence[0]
.find_prop("US")
.map(|prop| prop.values.join(", "));
Ok(GameTree {
file_format,
app_name,
@ -371,7 +406,7 @@ mod tests {
]
);
assert_eq!(tree.info.event, Some("21st Meijin".to_owned()));
assert_eq!(tree.info.event, Some("2 (final)".to_owned()));
assert_eq!(tree.info.round, Some("2 (final)".to_owned()));
assert_eq!(tree.info.source, Some("Go World #78".to_owned()));
assert_eq!(tree.info.game_keeper, Some("Arno Hollosi".to_owned()));
});