Fix warnings mostly in the SGF parser

This commit is contained in:
Savanni D'Gerinel 2025-01-19 12:31:26 -05:00
parent d5b4d051a5
commit cc4f8c1515
5 changed files with 110 additions and 75 deletions

6
Taskfile.yml Normal file
View File

@ -0,0 +1,6 @@
version: '3'
tasks:
lint:
cmds:
- cargo watch -x clippy

View File

@ -163,9 +163,6 @@ impl GameReview {
*self.review_tree.borrow_mut() = Some(review_tree); *self.review_tree.borrow_mut() = Some(review_tree);
} }
fn redraw(&self) {
}
pub fn widget(&self) -> gtk::Widget { pub fn widget(&self) -> gtk::Widget {
self.widget.clone().upcast::<gtk::Widget>() self.widget.clone().upcast::<gtk::Widget>()
} }

View File

@ -10,7 +10,7 @@ use nom::{
IResult, Parser, IResult, Parser,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{fmt::Write, num::ParseIntError, time::Duration}; use std::{fmt::Display, num::ParseIntError, time::Duration};
impl From<ParseSizeError> for Error { impl From<ParseSizeError> for Error {
fn from(_: ParseSizeError) -> Self { fn from(_: ParseSizeError) -> Self {
@ -142,9 +142,9 @@ impl From<&GameType> for String {
} }
} }
impl ToString for GameType { impl Display for GameType {
fn to_string(&self) -> String { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
String::from(self) write!(f, "{}", String::from(self))
} }
} }
@ -189,12 +189,15 @@ pub struct Tree {
pub root: Node, pub root: Node,
} }
impl ToString for Tree { impl Display for Tree {
fn to_string(&self) -> String { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
format!("({})", self.root.to_string()) write!(f, "({})", self.root)
} }
} }
struct Properties<'a>(&'a Vec<Property>);
struct Nodes<'a>(&'a Vec<Node>);
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Node { pub struct Node {
pub properties: Vec<Property>, pub properties: Vec<Property>,
@ -264,8 +267,12 @@ impl Node {
} }
} }
impl ToString for Node { impl Display for Node {
fn to_string(&self) -> String { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, ";{}", Properties(&self.properties))?;
write!(f, "{}", Nodes(&self.next))?;
Ok(())
/*
let props = self let props = self
.properties .properties
.iter() .iter()
@ -281,11 +288,36 @@ impl ToString for Node {
} else { } else {
self.next self.next
.iter() .iter()
.map(|node| format!("({})", node.to_string())) .map(|node| write!(f, "({})", node.to_string()))
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("") .join("")
}; };
format!(";{}{}", props, next) write!(f, ";{}{}", props, next)
*/
}
}
impl Display for Properties<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for property in self.0.iter() {
write!(f, "{}", property)?;
}
Ok(())
}
}
impl Display for Nodes<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.0.len() == 1 {
for node in self.0.iter() {
write!(f, "{}", node)?;
}
} else {
for node in self.0.iter() {
write!(f, "({})", node)?;
}
}
Ok(())
} }
} }
@ -460,95 +492,96 @@ pub struct UnknownProperty {
pub value: String, pub value: String,
} }
impl ToString for Property { impl Display for Property {
fn to_string(&self) -> String { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Property::Move((color, Move::Move(mv))) => { Property::Move((color, Move::Move(mv))) => {
format!("{}[{}]", color.abbreviation(), mv) write!(f, "{}[{}]", color.abbreviation(), mv)
} }
Property::Move((color, Move::Pass)) => { Property::Move((color, Move::Pass)) => {
format!("{}[]", color.abbreviation()) write!(f, "{}[]", color.abbreviation())
} }
Property::TimeLeft((color, time)) => { Property::TimeLeft((color, time)) => {
format!("{}[{}]", color.abbreviation(), time.as_secs()) write!(f, "{}[{}]", color.abbreviation(), time.as_secs())
} }
Property::Comment(value) => format!("C[{}]", value), Property::Comment(value) => write!(f, "C[{}]", value),
Property::Annotation(Annotation::BadMove) => "BM[]".to_owned(), Property::Annotation(Annotation::BadMove) => write!(f, "BM[]"),
Property::Annotation(Annotation::DoubtfulMove) => "DO[]".to_owned(), Property::Annotation(Annotation::DoubtfulMove) => write!(f, "DO[]"),
Property::Annotation(Annotation::InterestingMove) => "IT[]".to_owned(), Property::Annotation(Annotation::InterestingMove) => write!(f, "IT[]"),
Property::Annotation(Annotation::Tesuji) => "TE[]".to_owned(), Property::Annotation(Annotation::Tesuji) => write!(f, "TE[]"),
Property::Application(app) => format!("AP[{}]", app), Property::Application(app) => write!(f, "AP[{}]", app),
Property::Charset(set) => format!("CA[{}]", set), Property::Charset(set) => write!(f, "CA[{}]", set),
Property::FileFormat(ff) => format!("FF[{}]", ff), Property::FileFormat(ff) => write!(f, "FF[{}]", ff),
Property::GameType(gt) => format!("GM[{}]", gt.to_string()), Property::GameType(gt) => write!(f, "GM[{}]", gt),
Property::VariationDisplay => unimplemented!(), Property::VariationDisplay => unimplemented!(),
Property::BoardSize(Size { width, height }) => { Property::BoardSize(Size { width, height }) => {
if width == height { if width == height {
format!("SZ[{}]", width) write!(f, "SZ[{}]", width)
} else { } else {
format!("SZ[{}:{}]", width, height) write!(f, "SZ[{}:{}]", width, height)
} }
} }
Property::SetupBlackStones(positions) => { Property::SetupBlackStones(positions) => {
format!("AB[{}]", positions.compressed_list(),) write!(f, "AB[{}]", positions.compressed_list(),)
} }
Property::ClearStones(positions) => { Property::ClearStones(positions) => {
format!("AE[{}]", positions.compressed_list(),) write!(f, "AE[{}]", positions.compressed_list(),)
} }
Property::SetupWhiteStones(positions) => { Property::SetupWhiteStones(positions) => {
format!("AW[{}]", positions.compressed_list(),) write!(f, "AW[{}]", positions.compressed_list(),)
} }
Property::NextPlayer(color) => format!("PL[{}]", color.abbreviation()), Property::NextPlayer(color) => write!(f, "PL[{}]", color.abbreviation()),
Property::Evaluation(Evaluation::Even) => "DM[]".to_owned(), Property::Evaluation(Evaluation::Even) => write!(f, "DM[]"),
Property::Evaluation(Evaluation::GoodForBlack) => "GB[]".to_owned(), Property::Evaluation(Evaluation::GoodForBlack) => write!(f, "GB[]"),
Property::Evaluation(Evaluation::GoodForWhite) => "GW[]".to_owned(), Property::Evaluation(Evaluation::GoodForWhite) => write!(f, "GW[]"),
Property::Evaluation(Evaluation::Unclear) => "UC[]".to_owned(), Property::Evaluation(Evaluation::Unclear) => write!(f, "UC[]"),
Property::Hotspot => "HO[]".to_owned(), Property::Hotspot => write!(f, "HO[]"),
Property::Value(value) => format!("V[{}]", value), Property::Value(value) => write!(f, "V[{}]", value),
Property::Annotator(value) => format!("AN[{}]", value), Property::Annotator(value) => write!(f, "AN[{}]", value),
Property::BlackRank(value) => format!("BR[{}]", value), Property::BlackRank(value) => write!(f, "BR[{}]", value),
Property::BlackTeam(value) => format!("BT[{}]", value), Property::BlackTeam(value) => write!(f, "BT[{}]", value),
Property::Copyright(value) => format!("CP[{}]", value), Property::Copyright(value) => write!(f, "CP[{}]", value),
Property::EventDates(_) => unimplemented!(), Property::EventDates(_) => unimplemented!(),
Property::EventName(value) => format!("EV[{}]", value), Property::EventName(value) => write!(f, "EV[{}]", value),
Property::GameName(value) => format!("GN[{}]", value), Property::GameName(value) => write!(f, "GN[{}]", value),
Property::ExtraGameInformation(value) => format!("GC[{}]", value), Property::ExtraGameInformation(value) => write!(f, "GC[{}]", value),
Property::GameOpening(value) => format!("ON[{}]", value), Property::GameOpening(value) => write!(f, "ON[{}]", value),
Property::Overtime(value) => format!("OT[{}]", value), Property::Overtime(value) => write!(f, "OT[{}]", value),
Property::BlackPlayer(value) => format!("PB[{}]", value), Property::BlackPlayer(value) => write!(f, "PB[{}]", value),
Property::GameLocation(value) => format!("PC[{}]", value), Property::GameLocation(value) => write!(f, "PC[{}]", value),
Property::WhitePlayer(value) => format!("PW[{}]", value), Property::WhitePlayer(value) => write!(f, "PW[{}]", value),
Property::Result(_) => unimplemented!(), Property::Result(_) => unimplemented!(),
Property::Round(value) => format!("RO[{}]", value), Property::Round(value) => write!(f, "RO[{}]", value),
Property::Ruleset(value) => format!("RU[{}]", value), Property::Ruleset(value) => write!(f, "RU[{}]", value),
Property::Source(value) => format!("SO[{}]", value), Property::Source(value) => write!(f, "SO[{}]", value),
Property::TimeLimit(value) => format!("TM[{}]", value.as_secs()), Property::TimeLimit(value) => write!(f, "TM[{}]", value.as_secs()),
Property::User(value) => format!("US[{}]", value), Property::User(value) => write!(f, "US[{}]", value),
Property::WhiteRank(value) => format!("WR[{}]", value), Property::WhiteRank(value) => write!(f, "WR[{}]", value),
Property::WhiteTeam(value) => format!("WT[{}]", value), Property::WhiteTeam(value) => write!(f, "WT[{}]", value),
Property::Territory(Color::White, positions) => { Property::Territory(Color::White, positions) => {
positions write!(f, "TW[{}]", Positions(positions))
.iter()
.fold("TW".to_owned(), |mut output, Position(p)| {
let _ = write!(output, "{}", p);
output
})
} }
Property::Territory(Color::Black, positions) => { Property::Territory(Color::Black, positions) => {
positions write!(f, "TB[{}]", Positions(positions))
.iter()
.fold("TB".to_owned(), |mut output, Position(p)| {
let _ = write!(output, "{}", p);
output
})
} }
Property::Unknown(UnknownProperty { ident, value }) => { Property::Unknown(UnknownProperty { ident, value }) => {
format!("{}[{}]", ident, value) write!(f, "{}[{}]", ident, value)
} }
} }
} }
} }
struct Positions<'a>(&'a Vec<Position>);
impl Display for Positions<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for Position(p) in self.0.iter() {
write!(f, "{}", p)?;
}
Ok(())
}
}
pub fn parse_collection<'a, E: nom::error::ParseError<&'a str>>( pub fn parse_collection<'a, E: nom::error::ParseError<&'a str>>(
input: &'a str, input: &'a str,
) -> IResult<&'a str, Vec<Tree>, E> { ) -> IResult<&'a str, Vec<Tree>, E> {

View File

@ -53,6 +53,7 @@ pub enum Error {
// InvalidField, // InvalidField,
// InvalidBoardSize, // InvalidBoardSize,
Incomplete, Incomplete,
#[allow(dead_code)]
InvalidSgf(VerboseNomError), InvalidSgf(VerboseNomError),
} }

View File

@ -62,14 +62,12 @@ impl<T> Tree<T> {
// Do a depth-first-search in order to get the path to a node. Start with a naive recursive // Do a depth-first-search in order to get the path to a node. Start with a naive recursive
// implementation, then switch to a stack-based implementation in order to avoid exceeding the // implementation, then switch to a stack-based implementation in order to avoid exceeding the
// stack. // stack.
/* pub fn path_to<F>(&self, _f: F) -> Vec<Node<T>>
pub fn path_to<F>(&self, f: F) -> Vec<Node<T>>
where where
F: FnOnce(&T) -> bool + Copy, F: FnOnce(&T) -> bool + Copy,
{ {
unimplemented!() unimplemented!()
} }
*/
/// Convert each node of a tree from type T to type U /// Convert each node of a tree from type T to type U
pub fn map<F, U>(&self, op: F) -> Tree<U> pub fn map<F, U>(&self, op: F) -> Tree<U>