Skip to content

Commit

Permalink
Add rustfmt and clippy to travis checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael A. Plikk committed Sep 12, 2019
1 parent 91b3543 commit a21d8bf
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 101 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 2 additions & 2 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ impl Into<String> 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("")
Expand Down
28 changes: 16 additions & 12 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
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()),
Expand Down
116 changes: 56 additions & 60 deletions src/token.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -21,7 +21,6 @@ impl Not for Color {
}
}


#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Outcome {
WinnerByResign(Color),
Expand All @@ -34,12 +33,11 @@ pub enum Outcome {
impl Outcome {
pub fn get_winner(self) -> Option<Color> {
match self {
WinnerByTime(color) |
WinnerByForfeit(color) |
WinnerByPoints(color, ..) |
WinnerByResign(color)
=> Some(color),
_ => None
WinnerByTime(color)
| WinnerByForfeit(color)
| WinnerByPoints(color, ..)
| WinnerByResign(color) => Some(color),
_ => None,
}
}
}
Expand Down Expand Up @@ -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 }),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -228,40 +225,40 @@ impl Into<String> 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)
Expand All @@ -285,7 +282,7 @@ impl Into<String> for &SgfToken {
};
let value = match *action {
Move(x, y) => coordinate_to_str((x, y)),
Pass => String::new()
Pass => String::new(),
};
format!("{}[{}]", token, value)
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -380,32 +376,32 @@ fn parse_outcome_str(s: &str) -> Result<Outcome, SgfError> {
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));
}

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::<f32>().map(|score|
WinnerByPoints(winner, score)) {
if let Ok(outcome) = points
.parse::<f32>()
.map(|score| WinnerByPoints(winner, score))
{
Ok(outcome)
} else {
Err(SgfError::from(SgfErrorKind::ParseError))
}
}
};

outcome
}
}

fn move_str_to_coord(input: &str) -> Result<Action, SgfError> {
Expand All @@ -414,7 +410,7 @@ fn move_str_to_coord(input: &str) -> Result<Action, SgfError> {
} else {
match str_to_coordinates(input) {
Ok(coordinates) => Ok(Move(coordinates.0, coordinates.1)),
Err(e) => Err(e)
Err(e) => Err(e),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/model.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
31 changes: 20 additions & 11 deletions tests/parse.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -109,15 +109,15 @@ mod parser_tests {
color: Color::Black,
action: Move(1, 1),
}],
}, ],
},],
variations: vec![
GameTree {
nodes: vec![GameNode {
tokens: vec![SgfToken::Move {
color: Color::White,
action: Move(2, 2),
}],
}, ],
},],
variations: vec![],
},
GameTree {
Expand All @@ -126,7 +126,7 @@ mod parser_tests {
color: Color::White,
action: Move(3, 3),
}],
}, ],
},],
variations: vec![],
}
],
Expand Down Expand Up @@ -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 {
Expand All @@ -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![],
}
);
Expand All @@ -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![],
}
);
Expand Down
17 changes: 4 additions & 13 deletions tests/token.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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]
Expand Down
Loading

0 comments on commit a21d8bf

Please sign in to comment.