1
0
Fork 0

Clippy suggestions

This commit is contained in:
Vivianne 2022-12-02 02:00:56 -08:00
parent 6d5f7f6728
commit 2525f43f99

View file

@ -5,31 +5,31 @@ use std::fs;
use std::io::{self, BufRead};
use std::str::FromStr;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, FromPrimitive)]
enum RPS {
#[derive(Clone, Copy, Debug, PartialEq, Eq, FromPrimitive)]
enum Rps {
Rock = 0,
Paper,
Scissors,
}
impl FromStr for RPS {
impl FromStr for Rps {
type Err = &'static str;
fn from_str(input: &str) -> Result<Self, Self::Err> {
match input {
"A" | "X" => Ok(RPS::Rock),
"B" | "Y" => Ok(RPS::Paper),
"C" | "Z" => Ok(RPS::Scissors),
"A" | "X" => Ok(Rps::Rock),
"B" | "Y" => Ok(Rps::Paper),
"C" | "Z" => Ok(Rps::Scissors),
_ => Err("Unknown move type!"),
}
}
}
impl Ord for RPS {
impl Ord for Rps {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(x, y) if x == y => Ordering::Equal,
(RPS::Rock, RPS::Scissors) | (RPS::Paper, RPS::Rock) | (RPS::Scissors, RPS::Paper) => {
(Rps::Rock, Rps::Scissors) | (Rps::Paper, Rps::Rock) | (Rps::Scissors, Rps::Paper) => {
Ordering::Greater
}
_ => Ordering::Less,
@ -37,8 +37,14 @@ impl Ord for RPS {
}
}
impl RPS {
fn score(self, theirs: RPS) -> u32 {
impl PartialOrd for Rps {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Rps {
fn score(self, theirs: Rps) -> u32 {
self as u32
+ 1
+ match self.cmp(&theirs) {
@ -48,7 +54,7 @@ impl RPS {
}
}
fn play_against(self, win: bool) -> RPS {
fn play_against(self, win: bool) -> Rps {
FromPrimitive::from_i8((self as i8 + if win { 1 } else { -1 }).rem_euclid(3)).unwrap()
}
}
@ -74,7 +80,7 @@ impl FromStr for PlayStrat {
}
impl PlayStrat {
fn against(self, theirs: RPS) -> RPS {
fn against(self, theirs: Rps) -> Rps {
match self {
Self::Draw => theirs,
Self::Win => theirs.play_against(true),
@ -95,7 +101,7 @@ fn main() {
let theirs = split
.next()
.expect("Want two items per line")
.parse::<RPS>()
.parse::<Rps>()
.expect("Can't get their move");
let strat = split
.next()
@ -128,38 +134,38 @@ mod tests {
#[test]
fn scores() {
assert_eq!(7, RPS::Rock.score(RPS::Scissors));
assert_eq!(8, RPS::Paper.score(RPS::Rock));
assert_eq!(1, RPS::Rock.score(RPS::Paper));
assert_eq!(6, RPS::Scissors.score(RPS::Scissors));
assert_eq!(7, Rps::Rock.score(Rps::Scissors));
assert_eq!(8, Rps::Paper.score(Rps::Rock));
assert_eq!(1, Rps::Rock.score(Rps::Paper));
assert_eq!(6, Rps::Scissors.score(Rps::Scissors));
}
#[test]
fn play_against() {
assert_eq!(RPS::Paper, RPS::Rock.play_against(true));
assert_eq!(RPS::Scissors, RPS::Paper.play_against(true));
assert_eq!(RPS::Rock, RPS::Scissors.play_against(true));
assert_eq!(Rps::Paper, Rps::Rock.play_against(true));
assert_eq!(Rps::Scissors, Rps::Paper.play_against(true));
assert_eq!(Rps::Rock, Rps::Scissors.play_against(true));
}
#[test]
fn strat_to_move() {
assert_eq!(RPS::Paper, PlayStrat::Win.against(RPS::Rock));
assert_eq!(RPS::Scissors, PlayStrat::Win.against(RPS::Paper));
assert_eq!(RPS::Rock, PlayStrat::Win.against(RPS::Scissors));
assert_eq!(Rps::Paper, PlayStrat::Win.against(Rps::Rock));
assert_eq!(Rps::Scissors, PlayStrat::Win.against(Rps::Paper));
assert_eq!(Rps::Rock, PlayStrat::Win.against(Rps::Scissors));
assert_eq!(RPS::Scissors, PlayStrat::Lose.against(RPS::Rock));
assert_eq!(RPS::Rock, PlayStrat::Lose.against(RPS::Paper));
assert_eq!(Rps::Scissors, PlayStrat::Lose.against(Rps::Rock));
assert_eq!(Rps::Rock, PlayStrat::Lose.against(Rps::Paper));
assert_eq!(RPS::Rock, PlayStrat::Draw.against(RPS::Rock));
assert_eq!(Rps::Rock, PlayStrat::Draw.against(Rps::Rock));
}
#[test]
fn part_two_ex() {
assert_eq!(4, PlayStrat::Draw.against(RPS::Rock).score(RPS::Rock));
assert_eq!(1, PlayStrat::Lose.against(RPS::Paper).score(RPS::Paper));
assert_eq!(4, PlayStrat::Draw.against(Rps::Rock).score(Rps::Rock));
assert_eq!(1, PlayStrat::Lose.against(Rps::Paper).score(Rps::Paper));
assert_eq!(
7,
PlayStrat::Win.against(RPS::Scissors).score(RPS::Scissors)
PlayStrat::Win.against(Rps::Scissors).score(Rps::Scissors)
);
}
}