zescrow_core/condition/
threshold.rs1use bincode::{Decode, Encode};
2#[cfg(feature = "json")]
3use serde::{Deserialize, Serialize};
4
5use super::Condition;
6
7#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
9#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
10pub struct Threshold {
11 pub threshold: usize,
13
14 pub subconditions: Vec<Condition>,
16}
17
18impl Threshold {
19 pub fn verify(&self) -> Result<(), Error> {
21 if self.threshold == 0 {
23 return Ok(());
24 }
25
26 let satisfied = self
27 .subconditions
28 .iter()
29 .filter(|c| c.verify().is_ok())
30 .count();
31
32 if satisfied >= self.threshold {
33 Ok(())
34 } else {
35 Err(Error::ThresholdNotMet {
36 required: self.threshold,
37 satisfied,
38 })
39 }
40 }
41}
42
43#[derive(Debug, thiserror::Error)]
45pub enum Error {
46 #[error("needed at least {required} passes, but only {satisfied} succeeded")]
48 ThresholdNotMet {
49 required: usize,
51 satisfied: usize,
53 },
54}