1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::interfaces::Ratio;
use serde::{Deserialize, Serialize};
use std::num::NonZeroU32;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PoolParticipationCapping {
    pub min: NonZeroU32,
    pub max: NonZeroU32,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct RewardConstraints {
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reward_drawing_limit_max: Option<Ratio>,

    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pool_participation_capping: Option<PoolParticipationCapping>,
}

impl RewardConstraints {
    pub fn is_none(&self) -> bool {
        self.reward_drawing_limit_max.is_none() && self.pool_participation_capping.is_none()
    }

    pub fn set_reward_drawing_limit_max(&mut self, limit: Option<Ratio>) {
        self.reward_drawing_limit_max = limit
    }

    pub fn set_pool_participation_capping(&mut self, setting: Option<(NonZeroU32, NonZeroU32)>) {
        let setting = setting.map(|(min, max)| PoolParticipationCapping { min, max });

        self.pool_participation_capping = setting;
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use quickcheck::{Arbitrary, Gen};

    impl Arbitrary for PoolParticipationCapping {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            Self {
                min: NonZeroU32::new(Arbitrary::arbitrary(g))
                    .unwrap_or(unsafe { NonZeroU32::new_unchecked(1) }),
                max: NonZeroU32::new(Arbitrary::arbitrary(g))
                    .unwrap_or(unsafe { NonZeroU32::new_unchecked(1) }),
            }
        }
    }

    impl Arbitrary for RewardConstraints {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            Self {
                reward_drawing_limit_max: Arbitrary::arbitrary(g),
                pool_participation_capping: Arbitrary::arbitrary(g),
            }
        }
    }
}