1use crate::{Decimal, NumericValue};
4use candid::{CandidType, Nat as WrappedNat};
5use derive_more::{Add, AddAssign, Sub, SubAssign};
6use num_bigint::BigUint;
7use serde::{Deserialize, Serialize};
8use std::{
9 fmt,
10 iter::{Product, Sum},
11 ops::{Div, DivAssign, Mul, MulAssign},
12 str::FromStr,
13};
14
15#[derive(
20 Add,
21 AddAssign,
22 CandidType,
23 Clone,
24 Debug,
25 Default,
26 Eq,
27 PartialEq,
28 Hash,
29 Ord,
30 PartialOrd,
31 Serialize,
32 Deserialize,
33 Sub,
34 SubAssign,
35)]
36pub struct NatBig(WrappedNat);
38
39impl NatBig {
40 #[must_use]
42 pub const fn from_candid(value: WrappedNat) -> Self {
43 Self(value)
44 }
45
46 #[must_use]
48 pub fn from_biguint(value: BigUint) -> Self {
49 Self::from_candid(WrappedNat::from(value))
50 }
51
52 #[must_use]
56 pub fn u32_digits(&self) -> Vec<u32> {
57 self.0.0.to_u32_digits()
58 }
59
60 #[must_use]
62 pub fn to_u128(&self) -> Option<u128> {
63 let big = &self.0.0;
64
65 u128::try_from(big).ok()
66 }
67
68 #[must_use]
70 pub fn to_u64(&self) -> Option<u64> {
71 let big = &self.0.0;
72
73 u64::try_from(big).ok()
74 }
75
76 #[must_use]
78 pub fn to_leb128(&self) -> Vec<u8> {
79 let mut out = Vec::new();
80 let encoded = self.0.encode(&mut out);
81 debug_assert!(
82 encoded.is_ok(),
83 "Vec-backed unsigned LEB128 encoding failed"
84 );
85
86 out
87 }
88
89 pub(crate) fn to_magnitude_bytes(&self) -> Vec<u8> {
90 self.0.0.to_bytes_be()
91 }
92
93 pub(crate) fn from_magnitude_bytes(magnitude: &[u8]) -> Self {
94 Self::from_biguint(BigUint::from_bytes_be(magnitude))
95 }
96
97 #[must_use]
99 pub fn saturating_add(self, rhs: Self) -> Self {
100 Self(self.0 + rhs.0)
101 }
102
103 #[must_use]
105 pub fn saturating_sub(self, rhs: Self) -> Self {
106 if rhs > self {
107 return Self::default();
108 }
109
110 Self(self.0 - rhs.0)
111 }
112}
113
114impl fmt::Display for NatBig {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 self.0.fmt(f)
117 }
118}
119
120impl FromStr for NatBig {
121 type Err = <WrappedNat as FromStr>::Err;
122
123 fn from_str(s: &str) -> Result<Self, Self::Err> {
124 WrappedNat::from_str(s).map(Self::from_candid)
125 }
126}
127
128impl Div for NatBig {
129 type Output = Self;
130
131 fn div(self, other: Self) -> Self::Output {
132 Self(self.0 / other.0)
133 }
134}
135
136impl DivAssign for NatBig {
137 fn div_assign(&mut self, other: Self) {
138 self.0 /= other.0;
139 }
140}
141
142impl From<u64> for NatBig {
143 fn from(n: u64) -> Self {
144 Self::from_candid(WrappedNat::from(n))
145 }
146}
147
148impl From<u32> for NatBig {
149 fn from(n: u32) -> Self {
150 Self::from_candid(WrappedNat::from(n))
151 }
152}
153
154impl Mul for NatBig {
155 type Output = Self;
156
157 fn mul(self, other: Self) -> Self::Output {
158 Self(self.0 * other.0)
159 }
160}
161
162impl MulAssign for NatBig {
163 fn mul_assign(&mut self, other: Self) {
164 self.0 *= other.0;
165 }
166}
167
168impl NumericValue for NatBig {
169 fn try_to_decimal(&self) -> Option<Decimal> {
170 self.to_u128().and_then(Decimal::from_u128)
171 }
172
173 fn try_from_decimal(value: Decimal) -> Option<Self> {
174 value.to_u128().map(WrappedNat::from).map(Self::from_candid)
175 }
176}
177
178impl Product for NatBig {
179 fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
180 iter.fold(Self::from(1_u32), |acc, value| acc * value)
181 }
182}
183
184impl Sum for NatBig {
185 fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
186 iter.fold(Self::default(), |acc, x| acc + x)
187 }
188}
189
190impl TryFrom<i32> for NatBig {
191 type Error = std::num::TryFromIntError;
192
193 fn try_from(n: i32) -> Result<Self, Self::Error> {
194 let v = Self::from_candid(WrappedNat::from(u32::try_from(n)?));
195 Ok(v)
196 }
197}