Convert the root game node

This commit is contained in:
Savanni D'Gerinel 2023-09-18 10:57:37 -04:00
parent d6a91b6af6
commit 99bb36dbcd
2 changed files with 116 additions and 4 deletions

View File

@ -20,6 +20,7 @@ pub enum SetupNodeError {
#[derive(Clone, Debug, PartialEq)]
pub enum GameError {
InvalidGame,
RequiredPropertiesMissing,
}
#[derive(Clone, Debug, PartialEq)]
@ -58,7 +59,7 @@ pub struct Game {
source: Option<String>,
time_limit: Option<Duration>,
overtime: Option<String>,
data_entry: Option<String>,
transcriber: Option<String>,
children: Vec<GameNode>,
}
@ -91,7 +92,7 @@ impl Game {
source: None,
time_limit: None,
overtime: None,
data_entry: None,
transcriber: None,
children: vec![],
}
@ -113,7 +114,81 @@ impl TryFrom<&parser::Tree> for Game {
type Error = GameError;
fn try_from(tree: &parser::Tree) -> Result<Self, Self::Error> {
unimplemented!()
let mut ty = None;
let mut size = None;
let mut black_player = Player {
name: None,
rank: None,
team: None,
};
let mut white_player = Player {
name: None,
rank: None,
team: None,
};
for prop in tree.root.properties.iter() {
match prop {
parser::Property::GameType(ty_) => ty = Some(ty_.clone()),
parser::Property::BoardSize(size_) => size = Some(size_.clone()),
parser::Property::BlackPlayer(name) => {
black_player.name = Some(name.clone());
}
parser::Property::WhitePlayer(name) => {
white_player.name = Some(name.clone());
}
parser::Property::BlackRank(rank) => {
black_player.rank = Some(rank.clone());
}
parser::Property::WhiteRank(rank) => {
white_player.rank = Some(rank.clone());
}
parser::Property::BlackTeam(team) => {
black_player.team = Some(team.clone());
}
parser::Property::WhiteTeam(team) => {
white_player.team = Some(team.clone());
}
_ => {}
}
}
let mut s = match (ty, size) {
(Some(ty), Some(size)) => Ok(Self::new(ty, size, black_player, white_player)),
_ => Err(Self::Error::RequiredPropertiesMissing),
}?;
for prop in tree.root.properties.iter() {
match prop {
parser::Property::GameType(_)
| parser::Property::BoardSize(_)
| parser::Property::BlackPlayer(_)
| parser::Property::WhitePlayer(_)
| parser::Property::BlackRank(_)
| parser::Property::WhiteRank(_)
| parser::Property::BlackTeam(_)
| parser::Property::WhiteTeam(_) => {}
parser::Property::Application(v) => s.app = Some(v.clone()),
parser::Property::Annotator(v) => s.annotator = Some(v.clone()),
parser::Property::Copyright(v) => s.copyright = Some(v.clone()),
parser::Property::EventDates(v) => s.dates = v.clone(),
parser::Property::EventName(v) => s.event = Some(v.clone()),
parser::Property::GameName(v) => s.game_name = Some(v.clone()),
parser::Property::ExtraGameInformation(v) => s.extra_info = Some(v.clone()),
parser::Property::GameOpening(v) => s.opening_info = Some(v.clone()),
parser::Property::GameLocation(v) => s.location = Some(v.clone()),
parser::Property::Result(v) => s.result = v.clone(),
parser::Property::Round(v) => s.round = Some(v.clone()),
parser::Property::Ruleset(v) => s.rules = Some(v.clone()),
parser::Property::Source(v) => s.source = Some(v.clone()),
parser::Property::TimeLimit(v) => s.time_limit = Some(v.clone()),
parser::Property::Overtime(v) => s.overtime = Some(v.clone()),
// parser::Property::Data(v) => s.transcriber = Some(v.clone()),
_ => {}
}
}
Ok(s)
}
}
@ -606,7 +681,7 @@ mod file_test {
assert_eq!(game.source, None);
assert_eq!(game.time_limit, Some(Duration::from_secs(1800)));
assert_eq!(game.overtime, Some("5x30 byo-yomi".to_owned()));
assert_eq!(game.data_entry, None);
assert_eq!(game.transcriber, None);
/*
Property {

View File

@ -482,6 +482,9 @@ pub enum Property {
// BL, WL
TimeLeft((Color, Duration)),
// TW, TB
Territory(Color, Vec<Position>),
Unknown(UnknownProperty),
}
@ -557,6 +560,24 @@ impl ToString for Property {
Property::User(value) => format!("US[{}]", value),
Property::WhiteRank(value) => format!("WR[{}]", value),
Property::WhiteTeam(value) => format!("WT[{}]", value),
Property::Territory(Color::White, positions) => {
format!(
"TW{}",
positions
.iter()
.map(|Position(p)| format!("[{}]", p))
.collect::<String>()
)
}
Property::Territory(Color::Black, positions) => {
format!(
"TB{}",
positions
.iter()
.map(|Position(p)| format!("[{}]", p))
.collect::<String>()
)
}
Property::Unknown(UnknownProperty { ident, value }) => {
format!("{}[{}]", ident, value)
}
@ -671,6 +692,12 @@ fn parse_property<'a, E: nom::error::ParseError<&'a str>>(
"US" => parse_propval(parse_simple_text().map(Property::User))(input)?,
"WR" => parse_propval(parse_simple_text().map(Property::WhiteRank))(input)?,
"WT" => parse_propval(parse_simple_text().map(Property::WhiteTeam))(input)?,
"TW" => parse_territory()
.map(|p| Property::Territory(Color::White, p))
.parse(input)?,
"TB" => parse_territory()
.map(|p| Property::Territory(Color::Black, p))
.parse(input)?,
_ => parse_propval(parse_simple_text().map(|value| {
Property::Unknown(UnknownProperty {
ident: ident.to_owned(),
@ -748,6 +775,16 @@ fn parse_game_result<'a, E: nom::error::ParseError<&'a str>>() -> impl Parser<&'
alt((parse_draw_result(), parse_void_result(), parse_win_result()))
}
fn parse_territory<'a, E: nom::error::ParseError<&'a str>>(
) -> impl Parser<&'a str, Vec<Position>, E> {
many1(|input| {
let (input, _) = tag("[")(input)?;
let (input, position) = parse_simple_text().map(|v| Position(v)).parse(input)?;
let (input, _) = tag("]")(input)?;
Ok((input, position))
})
}
fn parse_propval<'a, E: nom::error::ParseError<&'a str>>(
mut parser: impl Parser<&'a str, Property, E>,
) -> impl FnMut(&'a str) -> IResult<&'a str, Property, E> {