From a1a5fbe048ace8081a34efa0b84b70abb45281b6 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 7 Sep 2023 22:05:05 -0400 Subject: [PATCH] Start interpreting parsing into property types --- sgf/src/lib.rs | 11 +- sgf/src/tree.rs | 270 ++++++++++++++++++++++++++---------------------- 2 files changed, 152 insertions(+), 129 deletions(-) diff --git a/sgf/src/lib.rs b/sgf/src/lib.rs index 1ea6d95..91fa828 100644 --- a/sgf/src/lib.rs +++ b/sgf/src/lib.rs @@ -1,12 +1,12 @@ mod date; pub use date::Date; -pub mod go; +// pub mod go; mod tree; use tree::parse_collection; -mod game; +// mod game; use thiserror::Error; @@ -25,10 +25,7 @@ pub enum Color { } #[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct Position { - row: char, - column: char, -} +pub struct Position(String); #[derive(Debug)] pub struct VerboseNomError(nom::error::VerboseError); @@ -70,6 +67,7 @@ impl From> for ParseError { } } +/* pub enum Game { Go(go::Game), Unsupported(tree::Tree), @@ -87,6 +85,7 @@ pub fn parse_sgf(input: &str) -> Result, Error> { }) .collect::>()) } +*/ /* impl From<(&str, VerboseErrorKind)> for diff --git a/sgf/src/tree.rs b/sgf/src/tree.rs index 167353d..f9bf3ad 100644 --- a/sgf/src/tree.rs +++ b/sgf/src/tree.rs @@ -1,4 +1,4 @@ -use crate::Error; +use crate::{Color, Error, Position}; use nom::{ branch::alt, bytes::complete::{escaped_transform, tag}, @@ -93,6 +93,7 @@ impl ToString for Node { } } +/* impl Node { pub fn find_prop(&self, ident: &str) -> Option { self.properties @@ -105,21 +106,123 @@ impl Node { self.next.get(0) } } +*/ +// KO +// MN +// AB +// AE +// AW +// PL +// DM +// GB +// GW +// HO +// N +// UC +// V +// BM +// DO +// IT +// TE +// AR +// CR +// DD +// LB +// LN +// MA +// SL +// SQ +// TR +// AP +// CA +// FF +// GM +// ST +// SZ +// AN +// BR +// BT +// CP +// DT +// EV +// GN +// GC +// ON +// OT +// PB +// PC +// PW +// RE +// RO +// RU +// SO +// TM +// US +// WR +// WT +// BL +// OB +// OW +// WL +// FG +// PM +// VW +#[derive(Clone, Debug, PartialEq)] +pub enum Property { + // B, W + Move((Color, Position)), + + // C + Comment(Vec), + + Unknown(UnknownProperty), +} + +#[derive(Clone, Debug, PartialEq)] +pub struct UnknownProperty { + ident: String, + values: Vec, +} + +/* #[derive(Clone, Debug, PartialEq)] pub struct Property { pub ident: String, pub values: Vec, } +*/ impl ToString for Property { fn to_string(&self) -> String { + match self { + Property::Move((color, position)) => format!( + "{}[{}]", + match color { + Color::White => "W", + Color::Black => "B", + }, + position.0 + ), + Property::Comment(values) => format!( + "C{}", + values + .iter() + .map(|v| format!("[{}]", v)) + .collect::() + ), + Property::Unknown(UnknownProperty { ident, values }) => { + let values = values + .iter() + .map(|val| format!("[{}]", val)) + .collect::(); + format!("{}{}", ident, values) + } + } + /* let values = self - .values - .iter() - .map(|val| format!("[{}]", val)) .collect::(); - format!("{}{}", self.ident, values) + */ } } @@ -174,13 +277,18 @@ fn parse_property<'a, E: nom::error::ParseError<&'a str>>( .into_iter() .map(|v| v.to_owned()) .collect::>(); - Ok(( - input, - Property { + + let prop = match ident { + "W" => Property::Move((Color::White, Position(values[0].clone()))), + "B" => Property::Move((Color::Black, Position(values[0].clone()))), + "C" => Property::Comment(values), + _ => Property::Unknown(UnknownProperty { ident: ident.to_owned(), values, - }, - )) + }), + }; + + Ok((input, prop)) } fn parse_propval<'a, E: nom::error::ParseError<&'a str>>( @@ -221,21 +329,12 @@ mod test { #[test] fn it_can_parse_properties() { let (_, prop) = parse_property::>("C[a]").unwrap(); - assert_eq!( - prop, - Property { - ident: "C".to_owned(), - values: vec!["a".to_owned()] - } - ); + assert_eq!(prop, Property::Comment(vec!["a".to_owned()])); let (_, prop) = parse_property::>("C[a][b][c]").unwrap(); assert_eq!( prop, - Property { - ident: "C".to_owned(), - values: vec!["a".to_owned(), "b".to_owned(), "c".to_owned()] - } + Property::Comment(vec!["a".to_owned(), "b".to_owned(), "c".to_owned()]) ); } @@ -246,10 +345,7 @@ mod test { assert_eq!( node, Node { - properties: vec![Property { - ident: "B".to_owned(), - values: vec!["ab".to_owned()] - }], + properties: vec![Property::Move((Color::Black, Position("ab".to_owned())))], next: vec![] } ); @@ -261,25 +357,13 @@ mod test { assert_eq!( node, Node { - properties: vec![Property { - ident: "B".to_owned(), - values: vec!["ab".to_owned()] - }], + properties: vec![Property::Move((Color::Black, Position("ab".to_owned())))], next: vec![Node { - properties: vec![Property { - ident: "W".to_owned(), - values: vec!["dp".to_owned()] - }], + properties: vec![Property::Move((Color::White, Position("dp".to_owned())))], next: vec![Node { properties: vec![ - Property { - ident: "B".to_owned(), - values: vec!["pq".to_owned()] - }, - Property { - ident: "C".to_owned(), - values: vec!["some comments".to_owned()] - } + Property::Move((Color::Black, Position("pq".to_owned()))), + Property::Comment(vec!["some comments".to_owned()]) ], next: vec![], }] @@ -297,25 +381,13 @@ mod test { assert_eq!( sequence, Node { - properties: vec![Property { - ident: "B".to_owned(), - values: vec!["ab".to_owned()] - }], + properties: vec![Property::Move((Color::Black, Position("ab".to_owned())))], next: vec![Node { - properties: vec![Property { - ident: "W".to_owned(), - values: vec!["dp".to_owned()] - }], + properties: vec![Property::Move((Color::White, Position("dp".to_owned())))], next: vec![Node { properties: vec![ - Property { - ident: "B".to_owned(), - values: vec!["pq".to_owned()] - }, - Property { - ident: "C".to_owned(), - values: vec!["some comments".to_owned()] - } + Property::Move((Color::Black, Position("pq".to_owned()))), + Property::Comment(vec!["some comments".to_owned()]) ], next: vec![], }] @@ -330,33 +402,18 @@ mod test { let (_, tree) = parse_tree::>(text).unwrap(); let expected = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["a".to_owned()], - }], + properties: vec![Property::Comment(vec!["a".to_owned()])], next: vec![Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["b".to_owned()], - }], + properties: vec![Property::Comment(vec!["b".to_owned()])], next: vec![ Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["c".to_owned()], - }], + properties: vec![Property::Comment(vec!["c".to_owned()])], next: vec![], }, Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["d".to_owned()], - }], + properties: vec![Property::Comment(vec!["d".to_owned()])], next: vec![Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["e".to_owned()], - }], + properties: vec![Property::Comment(vec!["e".to_owned()])], next: vec![], }], }, @@ -372,85 +429,52 @@ mod test { let (_, tree) = parse_tree::>(EXAMPLE).unwrap(); let j = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["j".to_owned()], - }], + properties: vec![Property::Comment(vec!["j".to_owned()])], next: vec![], }; let i = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["i".to_owned()], - }], + properties: vec![Property::Comment(vec!["i".to_owned()])], next: vec![], }; let h = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["h".to_owned()], - }], + properties: vec![Property::Comment(vec!["h".to_owned()])], next: vec![i], }; let g = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["g".to_owned()], - }], + properties: vec![Property::Comment(vec!["g".to_owned()])], next: vec![h], }; let f = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["f".to_owned()], - }], + properties: vec![Property::Comment(vec!["f".to_owned()])], next: vec![g, j], }; let e = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["e".to_owned()], - }], + properties: vec![Property::Comment(vec!["e".to_owned()])], next: vec![], }; let d = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["d".to_owned()], - }], + properties: vec![Property::Comment(vec!["d".to_owned()])], next: vec![e], }; let c = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["c".to_owned()], - }], + properties: vec![Property::Comment(vec!["c".to_owned()])], next: vec![], }; let b = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["b".to_owned()], - }], + properties: vec![Property::Comment(vec!["b".to_owned()])], next: vec![c, d], }; let a = Node { - properties: vec![Property { - ident: "C".to_owned(), - values: vec!["a".to_owned()], - }], + properties: vec![Property::Comment(vec!["a".to_owned()])], next: vec![b], }; let expected = Node { properties: vec![ - Property { + Property::Unknown(UnknownProperty { ident: "FF".to_owned(), values: vec!["4".to_owned()], - }, - Property { - ident: "C".to_owned(), - values: vec!["root".to_owned()], - }, + }), + Property::Comment(vec!["root".to_owned()]), ], next: vec![a, f], };