feat: add method max to Outcome

This commit is contained in:
2025-11-16 09:30:22 +01:00
parent 32eafd26bc
commit 1688b2acc2
4 changed files with 50 additions and 0 deletions

View File

@@ -2,4 +2,5 @@ pub mod die_new;
pub mod die_roll;
pub mod handful_grab;
pub mod handful_roll;
pub mod outcome_max;
pub mod outcome_sum;

View File

@@ -0,0 +1,41 @@
use crate::*;
fn max_of(values: Vec<u16>) -> Result<Outcome, FailedTo> {
if values.is_empty() {
return Err(FailedTo::ProcessInput);
}
let ret = Ok(Outcome::Scalar(u16::MAX));
ret.and_then(|_| {
values
.iter()
.max()
.ok_or(FailedTo::FindMin)
.map(|min| Outcome::Scalar(*min))
})
}
impl Outcome {
pub fn max(self) -> Result<Outcome, FailedTo> {
match self {
Outcome::Scalar(value) => Ok(Outcome::Scalar(value)),
Outcome::List(values) => max_of(values),
}
}
}
#[cfg(test)]
mod unit_tests {
use super::*;
#[test]
fn check_max() {
let outcome = Handful::grab(2, 20).roll();
let max = outcome.clone().max().unwrap();
match outcome {
Outcome::List(values) => {
let expected_max = values.iter().max().unwrap();
assert_eq!(max, Outcome::Scalar(*expected_max));
}
_ => panic!("outcome is not a list"),
}
}
}