modes:mod: refactor Hand

Hand now requires Ord to be implemented, and test_footstool is a bit
more precise than test_footstool_non_reflexivity.
This commit is contained in:
2026-04-04 04:13:51 +01:00
committed by oreodave
parent fa6ce21fce
commit c2842c02e4

View File

@@ -9,39 +9,53 @@ pub enum Footstool {
Full, Full,
} }
pub trait Hand { pub trait Hand: Ord {
/** Given two instances of a Hand (`self` and `other`), verify if `self`
footstools `other`.
*/
fn footstool(&self, other: &Self) -> Footstool;
// Only need to implement is_proper. // Only need to implement is_proper.
fn is_proper(&self) -> bool; fn is_proper(&self) -> bool;
fn is_improper(&self) -> bool { fn is_improper(&self) -> bool {
!self.is_proper() !self.is_proper()
} }
/** Given two instances of a Hand (`self` and `other`), verify if `self`
footstools `other`.
*/
fn footstool(&self, other: &Self) -> Footstool;
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::fmt::Display;
use super::*; use super::*;
/** Given two hands, assert that their footstool condition is non-reflexive. /** Given two hands, assert that applying a footstool both ways fits a
Return the results of the two footstool checks (x on y, y on x). recognised pattern. Return the results of the two footstool checks (x on y,
y on x).
*/ */
pub fn test_footstool_non_reflexivity<T: Hand + Copy>( pub fn test_footstool<T>(x: &T, y: &T) -> (Footstool, Footstool)
x: &T, where
y: &T, T: Hand + Copy + Display,
) -> (Footstool, Footstool) { {
let res1 = x.footstool(y); let res1 = x.footstool(y);
let res2 = y.footstool(x); let res2 = y.footstool(x);
matches!( assert!(
(res1, res2), match (res1, res2) {
(Footstool::None, Footstool::None) // Default patterns we'd expect
(Footstool::Half, Footstool::None)
| (Footstool::None, Footstool::Half) | (Footstool::None, Footstool::Half)
| (Footstool::Half, Footstool::None) | (Footstool::None, Footstool::None)
| (Footstool::Full, Footstool::Full) => true,
// Patterns that require an exact examination to be certain
(Footstool::Full, Footstool::Full) => x == y,
_ => true,
},
"Expected footstool on {}, {} ({:?}, {:?}) to match a recognised pattern",
x,
y,
res1,
res2
); );
(res1, res2) (res1, res2)
} }