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
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_partial_eq_to_wrapper, impl_sub_to_wrapper,
};

/// [Cardano Slot number](https://docs.cardano.org/learn/cardano-node/#slotsandepochs)
#[derive(
    Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash,
)]
pub struct SlotNumber(pub u64);

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

impl Deref for SlotNumber {
    type Target = u64;

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

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

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

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

impl_add_to_wrapper!(SlotNumber, u64);
impl_sub_to_wrapper!(SlotNumber, u64);
impl_partial_eq_to_wrapper!(SlotNumber, u64);

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

    use super::*;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    #[test]
    fn test_try_into_i64() {
        let slot_number = SlotNumber(42);
        let number: i64 = slot_number.try_into().unwrap();
        assert_eq!(42, number);
    }
}