feat: add min method to Outcome
This commit is contained in:
40
01.workspace/oxidice_lib/src/imp/outcome_min.rs
Normal file
40
01.workspace/oxidice_lib/src/imp/outcome_min.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use crate::*;
|
||||
|
||||
fn min_of(values: Vec<u16>) -> Result<Outcome, FailedTo> {
|
||||
if values.is_empty() {
|
||||
return Err(FailedTo::ProcessInput);
|
||||
}
|
||||
Ok(Outcome::Scalar(u16::MIN)).and_then(|_| {
|
||||
values
|
||||
.iter()
|
||||
.min()
|
||||
.ok_or(FailedTo::FindMin)
|
||||
.map(|min| Outcome::Scalar(*min))
|
||||
})
|
||||
}
|
||||
|
||||
impl Outcome {
|
||||
pub fn min(self) -> Result<Outcome, FailedTo> {
|
||||
match self {
|
||||
Outcome::Scalar(value) => Ok(Outcome::Scalar(value)),
|
||||
Outcome::List(values) => min_of(values),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod unit_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn check_min() {
|
||||
let roll_result = Handful::grab(2, 20).roll();
|
||||
let min_result = roll_result.clone().min().unwrap();
|
||||
match roll_result {
|
||||
Outcome::List(values) => {
|
||||
let min = values.iter().min().unwrap();
|
||||
assert_eq!(min_result, Outcome::Scalar(*min));
|
||||
}
|
||||
_ => panic!("result is not a list"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user