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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::interfaces::{Ratio, Value};
use chain_impl_mockchain::rewards;
use serde::{Deserialize, Serialize};
use std::num::NonZeroU64;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Copy)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub struct TaxType {
    pub fixed: Value,

    pub ratio: Ratio,

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

/* ************** Conversion *********************************** */

impl From<TaxType> for rewards::TaxType {
    fn from(tax_type: TaxType) -> Self {
        rewards::TaxType {
            fixed: tax_type.fixed.into(),
            ratio: tax_type.ratio.into(),
            max_limit: tax_type.max_limit,
        }
    }
}

impl From<rewards::TaxType> for TaxType {
    fn from(tax_type: rewards::TaxType) -> Self {
        TaxType {
            fixed: tax_type.fixed.into(),
            ratio: tax_type.ratio.into(),
            max_limit: tax_type.max_limit,
        }
    }
}

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

    impl Arbitrary for TaxType {
        fn arbitrary<G>(g: &mut G) -> Self
        where
            G: Gen,
        {
            let denom = u64::arbitrary(g) + 1;
            let num = u64::arbitrary(g) % denom;

            let ratio = Ratio::new_checked(num, denom).unwrap();
            TaxType {
                fixed: Value::arbitrary(g),
                ratio,
                max_limit: NonZeroU64::new(Arbitrary::arbitrary(g)),
            }
        }
    }

    #[test]
    fn value_serde_yaml() {
        const FIXED: u64 = 8170;
        const NUMERATOR: u64 = 192;
        const DENOMINATOR: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(1291) };
        let tax_type = TaxType {
            fixed: FIXED.into(),
            ratio: Ratio::new(NUMERATOR, DENOMINATOR),
            max_limit: None,
        };

        assert_eq!(
            serde_yaml::to_string(&tax_type).unwrap(),
            format!(
                "---\nfixed: {}\nratio: {}/{}\n",
                FIXED, NUMERATOR, DENOMINATOR
            )
        );
    }

    #[test]
    fn value_serde_yaml_with_max_limit() {
        const FIXED: u64 = 8170;
        const NUMERATOR: u64 = 192;
        const DENOMINATOR: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(1291) };
        const MAX_LIMIT: u64 = 2028;
        let tax_type = TaxType {
            fixed: FIXED.into(),
            ratio: Ratio::new(NUMERATOR, DENOMINATOR),
            max_limit: NonZeroU64::new(MAX_LIMIT),
        };

        assert_eq!(
            serde_yaml::to_string(&tax_type).unwrap(),
            format!(
                "---\nfixed: {}\nratio: {}/{}\nmax_limit: {}\n",
                FIXED, NUMERATOR, DENOMINATOR, MAX_LIMIT
            )
        );
    }

    quickcheck! {
        fn value_serde_human_readable_encode_decode(value: TaxType) -> TestResult {
            let s = serde_yaml::to_string(&value).unwrap();
            let value_dec: TaxType = serde_yaml::from_str(&s).unwrap();

            TestResult::from_bool(value_dec == value)
        }
    }
}