diff --git a/go-sgf/src/date.rs b/go-sgf/src/date.rs index 5dc06f2..144eeb9 100644 --- a/go-sgf/src/date.rs +++ b/go-sgf/src/date.rs @@ -63,7 +63,7 @@ fn parse_numbers(s: &str) -> Result, Error> { .collect::, Error>>() } -fn parse_date_field(s: &str) -> Result, Error> { +pub fn parse_date_field(s: &str) -> Result, Error> { let date_elements = s.split(","); let mut dates = Vec::new(); diff --git a/go-sgf/src/go.rs b/go-sgf/src/go.rs index 15e570f..a4a7a24 100644 --- a/go-sgf/src/go.rs +++ b/go-sgf/src/go.rs @@ -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 { - 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, Error<'a>> { .and_then(|prop| prop.values[0].parse::().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())); });