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 fn magnitude_bits(&self) -> u64 {
43 self.0.0.bits()
44 }
45
46 #[must_use]
48 pub fn leb128_len(&self) -> u64 {
49 self.magnitude_bits().div_ceil(7).max(1)
50 }
51
52 #[must_use]
54 pub const fn from_candid(value: WrappedNat) -> Self {
55 Self(value)
56 }
57
58 #[must_use]
60 pub fn from_biguint(value: BigUint) -> Self {
61 Self::from_candid(WrappedNat::from(value))
62 }
63
64 #[must_use]
68 pub fn u32_digits(&self) -> Vec<u32> {
69 self.0.0.to_u32_digits()
70 }
71
72 #[must_use]
74 pub fn to_u128(&self) -> Option<u128> {
75 let big = &self.0.0;
76
77 u128::try_from(big).ok()
78 }
79
80 #[must_use]
82 pub fn to_u64(&self) -> Option<u64> {
83 let big = &self.0.0;
84
85 u64::try_from(big).ok()
86 }
87
88 #[must_use]
90 pub fn to_leb128(&self) -> Vec<u8> {
91 let mut out = Vec::new();
92 let encoded = self.0.encode(&mut out);
93 debug_assert!(
94 encoded.is_ok(),
95 "Vec-backed unsigned LEB128 encoding failed"
96 );
97
98 out
99 }
100
101 pub(crate) fn to_magnitude_bytes(&self) -> Vec<u8> {
102 self.0.0.to_bytes_be()
103 }
104
105 pub(crate) fn from_magnitude_bytes(magnitude: &[u8]) -> Self {
106 Self::from_biguint(BigUint::from_bytes_be(magnitude))
107 }
108
109 #[must_use]
111 pub fn saturating_add(self, rhs: Self) -> Self {
112 Self(self.0 + rhs.0)
113 }
114
115 #[must_use]
117 pub fn saturating_sub(self, rhs: Self) -> Self {
118 if rhs > self {
119 return Self::default();
120 }
121
122 Self(self.0 - rhs.0)
123 }
124}
125
126impl fmt::Display for NatBig {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 self.0.fmt(f)
129 }
130}
131
132impl FromStr for NatBig {
133 type Err = <WrappedNat as FromStr>::Err;
134
135 fn from_str(s: &str) -> Result<Self, Self::Err> {
136 WrappedNat::from_str(s).map(Self::from_candid)
137 }
138}
139
140impl Div for NatBig {
141 type Output = Self;
142
143 fn div(self, other: Self) -> Self::Output {
144 Self(self.0 / other.0)
145 }
146}
147
148impl DivAssign for NatBig {
149 fn div_assign(&mut self, other: Self) {
150 self.0 /= other.0;
151 }
152}
153
154impl From<u64> for NatBig {
155 fn from(n: u64) -> Self {
156 Self::from_candid(WrappedNat::from(n))
157 }
158}
159
160impl From<u32> for NatBig {
161 fn from(n: u32) -> Self {
162 Self::from_candid(WrappedNat::from(n))
163 }
164}
165
166impl Mul for NatBig {
167 type Output = Self;
168
169 fn mul(self, other: Self) -> Self::Output {
170 Self(self.0 * other.0)
171 }
172}
173
174impl MulAssign for NatBig {
175 fn mul_assign(&mut self, other: Self) {
176 self.0 *= other.0;
177 }
178}
179
180impl NumericValue for NatBig {
181 fn try_to_decimal(&self) -> Option<Decimal> {
182 self.to_u128().and_then(Decimal::from_u128)
183 }
184
185 fn try_from_decimal(value: Decimal) -> Option<Self> {
186 value.to_u128().map(WrappedNat::from).map(Self::from_candid)
187 }
188}
189
190impl Product for NatBig {
191 fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
192 iter.fold(Self::from(1_u32), |acc, value| acc * value)
193 }
194}
195
196impl Sum for NatBig {
197 fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
198 iter.fold(Self::default(), |acc, x| acc + x)
199 }
200}
201
202impl TryFrom<i32> for NatBig {
203 type Error = std::num::TryFromIntError;
204
205 fn try_from(n: i32) -> Result<Self, Self::Error> {
206 let v = Self::from_candid(WrappedNat::from(u32::try_from(n)?));
207 Ok(v)
208 }
209}