Finish filling out all of the basic game info
This commit is contained in:
parent
82deabce48
commit
741f963606
|
@ -63,7 +63,7 @@ fn parse_numbers(s: &str) -> Result<Vec<i32>, Error> {
|
||||||
.collect::<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 date_elements = s.split(",");
|
||||||
let mut dates = Vec::new();
|
let mut dates = Vec::new();
|
||||||
|
|
||||||
|
|
|
@ -69,7 +69,7 @@
|
||||||
// VW
|
// VW
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
date::Date,
|
date::{self, parse_date_field, Date},
|
||||||
tree::{parse_collection, ParseSizeError, Size},
|
tree::{parse_collection, ParseSizeError, Size},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -172,7 +172,6 @@ pub enum GameResult {
|
||||||
impl TryFrom<&str> for GameResult {
|
impl TryFrom<&str> for GameResult {
|
||||||
type Error = String;
|
type Error = String;
|
||||||
fn try_from(s: &str) -> Result<GameResult, Self::Error> {
|
fn try_from(s: &str) -> Result<GameResult, Self::Error> {
|
||||||
println!("Result try_from: {:?}", s);
|
|
||||||
if s == "0" {
|
if s == "0" {
|
||||||
Ok(GameResult::Draw)
|
Ok(GameResult::Draw)
|
||||||
} else if s == "Void" {
|
} 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(|prop| prop.values[0].parse::<u64>().ok())
|
||||||
.and_then(|seconds| Some(std::time::Duration::from_secs(seconds)));
|
.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 {
|
Ok(GameTree {
|
||||||
file_format,
|
file_format,
|
||||||
app_name,
|
app_name,
|
||||||
|
@ -371,7 +406,7 @@ mod tests {
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
assert_eq!(tree.info.event, Some("21st Meijin".to_owned()));
|
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.source, Some("Go World #78".to_owned()));
|
||||||
assert_eq!(tree.info.game_keeper, Some("Arno Hollosi".to_owned()));
|
assert_eq!(tree.info.game_keeper, Some("Arno Hollosi".to_owned()));
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue