Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PartialEq<u8> and PartialOrd for Score #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@
}
}

impl PartialEq<u8> for Score {
fn eq(&self, other: &u8) -> bool {
*self as u8 == *other
}
}

impl PartialEq<Score> for u8 {
fn eq(&self, other: &Score) -> bool {
*self == *other as u8
}
}

impl PartialOrd<u8> for Score {
fn partial_cmp(&self, other: &u8) -> Option<cmp::Ordering> {
(*self as u8).partial_cmp(other)
}
}

impl PartialOrd<Score> for u8 {
fn partial_cmp(&self, other: &Score) -> Option<cmp::Ordering> {
self.partial_cmp(&(*other as u8))
}
}

#[derive(Debug, Clone)]
pub struct GuessCalculation {
/// Estimated guesses needed to crack the password
Expand Down Expand Up @@ -1162,13 +1186,46 @@
}
}

#[test]
fn test_score_partial_eq_u8() {
assert_eq!(scoring::Score::Zero, 0);
assert_eq!(scoring::Score::One, 1);
assert_eq!(scoring::Score::Two, 2);
assert_eq!(scoring::Score::Three, 3);
assert_eq!(scoring::Score::Four, 4);

assert_eq!(0, scoring::Score::Zero);
assert_eq!(1, scoring::Score::One);
assert_eq!(2, scoring::Score::Two);
assert_eq!(3, scoring::Score::Three);
assert_eq!(4, scoring::Score::Four);
}

#[test]
fn test_score_partial_ord_u8() {
assert!(scoring::Score::Zero < 1);
assert!(scoring::Score::One > 0);
assert!(scoring::Score::Two >= 1);
assert!(scoring::Score::Three <= 4);

assert!(0 < scoring::Score::One);
assert!(1 > scoring::Score::Zero);
assert!(1 <= scoring::Score::Two);
assert!(4 >= scoring::Score::Three);

assert!(!(scoring::Score::Zero < 0));
assert!(!(scoring::Score::Four > 4));
assert!(!(0 > scoring::Score::Zero));
assert!(!(4 < scoring::Score::Four));
}

#[cfg(feature = "ser")]
#[test]
fn serde_score() {
let score = scoring::Score::One;
let value = serde_json::to_value(&score).unwrap();
assert!(matches!(value, serde_json::Value::Number(_)));
let new_score = serde_json::from_value(value).unwrap();

Check failure on line 1228 in src/scoring.rs

View workflow job for this annotation

GitHub Actions / clippy-rustfmt

type annotations needed

Check failure on line 1228 in src/scoring.rs

View workflow job for this annotation

GitHub Actions / clippy-rustfmt

type annotations needed
assert_eq!(scoring::Score::One, new_score);
}
}
Loading