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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use std::fmt::{Display, Formatter};
use std::num::TryFromIntError;
use std::ops::{Deref, DerefMut};

use serde::{Deserialize, Serialize};

use crate::entities::arithmetic_operation_wrapper::{
    impl_add_to_wrapper, impl_div_to_wrapper, impl_mul_to_wrapper, impl_partial_eq_to_wrapper,
    impl_rem_to_wrapper, impl_sub_to_wrapper,
};
use crate::signable_builder::Beacon;

#[cfg(target_family = "wasm")]
use wasm_bindgen::prelude::*;

/// BlockNumber is the block number of a Cardano transaction.
#[derive(
    Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash,
)]
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
pub struct BlockNumber(pub u64);

impl Beacon for BlockNumber {}

impl Display for BlockNumber {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Deref for BlockNumber {
    type Target = u64;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for BlockNumber {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

// Useful for conversion to sqlite number (that use i64)
impl TryFrom<BlockNumber> for i64 {
    type Error = TryFromIntError;

    fn try_from(value: BlockNumber) -> Result<Self, Self::Error> {
        value.0.try_into()
    }
}

impl_add_to_wrapper!(BlockNumber, u64);
impl_sub_to_wrapper!(BlockNumber, u64);
impl_mul_to_wrapper!(BlockNumber, u64);
impl_div_to_wrapper!(BlockNumber, u64);
impl_rem_to_wrapper!(BlockNumber, u64);
impl_partial_eq_to_wrapper!(BlockNumber, u64);

#[cfg(test)]
mod tests {
    use crate::entities::arithmetic_operation_wrapper::tests::test_op_assign;

    use super::*;

    #[test]
    fn test_display() {
        assert_eq!(format!("{}", BlockNumber(72)), "72");
        assert_eq!(format!("{}", &BlockNumber(13224)), "13224");
    }

    #[test]
    fn test_serialize() {
        assert_eq!(serde_json::to_string(&BlockNumber(72)).unwrap(), "72");
    }

    #[test]
    fn test_deserialize() {
        let block_number: BlockNumber = serde_json::from_str("13224").unwrap();
        assert_eq!(block_number, BlockNumber(13224));
    }

    #[test]
    #[allow(clippy::op_ref)]
    fn test_add() {
        assert_eq!(BlockNumber(4), BlockNumber(1) + BlockNumber(3));
        assert_eq!(BlockNumber(4), BlockNumber(1) + 3_u64);
        assert_eq!(BlockNumber(4), BlockNumber(1) + &3_u64);

        assert_eq!(BlockNumber(4), 3_u64 + BlockNumber(1));
        assert_eq!(BlockNumber(4), 3_u64 + &BlockNumber(1));
        assert_eq!(BlockNumber(4), &3_u64 + BlockNumber(1));
        assert_eq!(BlockNumber(4), &3_u64 + &BlockNumber(1));

        test_op_assign!(BlockNumber(1), +=, BlockNumber(3) => BlockNumber(4));
        test_op_assign!(BlockNumber(1), +=, 3_u64 => BlockNumber(4));
        test_op_assign!(BlockNumber(1), +=, &3_u64 => BlockNumber(4));

        test_op_assign!(1_u64, +=, BlockNumber(3) => 4_u64);
        test_op_assign!(1_u64, +=, &BlockNumber(3) => 4_u64);
    }

    #[test]
    #[allow(clippy::op_ref)]
    fn test_sub() {
        assert_eq!(BlockNumber(8), BlockNumber(14) - BlockNumber(6));
        assert_eq!(BlockNumber(8), BlockNumber(14) - 6_u64);
        assert_eq!(BlockNumber(8), BlockNumber(14) - &6_u64);

        assert_eq!(BlockNumber(8), 6_u64 - BlockNumber(14));
        assert_eq!(BlockNumber(8), 6_u64 - &BlockNumber(14));
        assert_eq!(BlockNumber(8), &6_u64 - BlockNumber(14));
        assert_eq!(BlockNumber(8), &6_u64 - &BlockNumber(14));

        test_op_assign!(BlockNumber(14), -=, BlockNumber(6) => BlockNumber(8));
        test_op_assign!(BlockNumber(14), -=, 6_u64 => BlockNumber(8));
        test_op_assign!(BlockNumber(14), -=, &6_u64 => BlockNumber(8));

        test_op_assign!(14_u64, -=, BlockNumber(6) => 8_u64);
        test_op_assign!(14_u64, -=, &BlockNumber(6) => 8_u64);
    }

    #[test]
    fn saturating_sub() {
        assert_eq!(BlockNumber(0), BlockNumber(1) - BlockNumber(5));
        assert_eq!(BlockNumber(0), BlockNumber(1) - 5_u64);
    }

    #[test]
    #[allow(clippy::op_ref)]
    fn test_mul() {
        assert_eq!(BlockNumber(6), BlockNumber(2) * BlockNumber(3));
        assert_eq!(BlockNumber(6), BlockNumber(2) * 3_u64);
        assert_eq!(BlockNumber(6), BlockNumber(2) * &3_u64);

        assert_eq!(BlockNumber(6), 3_u64 * BlockNumber(2));
        assert_eq!(BlockNumber(6), 3_u64 * &BlockNumber(2));
        assert_eq!(BlockNumber(6), &3_u64 * BlockNumber(2));
        assert_eq!(BlockNumber(6), &3_u64 * &BlockNumber(2));

        test_op_assign!(BlockNumber(2), *=, BlockNumber(3) => BlockNumber(6));
        test_op_assign!(BlockNumber(2), *=, 3_u64 => BlockNumber(6));
        test_op_assign!(BlockNumber(2), *=, &3_u64 => BlockNumber(6));

        test_op_assign!(2_u64, *=, BlockNumber(3) => 6_u64);
        test_op_assign!(2_u64, *=, &BlockNumber(3) => 6_u64);
    }

    #[test]
    #[allow(clippy::op_ref)]
    fn test_div() {
        assert_eq!(BlockNumber(6), BlockNumber(18) / BlockNumber(3));
        assert_eq!(BlockNumber(6), BlockNumber(18) / 3_u64);
        assert_eq!(BlockNumber(6), BlockNumber(18) / &3_u64);

        assert_eq!(BlockNumber(6), 12_u64 / BlockNumber(2));
        assert_eq!(BlockNumber(6), 12_u64 / &BlockNumber(2));
        assert_eq!(BlockNumber(6), &12_u64 / BlockNumber(2));
        assert_eq!(BlockNumber(6), &12_u64 / &BlockNumber(2));

        test_op_assign!(BlockNumber(18), /=, BlockNumber(3) => BlockNumber(6));
        test_op_assign!(BlockNumber(18), /=, 3_u64 => BlockNumber(6));
        test_op_assign!(BlockNumber(18), /=, &3_u64 => BlockNumber(6));

        test_op_assign!(18_u64, /=, BlockNumber(3) => 6_u64);
        test_op_assign!(18_u64, /=, &BlockNumber(3) => 6_u64);
    }

    #[test]
    #[allow(clippy::op_ref)]
    fn test_rem() {
        assert_eq!(BlockNumber(3), BlockNumber(18) % BlockNumber(5));
        assert_eq!(BlockNumber(3), BlockNumber(18) % 5_u64);
        assert_eq!(BlockNumber(3), BlockNumber(18) % &5_u64);

        assert_eq!(BlockNumber(6), 20_u64 % BlockNumber(7));
        assert_eq!(BlockNumber(6), 20_u64 % &BlockNumber(7));
        assert_eq!(BlockNumber(6), &20_u64 % BlockNumber(7));
        assert_eq!(BlockNumber(6), &20_u64 % &BlockNumber(7));

        test_op_assign!(BlockNumber(18), %=, BlockNumber(5) => BlockNumber(3));
        test_op_assign!(BlockNumber(18), %=, 5_u64 => BlockNumber(3));
        test_op_assign!(BlockNumber(18), %=, &5_u64 => BlockNumber(3));

        test_op_assign!(18_u64, %=, BlockNumber(5) => 3_u64);
        test_op_assign!(18_u64, %=, &BlockNumber(5) => 3_u64);
    }

    #[test]
    fn test_eq() {
        assert_eq!(BlockNumber(1), BlockNumber(1));
        assert_eq!(BlockNumber(2), &BlockNumber(2));
        assert_eq!(&BlockNumber(3), BlockNumber(3));
        assert_eq!(&BlockNumber(4), &BlockNumber(4));

        assert_eq!(BlockNumber(5), 5);
        assert_eq!(BlockNumber(6), &6);
        assert_eq!(&BlockNumber(7), 7);
        assert_eq!(&BlockNumber(8), &8);

        assert_eq!(9, BlockNumber(9));
        assert_eq!(10, &BlockNumber(10));
        assert_eq!(&11, BlockNumber(11));
        assert_eq!(&12, &BlockNumber(12));
    }
}