1use std::convert::TryFrom;
2
3use crate::error::*;
4
5pub const SCALE: usize = 18;
7pub const WAD: u64 = 1_000_000_000_000_000_000;
9pub const HALF_WAD: u64 = 500_000_000_000_000_000;
11pub const PERCENT_SCALER: u64 = 10_000_000_000_000_000;
13
14pub const BPS_SCALER: u64 = PERCENT_SCALER / 100;
16
17#[allow(clippy::assign_op_pattern)]
19#[allow(clippy::ptr_offset_with_cast)]
20#[allow(clippy::reversed_empty_ranges)]
21#[allow(clippy::manual_range_contains)]
22pub mod uint {
23 use super::*;
24 use ::uint::construct_uint;
25
26 construct_uint! {
27 pub struct U192(3);
29 }
30
31 construct_uint! {
32 pub struct U128(2);
34 }
35
36 impl From<U128> for U192 {
37 fn from(value: U128) -> U192 {
38 let U128(ref arr) = value;
39 let mut ret = [0; 3];
40 ret[0] = arr[0];
41 ret[1] = arr[1];
42 U192(ret)
43 }
44 }
45
46 impl TryFrom<U192> for U128 {
47 type Error = DecimalError;
48
49 fn try_from(value: U192) -> Result<U128, DecimalError> {
50 let U192(ref arr) = value;
51 if arr[2] != 0 {
52 return Err(DecimalError::MathOverflow);
53 }
54 let mut ret = [0; 2];
55 ret[0] = arr[0];
56 ret[1] = arr[1];
57 Ok(U128(ret))
58 }
59 }
60}
61
62pub trait TrySub: Sized {
64 fn try_sub(self, rhs: Self) -> Result<Self, DecimalError>;
66}
67
68pub trait TryAdd: Sized {
70 fn try_add(self, rhs: Self) -> Result<Self, DecimalError>;
72}
73
74pub trait TryDiv<RHS>: Sized {
76 fn try_div(self, rhs: RHS) -> Result<Self, DecimalError>;
78}
79
80pub trait TryMul<RHS>: Sized {
82 fn try_mul(self, rhs: RHS) -> Result<Self, DecimalError>;
84}