From 88510e489eb10af6e7166147c23218d3f1b5fe65 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 5 Oct 2023 11:41:00 -0400 Subject: [PATCH] Resolve warnings in the SGF library --- sgf/src/date.rs | 27 ++++++++++++--------------- sgf/src/go.rs | 7 ++++--- sgf/src/lib.rs | 2 +- sgf/src/tree.rs | 19 ++----------------- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/sgf/src/date.rs b/sgf/src/date.rs index 3058174..8de0e1b 100644 --- a/sgf/src/date.rs +++ b/sgf/src/date.rs @@ -1,6 +1,6 @@ use chrono::{Datelike, NaiveDate}; use serde::{Deserialize, Serialize}; -use std::num::ParseIntError; +use std::{fmt, num::ParseIntError}; use thiserror::Error; use typeshare::typeshare; @@ -11,9 +11,6 @@ pub enum Error { #[error("Invalid date")] InvalidDate, - - #[error("unsupported date format")] - Unsupported, } #[derive(Clone, Debug, PartialEq, PartialOrd, Deserialize, Serialize)] @@ -24,12 +21,12 @@ pub enum Date { Date(chrono::NaiveDate), } -impl Date { - pub fn to_string(&self) -> String { +impl fmt::Display for Date { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Date::Year(y) => format!("{}", y), - Date::YearMonth(y, m) => format!("{}-{}", y, m), - Date::Date(date) => format!("{}-{}-{}", date.year(), date.month(), date.day()), + Date::Year(y) => write!(f, "{}", y), + Date::YearMonth(y, m) => write!(f, "{}-{}", y, m), + Date::Date(date) => write!(f, "{}-{}-{}", date.year(), date.month(), date.day()), } } } @@ -71,13 +68,13 @@ impl TryFrom<&str> for Date { */ fn parse_numbers(s: &str) -> Result, Error> { - s.split("-") - .map(|s| s.parse::().map_err(|err| Error::ParseNumberError(err))) + s.split('-') + .map(|s| s.parse::().map_err(Error::ParseNumberError)) .collect::, Error>>() } pub fn parse_date_field(s: &str) -> Result, Error> { - let date_elements = s.split(","); + let date_elements = s.split(','); let mut dates = Vec::new(); let mut most_recent: Option = None; @@ -96,9 +93,9 @@ pub fn parse_date_field(s: &str) -> Result, Error> { None => Date::Year(*v1), }, [v1, v2] => Date::YearMonth(*v1, *v2 as u32), - [v1, v2, v3, ..] => Date::Date( - NaiveDate::from_ymd_opt(v1.clone(), v2.clone() as u32, v3.clone() as u32).unwrap(), - ), + [v1, v2, v3, ..] => { + Date::Date(NaiveDate::from_ymd_opt(*v1, *v2 as u32, *v3 as u32).unwrap()) + } }; dates.push(new_date.clone()); most_recent = Some(new_date); diff --git a/sgf/src/go.rs b/sgf/src/go.rs index d0fa213..2c226f4 100644 --- a/sgf/src/go.rs +++ b/sgf/src/go.rs @@ -95,6 +95,7 @@ impl Deref for Game { impl TryFrom for Game { type Error = Error; + #[allow(clippy::field_reassign_with_default)] fn try_from(tree: Tree) -> Result { let board_size = match tree.root.find_prop("SZ") { Some(prop) => Size::try_from(prop.values[0].as_str())?, @@ -131,7 +132,7 @@ impl TryFrom for Game { .root .find_prop("TM") .and_then(|prop| prop.values[0].parse::().ok()) - .and_then(|seconds| Some(std::time::Duration::from_secs(seconds))); + .map(std::time::Duration::from_secs); info.date = tree .root @@ -182,7 +183,7 @@ pub enum Rank { impl TryFrom<&str> for Rank { type Error = String; fn try_from(r: &str) -> Result { - let parts = r.split(" ").map(|s| s.to_owned()).collect::>(); + let parts = r.split(' ').map(|s| s.to_owned()).collect::>(); let cnt = parts[0].parse::().map_err(|err| format!("{:?}", err))?; match parts[1].to_ascii_lowercase().as_str() { "kyu" => Ok(Rank::Kyu(cnt)), @@ -250,7 +251,7 @@ impl TryFrom<&str> for GameResult { } else if s == "Void" { Ok(GameResult::Annulled) } else { - let parts = s.split("+").collect::>(); + let parts = s.split('+').collect::>(); let res = match parts[0].to_ascii_lowercase().as_str() { "b" => GameResult::Black, "w" => GameResult::White, diff --git a/sgf/src/lib.rs b/sgf/src/lib.rs index bd2937c..3c42229 100644 --- a/sgf/src/lib.rs +++ b/sgf/src/lib.rs @@ -51,7 +51,7 @@ impl From> for ParseError { fn from(err: nom::error::Error<&str>) -> Self { Self::NomError(nom::error::Error { input: err.input.to_owned(), - code: err.code.clone(), + code: err.code, }) } } diff --git a/sgf/src/tree.rs b/sgf/src/tree.rs index e1340a2..167353d 100644 --- a/sgf/src/tree.rs +++ b/sgf/src/tree.rs @@ -2,7 +2,7 @@ use crate::Error; use nom::{ branch::alt, bytes::complete::{escaped_transform, tag}, - character::complete::{alpha1, digit1, multispace0, multispace1, none_of}, + character::complete::{alpha1, multispace0, multispace1, none_of}, combinator::{opt, value}, multi::{many0, many1, separated_list1}, IResult, @@ -101,7 +101,7 @@ impl Node { .cloned() } - pub fn next<'a>(&'a self) -> Option<&'a Node> { + pub fn next(&self) -> Option<&Node> { self.next.get(0) } } @@ -209,21 +209,6 @@ fn parse_propval_text<'a, E: nom::error::ParseError<&'a str>>( Ok((input, value.map(|v| v.to_owned()))) } -pub fn parse_size<'a, E: nom::error::ParseError<&'a str>>( - input: &'a str, -) -> IResult<&'a str, Size, E> { - let (input, dimensions) = separated_list1(tag(":"), digit1)(input)?; - let (width, height) = match dimensions.as_slice() { - [width] => (width.parse::().unwrap(), width.parse::().unwrap()), - [width, height] => ( - width.parse::().unwrap(), - height.parse::().unwrap(), - ), - _ => (19, 19), - }; - Ok((input, Size { width, height })) -} - #[cfg(test)] mod test { use super::*;