From a21d8bf4459431d80be7a115abdd51e5b635af48 Mon Sep 17 00:00:00 2001 From: "Michael A. Plikk" Date: Thu, 12 Sep 2019 17:59:59 +0200 Subject: [PATCH] Add rustfmt and clippy to travis checking --- .travis.yml | 7 +++ src/lib.rs | 2 +- src/node.rs | 4 +- src/parser.rs | 28 +++++++----- src/token.rs | 116 ++++++++++++++++++++++++------------------------- tests/model.rs | 2 +- tests/parse.rs | 31 ++++++++----- tests/token.rs | 17 ++------ tests/tree.rs | 2 +- 9 files changed, 108 insertions(+), 101 deletions(-) diff --git a/.travis.yml b/.travis.yml index b4f863c..762db69 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,3 +3,10 @@ rust: - stable - beta - nightly +install: + - rustup component add rustfmt-preview + - rustup component add clippy-preview +script: + - cargo fmt -- --check + - touch ./src/lib.rs && cargo clippy -- -D warnings + - cargo test diff --git a/src/lib.rs b/src/lib.rs index e38ad74..f37e7dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,5 +37,5 @@ mod tree; pub use crate::error::{SgfError, SgfErrorKind}; pub use crate::node::GameNode; pub use crate::parser::parse; -pub use crate::token::{Color, SgfToken, Action, Outcome}; +pub use crate::token::{Action, Color, Outcome, SgfToken}; pub use crate::tree::GameTree; diff --git a/src/node.rs b/src/node.rs index c57991f..befde33 100644 --- a/src/node.rs +++ b/src/node.rs @@ -42,11 +42,11 @@ impl Into for &GameNode { Some(ref prop) if token.starts_with(prop) => { out.push(&token[offset..]); (prev, out) - }, + } _ => { out.push(&token); (Some(&token[0..offset]), out) - }, + } } }); out.join("") diff --git a/src/parser.rs b/src/parser.rs index 1805de5..36641e8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -106,20 +106,24 @@ fn parse_pair(pair: Pair<'_, Rule>) -> ParserNode<'_> { Rule::node => ParserNode::Node(pair.into_inner().map(parse_pair).collect()), Rule::property => { let text_nodes = pair.into_inner().map(parse_pair).collect::>(); - let (_, ts) = text_nodes.iter().try_fold((None, vec![]), |(ident, mut tokens), value| { - if let ParserNode::Text(value) = value { - match ident { - None => Some((Some(*value), tokens)), - Some(id) => { - tokens.push(SgfToken::from_pair(id, value)); - Some((ident, tokens)) + let (_, ts) = text_nodes + .iter() + .try_fold((None, vec![]), |(ident, mut tokens), value| { + if let ParserNode::Text(value) = value { + match ident { + None => Some((Some(*value), tokens)), + Some(id) => { + tokens.push(SgfToken::from_pair(id, value)); + Some((ident, tokens)) + } } + } else { + None } - } else { - None - } - - }).expect("Pest parsing guarantee that all properties have an identifier and a value"); + }) + .expect( + "Pest parsing guarantee that all properties have an identifier and a value", + ); ParserNode::Token(ts) } Rule::property_identifier => ParserNode::Text(pair.as_str()), diff --git a/src/token.rs b/src/token.rs index 662345f..ff4b4a4 100644 --- a/src/token.rs +++ b/src/token.rs @@ -1,8 +1,8 @@ +use crate::token::Action::{Move, Pass}; +use crate::token::Color::{Black, White}; +use crate::token::Outcome::{Draw, WinnerByForfeit, WinnerByPoints, WinnerByResign, WinnerByTime}; use crate::{SgfError, SgfErrorKind}; use std::ops::Not; -use crate::token::Action::{Pass, Move}; -use crate::token::Color::{Black, White}; -use crate::token::Outcome::{WinnerByPoints, WinnerByResign, WinnerByTime, Draw, WinnerByForfeit}; /// Indicates what color the token is related to #[derive(Debug, PartialEq, Eq, Copy, Clone)] @@ -21,7 +21,6 @@ impl Not for Color { } } - #[derive(Debug, PartialEq, Copy, Clone)] pub enum Outcome { WinnerByResign(Color), @@ -34,12 +33,11 @@ pub enum Outcome { impl Outcome { pub fn get_winner(self) -> Option { match self { - WinnerByTime(color) | - WinnerByForfeit(color) | - WinnerByPoints(color, ..) | - WinnerByResign(color) - => Some(color), - _ => None + WinnerByTime(color) + | WinnerByForfeit(color) + | WinnerByPoints(color, ..) + | WinnerByResign(color) => Some(color), + _ => None, } } } @@ -112,8 +110,10 @@ impl SgfToken { coordinate, }) }), - "HA" => Some(SgfToken::Handicap(value.parse() - .expect(&format!("Error parsing the handicap value : {}", value)))), + "HA" => match value.parse() { + Ok(value) => Some(SgfToken::Handicap(value)), + _ => None, + }, "SQ" => str_to_coordinates(value) .ok() .map(|coordinate| SgfToken::Square { coordinate }), @@ -168,10 +168,7 @@ impl SgfToken { color: Color::White, rank: value.to_string(), }), - "RE" => parse_outcome_str(value) - .ok() - .map(|o| SgfToken::Result(o)) - , + "RE" => parse_outcome_str(value).ok().map(SgfToken::Result), "KM" => value.parse().ok().map(SgfToken::Komi), "SZ" => { if let Some((width, height)) = split_size_text(value) { @@ -228,40 +225,40 @@ impl Into for &SgfToken { let value = coordinate_to_str(*coordinate); format!("LB[{}:{}]", value, label) } - SgfToken::Handicap(nb_stones) => - format!("HA[{}]", nb_stones), - SgfToken::Result(outcome) => { - match outcome { - WinnerByPoints(color, points) => format!("RE[{}+{}]", - match color { - Black => "B", - White => "W" - }, - points - ), - WinnerByResign(color) => format!("RE[{}+R]", - match color { - Black => "B", - White => "W" - } - ), + SgfToken::Handicap(nb_stones) => format!("HA[{}]", nb_stones), + SgfToken::Result(outcome) => match outcome { + WinnerByPoints(color, points) => format!( + "RE[{}+{}]", + match color { + Black => "B", + White => "W", + }, + points + ), + WinnerByResign(color) => format!( + "RE[{}+R]", + match color { + Black => "B", + White => "W", + } + ), - WinnerByTime(color) => format!("RE[{}+T]", - match color { - Black => "B", - White => "W" - } - ) - , - WinnerByForfeit(color) => format!("RE[{}+F]", - match color { - Black => "B", - White => "W" - } - ), - Draw => "RE[Draw]".to_string() - } - } + WinnerByTime(color) => format!( + "RE[{}+T]", + match color { + Black => "B", + White => "W", + } + ), + WinnerByForfeit(color) => format!( + "RE[{}+F]", + match color { + Black => "B", + White => "W", + } + ), + Draw => "RE[Draw]".to_string(), + }, SgfToken::Square { coordinate } => { let value = coordinate_to_str(*coordinate); format!("SQ[{}]", value) @@ -285,7 +282,7 @@ impl Into for &SgfToken { }; let value = match *action { Move(x, y) => coordinate_to_str((x, y)), - Pass => String::new() + Pass => String::new(), }; format!("{}[{}]", token, value) } @@ -340,7 +337,6 @@ fn split_size_text(input: &str) -> Option<(u32, u32)> { Some((width, height)) } - /// Converts goban coordinates to string representation fn coordinate_to_str(coordinate: (u8, u8)) -> String { let x = (coordinate.0 + 96) as char; @@ -380,7 +376,7 @@ fn parse_outcome_str(s: &str) -> Result { return Ok(Draw); } - let winner_option: Vec<&str> = s.split("+").collect(); + let winner_option: Vec<&str> = s.split('+').collect(); if winner_option.len() != 2 { return Err(SgfError::from(SgfErrorKind::ParseError)); } @@ -388,24 +384,24 @@ fn parse_outcome_str(s: &str) -> Result { let winner: Color = match &winner_option[0] as &str { "B" => Black, "W" => White, - _ => return Err(SgfError::from(SgfErrorKind::ParseError)) + _ => return Err(SgfError::from(SgfErrorKind::ParseError)), }; - let outcome = match &winner_option[1] as &str { + match &winner_option[1] as &str { "F" | "Forfeit" => Ok(WinnerByForfeit(winner)), "R" | "Resign" => Ok(WinnerByResign(winner)), "T" | "Time" => Ok(WinnerByTime(winner)), points => { - if let Ok(outcome) = points.parse::().map(|score| - WinnerByPoints(winner, score)) { + if let Ok(outcome) = points + .parse::() + .map(|score| WinnerByPoints(winner, score)) + { Ok(outcome) } else { Err(SgfError::from(SgfErrorKind::ParseError)) } } - }; - - outcome + } } fn move_str_to_coord(input: &str) -> Result { @@ -414,7 +410,7 @@ fn move_str_to_coord(input: &str) -> Result { } else { match str_to_coordinates(input) { Ok(coordinates) => Ok(Move(coordinates.0, coordinates.1)), - Err(e) => Err(e) + Err(e) => Err(e), } } } diff --git a/tests/model.rs b/tests/model.rs index 5071f21..f7ce6db 100644 --- a/tests/model.rs +++ b/tests/model.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod model_tests { - use sgf_parser::*; use sgf_parser::Action::Move; + use sgf_parser::*; #[test] fn can_get_unknown_nodes() { diff --git a/tests/parse.rs b/tests/parse.rs index d3816af..cb9b6b2 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod parser_tests { - use sgf_parser::*; use sgf_parser::Action::Move; + use sgf_parser::*; #[test] fn errors_on_invalid_root_token_placement() { @@ -109,7 +109,7 @@ mod parser_tests { color: Color::Black, action: Move(1, 1), }], - }, ], + },], variations: vec![ GameTree { nodes: vec![GameNode { @@ -117,7 +117,7 @@ mod parser_tests { color: Color::White, action: Move(2, 2), }], - }, ], + },], variations: vec![], }, GameTree { @@ -126,7 +126,7 @@ mod parser_tests { color: Color::White, action: Move(3, 3), }], - }, ], + },], variations: vec![], } ], @@ -185,7 +185,7 @@ mod parser_tests { }], }, GameNode { - tokens: vec![SgfToken::Unknown(("FO".to_string(), "asdf".to_string())), ], + tokens: vec![SgfToken::Unknown(("FO".to_string(), "asdf".to_string())),], }, GameNode { tokens: vec![SgfToken::Move { @@ -208,8 +208,8 @@ mod parser_tests { sgf, GameTree { nodes: vec![GameNode { - tokens: vec![SgfToken::Comment("a [wrapped\\] comment".to_string()), ], - }, ], + tokens: vec![SgfToken::Comment("a [wrapped\\] comment".to_string()),], + },], variations: vec![], } ); @@ -225,11 +225,20 @@ mod parser_tests { GameTree { nodes: vec![GameNode { tokens: vec![ - SgfToken::Add{ color: Color::Black, coordinate: (1, 1)}, - SgfToken::Add{ color: Color::Black, coordinate: (1, 2)}, - SgfToken::Add{ color: Color::Black, coordinate: (3, 3)}, + SgfToken::Add { + color: Color::Black, + coordinate: (1, 1) + }, + SgfToken::Add { + color: Color::Black, + coordinate: (1, 2) + }, + SgfToken::Add { + color: Color::Black, + coordinate: (3, 3) + }, ], - }, ], + },], variations: vec![], } ); diff --git a/tests/token.rs b/tests/token.rs index db095ce..92461c3 100644 --- a/tests/token.rs +++ b/tests/token.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod token_tests { - use sgf_parser::*; use sgf_parser::Action::Move; + use sgf_parser::*; #[test] fn can_parse_move_tokens() { @@ -270,18 +270,9 @@ mod token_tests { #[test] fn can_parse_handicap_token() { - assert_eq!( - SgfToken::from_pair("HA", "3"), - SgfToken::Handicap(3) - ); - assert_eq!( - SgfToken::from_pair("HA", "0"), - SgfToken::Handicap(0) - ); - assert_eq!( - SgfToken::from_pair("HA", "999"), - SgfToken::Handicap(999) - ) + assert_eq!(SgfToken::from_pair("HA", "3"), SgfToken::Handicap(3)); + assert_eq!(SgfToken::from_pair("HA", "0"), SgfToken::Handicap(0)); + assert_eq!(SgfToken::from_pair("HA", "999"), SgfToken::Handicap(999)) } #[test] diff --git a/tests/tree.rs b/tests/tree.rs index 68bffae..169a96c 100644 --- a/tests/tree.rs +++ b/tests/tree.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tree_tests { - use sgf_parser::*; use sgf_parser::Action::Move; + use sgf_parser::*; #[test] fn can_convert_game_tree_without_variations() {