helix_db/utils/
count.rs

1use sonic_rs::{Deserialize, Serialize};
2
3#[derive(PartialEq, Deserialize, Clone)]
4pub struct Count {
5    value: usize,
6}
7
8
9
10impl Count {
11    pub fn new(value: usize) -> Count {
12        Count {
13            value
14        }
15    }
16
17    pub fn gt(&self, cmp: usize) -> bool {
18        self.value > cmp
19    }
20
21    pub fn gte(&self, cmp: usize) -> bool {
22        self.value >= cmp
23    }
24
25    pub fn lt(&self, cmp: usize) -> bool {
26        self.value < cmp
27    }
28
29    pub fn lte(&self, cmp: usize) -> bool {
30        self.value <= cmp
31    }
32
33    pub fn eq(&self, cmp: usize) -> bool {
34        self.value == cmp
35    }
36
37    pub fn neq(&self, cmp: usize) -> bool {
38        self.value != cmp
39    }
40
41    pub fn value(&self) -> usize {
42        self.value
43    }
44}
45
46impl std::fmt::Debug for Count {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        self.value.fmt(f)
49    }
50}
51
52impl std::cmp::PartialEq<usize> for Count {
53    fn eq(&self, other: &usize) -> bool {
54        &self.value == other
55    }
56    fn ne(&self, other: &usize) -> bool {
57       &self.value != other
58    }
59}
60
61impl std::cmp::PartialOrd<usize> for Count {
62    fn partial_cmp(&self, other: &usize) -> Option<std::cmp::Ordering> {
63        self.value.partial_cmp(other)
64    }
65}
66
67impl PartialOrd for Count {
68    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
69        self.value.partial_cmp(&other.value)
70    }
71}
72
73impl Ord for Count {
74    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
75        self.value.cmp(&other.value)
76    }
77}
78
79impl Eq for Count {}
80
81impl From<Count> for usize {
82    fn from(value: Count) -> Self {
83        value.value
84    }
85}
86
87impl From<usize> for Count { 
88    fn from(value: usize) -> Self {
89        Count::new(value)
90    }
91}
92
93impl Serialize for Count {
94    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
95    where
96        S: serde::ser::Serializer,
97    {
98        self.value.serialize(serializer)
99    }
100}