1use std::{cmp::Ordering, hash::Hash, ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign}};
2
3use treeerror::from;
4
5use bigdecimal::{Zero, BigDecimal};
6use diesel_async::AsyncConnection;
7pub use serde;
9pub use diesel;
10use diesel::{
11 sql_types::{
12 Numeric,
13 BigInt,
14 },
15 pg::Pg,
16 AsExpression,
17 FromSqlRow,
18};
19use serde::{Deserialize, Serialize};
20
21pub mod error;
22pub mod wrap;
23
24pub mod ext;
25
26use crate::error::{
27 NumericU64Error,
28 NumericU32Error,
29};
30
31wrap_type! {
32 #[derive(Debug, Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
33 PgU64<Pg>(Numeric > bigdecimal::BigDecimal > u64)
34 |big| {
35 use bigdecimal::{
36 ToPrimitive,
37 Signed,
38 };
39 if let Some(value) = big.to_u64() {
40 Ok(value)
41 } else if big > u64::MAX {
42 Err(NumericU64Error::Overflow)
43 } else if big.is_negative() {
44 Err(NumericU64Error::Negative)
45 } else if !big.is_integer() {
46 Err(NumericU64Error::Decimal)
47 } else {
48 Err(NumericU64Error::Unknown)?
49 }?
50 }
51 |b| {
52 use bigdecimal::BigDecimal;
53 &BigDecimal::from(b)
54 }
55}
56
57from!(PgU64 = |u: u64| Self(u));
59
60wrap_type! {
61 #[derive(Debug, Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
62 PgU32<Pg>(BigInt > i64 > u32)
63 |value| {
64 if value > u32::MAX.into() {
65 return Err(NumericU32Error::Overflow.into());
66 } else if value.is_negative() {
67 return Err(NumericU32Error::Negative.into());
68 }
69 value as u32
70 }
71 |b| {
72 &(b as i64)
73 }
74}
75
76from!(PgU32 = |u: u32| Self(u));
78
79#[derive(Debug, Default, Copy, Clone)]
80#[derive(Serialize, Deserialize)]
81#[derive(AsExpression, FromSqlRow)]
82#[diesel(sql_type = Numeric)]
83pub struct SignedU64 {
84 pub total: u64,
85 pub is_negative: bool,
86}
87
88impl SubAssign for SignedU64 {
89 fn sub_assign(&mut self, other: Self) {
90 *self += -other;
91 }
92}
93
94impl Sub for SignedU64 {
95 type Output = Self;
96
97 fn sub(self, other: Self) -> Self {
98 self + -other
99 }
100}
101
102impl Add for SignedU64 {
103 type Output = Self;
104
105 fn add(self, other: Self) -> Self {
106 let mut cloned = self;
108 cloned += other;
109 cloned
110 }
111}
112
113impl AddAssign for SignedU64 {
114 fn add_assign(&mut self, other: Self) {
115 if self.is_negative == other.is_negative {
116 self.total += other.total;
117 } else if self.total >= other.total {
118 self.total -= other.total;
119 } else {
120 self.total = other.total - self.total;
121 self.is_negative = other.is_negative;
122 }
123 }
124}
125
126impl Neg for SignedU64 {
127 type Output = Self;
128
129 fn neg(self) -> Self {
130 Self {
131 total: self.total,
132 is_negative: !self.is_negative,
133 }
134 }
135}
136
137impl Zero for SignedU64 {
138 fn zero() -> Self {
139 Self {
140 total: 0,
141 is_negative: false,
142 }
143 }
144
145 fn is_zero(&self) -> bool {
146 self.total == 0
147 }
148}
149
150impl Ord for SignedU64 {
151 fn cmp(&self, other: &Self) -> Ordering {
152 if self.total == 0 && other.total == 0 {
153 return Ordering::Equal;
154 }
155
156 match (self.is_negative, other.is_negative) {
157 (true, false) => {
158 Ordering::Less
159 },
160 (true, true) => {
161 other.total.cmp(&self.total)
162 },
163 (false, true) => {
164 Ordering::Greater
165 },
166 (false, false) => {
167 self.total.cmp(&other.total)
168 },
169 }
170 }
171}
172
173impl PartialOrd for SignedU64 {
174 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
175 Some(self.cmp(other))
176 }
177}
178
179impl PartialEq for SignedU64 {
180 fn eq(&self, other: &Self) -> bool {
181 if self.total == 0 && other.total == 0 {
182 return true;
183 }
184
185 self.is_negative == other.is_negative && self.total == other.total
186 }
187}
188
189impl Eq for SignedU64 {}
190
191impl Hash for SignedU64 {
192 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
193 if self.total == 0 {
194 0u64.hash(state);
195 false.hash(state);
196 } else {
197 self.total.hash(state);
198 self.is_negative.hash(state);
199 }
200 }
201}
202
203impl Mul for SignedU64 {
204 type Output = SignedU64;
205 fn mul(self, other: Self) -> Self::Output {
206 let is_negative = self.is_negative ^ other.is_negative;
207 let total = self.total * other.total;
208 Self {
209 total,
210 is_negative,
211 }
212 }
213}
214
215impl Mul<u64> for SignedU64 {
216 type Output = SignedU64;
217 fn mul(self, other: u64) -> Self::Output {
218 let total = self.total * other;
219 Self {
220 total,
221 is_negative: self.is_negative,
222 }
223 }
224}
225
226impl_sql_convert!(
227 <Pg>
228 Numeric > BigDecimal > SignedU64
229 |big| {
230 use bigdecimal::{
231 ToPrimitive,
232 Signed,
233 };
234 if let Some(value) = big.abs().to_u64() {
235 if big.is_negative() {
236 Ok(SignedU64 {
237 is_negative: true,
238 total: value,
239 })
240 } else {
241 Ok(SignedU64 {
242 is_negative: false,
243 total: value,
244 })
245 }
246 } else if big > u64::MAX {
247 Err(NumericU64Error::Overflow)
248 } else if !big.is_integer() {
249 Err(NumericU64Error::Decimal)
250 } else {
251 Err(NumericU64Error::Unknown)
252 }?
253 }
254 |v| {
255 if v.is_negative {
256 BigDecimal::from(v.total).neg()
257 } else {
258 BigDecimal::from(v.total)
259 }
260 }
261);
262
263from!(SignedU64 = |u: u64| Self {
264 total: u,
265 is_negative: false,
266});
267
268
269pub trait PgC: AsyncConnection<Backend = Pg> + 'static {}
270impl <C: AsyncConnection<Backend = Pg> + 'static> PgC for C {}
271
272#[cfg(test)]
273mod test {
274 mod signed_u64 {
275 mod ord_and_eq {
276 use crate::SignedU64;
277
278 const fn test_data() -> [SignedU64; 6] {
279 [SignedU64 {
280 total: 0,
281 is_negative: false,
282 },
283 SignedU64 {
284 total: 0,
285 is_negative: true,
286 },
287 SignedU64 {
288 total: 1,
289 is_negative: false,
290 },
291 SignedU64 {
292 total: 1,
293 is_negative: true,
294 },
295 SignedU64 {
296 total: 4,
297 is_negative: false,
298 },
299 SignedU64 {
300 total: 4,
301 is_negative: true,
302 }]
303 }
304
305 macro_rules! test_single_sign {
306 ($val:expr, $sign:tt) => {
307 let [pos_0, neg_0, pos_1, neg_1, pos_4, neg_4] = $val;
308
309 assert_eq!(pos_0 $sign pos_0, 0 $sign 0);
310 assert_eq!(pos_0 $sign neg_0, 0 $sign -0);
311 assert_eq!(pos_0 $sign pos_1, 0 $sign 1);
312 assert_eq!(pos_0 $sign neg_1, 0 $sign -1);
313 assert_eq!(pos_0 $sign pos_4, 0 $sign 4);
314 assert_eq!(pos_0 $sign neg_4, 0 $sign -4);
315
316 assert_eq!(neg_0 $sign pos_0, -0 $sign 0);
317 assert_eq!(neg_0 $sign neg_0, -0 $sign -0);
318 assert_eq!(neg_0 $sign pos_1, -0 $sign 1);
319 assert_eq!(neg_0 $sign neg_1, -0 $sign -1);
320 assert_eq!(neg_0 $sign pos_4, -0 $sign 4);
321 assert_eq!(neg_0 $sign neg_4, -0 $sign -4);
322
323 assert_eq!(pos_1 $sign pos_0, 1 $sign 0);
324 assert_eq!(pos_1 $sign neg_0, 1 $sign -0);
325 assert_eq!(pos_1 $sign pos_1, 1 $sign 1);
326 assert_eq!(pos_1 $sign neg_1, 1 $sign -1);
327 assert_eq!(pos_1 $sign pos_4, 1 $sign 4);
328 assert_eq!(pos_1 $sign neg_4, 1 $sign -4);
329
330 assert_eq!(neg_1 $sign pos_0, -1 $sign 0);
331 assert_eq!(neg_1 $sign neg_0, -1 $sign -0);
332 assert_eq!(neg_1 $sign pos_1, -1 $sign 1);
333 assert_eq!(neg_1 $sign neg_1, -1 $sign -1);
334 assert_eq!(neg_1 $sign pos_4, -1 $sign 4);
335 assert_eq!(neg_1 $sign neg_4, -1 $sign -4);
336
337 assert_eq!(pos_4 $sign pos_0, 4 $sign 0);
338 assert_eq!(pos_4 $sign neg_0, 4 $sign -0);
339 assert_eq!(pos_4 $sign pos_1, 4 $sign 1);
340 assert_eq!(pos_4 $sign neg_1, 4 $sign -1);
341 assert_eq!(pos_4 $sign pos_4, 4 $sign 4);
342 assert_eq!(pos_4 $sign neg_4, 4 $sign -4);
343
344 assert_eq!(neg_4 $sign pos_0, -4 $sign 0);
345 assert_eq!(neg_4 $sign neg_0, -4 $sign -0);
346 assert_eq!(neg_4 $sign pos_1, -4 $sign 1);
347 assert_eq!(neg_4 $sign neg_1, -4 $sign -1);
348 assert_eq!(neg_4 $sign pos_4, -4 $sign 4);
349 assert_eq!(neg_4 $sign neg_4, -4 $sign -4);
350 };
351 }
352
353 #[test]
354 fn eq() {
355 test_single_sign!(test_data(), ==);
356 }
357 #[test]
358 fn ne() {
359 test_single_sign!(test_data(), !=);
360 }
361 #[test]
362 fn gt() {
363 test_single_sign!(test_data(), >);
364 }
365 #[test]
366 fn lt() {
367 test_single_sign!(test_data(), <);
368 }
369 #[test]
370 fn ge() {
371 test_single_sign!(test_data(), >=);
372 }
373 #[test]
374 fn le() {
375 test_single_sign!(test_data(), <=);
376 }
377 }
378 }
379}