1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
//! A `UncToken` type to represent a value of Near.
//!
//! Each `UncTokens` is composed of a floating point number of tokens where each integer unit is equal to one atto-Near.
//! `UncToken` is implementing the common trait `FromStr`. Also, have utils function to parse from `str` into `u128`.
//!
//! # Examples
//! ```
//! use unc_token::UncToken;
//!
//! let one_unc = UncToken::from_attounc(10_u128.pow(24));
//! assert_eq!(one_unc, UncToken::from_unc(1));
//! assert_eq!(one_unc, UncToken::from_milliunc(1000));
//! ```
//!
//! # Crate features
//!
//! * **borsh** (optional) -
//! When enabled allows `UncToken` to serialized and deserialized by `borsh`.
//!
//! * **serde** (optional) -
//! When enabled allows `UncToken` to serialized and deserialized by `serde`.
//!
//! * **schemars** (optional) -
//! Implements `schemars::JsonSchema` for `UncToken`.
//!
//! * **interactive-clap** (optional) -
//! Implements `interactive_clap::ToCli` for `UncToken`.
mod error;
mod utils;
mod trait_impls;
pub use self::error::UncTokenError;
pub use self::utils::DecimalNumberParsingError;
#[derive(Default, Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshDeserialize, borsh::BorshSerialize)
)]
#[cfg_attr(feature = "abi", derive(borsh::BorshSchema))]
#[repr(transparent)]
pub struct UncToken {
inner: u128,
}
const ONE_UNC: u128 = 10_u128.pow(24);
const ONE_MILLIUNC: u128 = 10_u128.pow(21);
impl UncToken {
/// `from_attounc` is a function that takes value by a number of atto-unc.
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!( UncToken::from_attounc(10u128.pow(21)), UncToken::from_milliunc(1))
/// ```
pub const fn from_attounc(inner: u128) -> Self {
Self { inner }
}
/// `from_milliunc` is a function that takes value by a number of mili-unc and converts it to an equivalent to the atto-unc.
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_milliunc(1), UncToken::from_attounc(10u128.pow(21)))
/// ```
pub const fn from_milliunc(inner: u128) -> Self {
Self {
inner: inner * ONE_MILLIUNC,
}
}
/// `from_unc` is a function that takes value by a number of unc and converts it to an equivalent to the atto-unc.
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_unc(1), UncToken::from_attounc(10u128.pow(24)))
/// ```
pub const fn from_unc(inner: u128) -> Self {
Self {
inner: inner * ONE_UNC,
}
}
/// `as_unc` is a function that converts number of atto-unc to an equivalent to the unc.
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_attounc(10u128.pow(24)).as_unc(), 1)
/// ```
pub const fn as_unc(&self) -> u128 {
self.inner / ONE_UNC
}
/// `as_milliunc` is a function that converts number of atto-unc to an equivalent to the mili-unc.
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_attounc(10u128.pow(21)).as_milliunc(), 1)
/// ```
pub const fn as_milliunc(&self) -> u128 {
self.inner / ONE_MILLIUNC
}
/// `as_attounc` is a function that shows a number of atto-unc.
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_attounc(10).as_attounc(), 10)
/// ```
pub const fn as_attounc(&self) -> u128 {
self.inner
}
/// `is_zero` is a boolian function that checks `UncToken`
/// if a `UncToken` inner is zero, returns true.
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_attounc(0).is_zero(), true)
/// ```
pub const fn is_zero(&self) -> bool {
self.inner == 0
}
/// Checked integer addition. Computes self + rhs, returning None if overflow occurred.
///
/// # Examples
/// ```
/// use unc_token::UncToken;
/// use std::u128;
/// assert_eq!(UncToken::from_attounc(u128::MAX -2).checked_add(UncToken::from_attounc(2)), Some(UncToken::from_attounc(u128::MAX)));
/// assert_eq!(UncToken::from_attounc(u128::MAX -2).checked_add(UncToken::from_attounc(3)), None);
/// ```
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
if let Some(unc) = self.as_attounc().checked_add(rhs.as_attounc()) {
Some(Self::from_attounc(unc))
} else {
None
}
}
/// Checked integer subtraction. Computes self - rhs, returning None if overflow occurred.
///
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_attounc(2).checked_sub(UncToken::from_attounc(2)), Some(UncToken::from_attounc(0)));
/// assert_eq!(UncToken::from_attounc(2).checked_sub(UncToken::from_attounc(3)), None);
/// ```
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
if let Some(unc) = self.as_attounc().checked_sub(rhs.as_attounc()) {
Some(Self::from_attounc(unc))
} else {
None
}
}
/// Checked integer multiplication. Computes self * rhs, returning None if overflow occurred.
///
/// # Examples
/// ```
/// use unc_token::UncToken;
/// use std::u128;
/// assert_eq!(UncToken::from_attounc(2).checked_mul(2), Some(UncToken::from_attounc(4)));
/// assert_eq!(UncToken::from_attounc(u128::MAX).checked_mul(2), None)
pub const fn checked_mul(self, rhs: u128) -> Option<Self> {
if let Some(unc) = self.as_attounc().checked_mul(rhs) {
Some(Self::from_attounc(unc))
} else {
None
}
}
/// Checked integer division. Computes self / rhs, returning None if rhs == 0.
///
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_attounc(10).checked_div(2), Some(UncToken::from_attounc(5)));
/// assert_eq!(UncToken::from_attounc(2).checked_div(0), None);
/// ```
pub const fn checked_div(self, rhs: u128) -> Option<Self> {
if let Some(unc) = self.as_attounc().checked_div(rhs) {
Some(Self::from_attounc(unc))
} else {
None
}
}
/// Saturating integer addition. Computes self + rhs, saturating at the numeric bounds instead of overflowing.
///
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_attounc(5).saturating_add(UncToken::from_attounc(5)), UncToken::from_attounc(10));
/// assert_eq!(UncToken::from_attounc(u128::MAX).saturating_add(UncToken::from_attounc(1)), UncToken::from_attounc(u128::MAX));
/// ```
pub const fn saturating_add(self, rhs: Self) -> Self {
UncToken::from_attounc(self.as_attounc().saturating_add(rhs.as_attounc()))
}
/// Saturating integer subtraction. Computes self - rhs, saturating at the numeric bounds instead of overflowing.
///
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_attounc(5).saturating_sub(UncToken::from_attounc(2)), UncToken::from_attounc(3));
/// assert_eq!(UncToken::from_attounc(1).saturating_sub(UncToken::from_attounc(2)), UncToken::from_attounc(0));
/// ```
pub const fn saturating_sub(self, rhs: Self) -> Self {
UncToken::from_attounc(self.as_attounc().saturating_sub(rhs.as_attounc()))
}
/// Saturating integer multiplication. Computes self * rhs, saturating at the numeric bounds instead of overflowing.
///
/// # Examples
/// ```
/// use unc_token::UncToken;
/// use std::u128;
/// assert_eq!(UncToken::from_attounc(2).saturating_mul(5), UncToken::from_attounc(10));
/// assert_eq!(UncToken::from_attounc(u128::MAX).saturating_mul(2), UncToken::from_attounc(u128::MAX));
/// ```
pub const fn saturating_mul(self, rhs: u128) -> Self {
UncToken::from_attounc(self.as_attounc().saturating_mul(rhs))
}
/// Saturating integer division. Computes self / rhs, saturating at the numeric bounds instead of overflowing.
///
/// # Examples
/// ```
/// use unc_token::UncToken;
/// assert_eq!(UncToken::from_attounc(10).saturating_div(2), UncToken::from_attounc(5));
/// assert_eq!(UncToken::from_attounc(10).saturating_div(0), UncToken::from_attounc(0))
/// ```
pub const fn saturating_div(self, rhs: u128) -> Self {
if rhs == 0 {
return UncToken::from_attounc(0);
}
UncToken::from_attounc(self.as_attounc().saturating_div(rhs))
}
}
#[cfg(test)]
mod test {
use crate::UncToken;
#[test]
fn checked_add_tokens() {
let tokens = UncToken::from_attounc(u128::MAX - 3);
let any_tokens = UncToken::from_attounc(3);
let more_tokens = UncToken::from_attounc(4);
assert_eq!(
tokens.checked_add(any_tokens),
Some(UncToken::from_attounc(u128::MAX))
);
assert_eq!(tokens.checked_add(more_tokens), None);
}
#[test]
fn checked_sub_tokens() {
let tokens = UncToken::from_attounc(3);
let any_tokens = UncToken::from_attounc(1);
let more_tokens = UncToken::from_attounc(4);
assert_eq!(
tokens.checked_sub(any_tokens),
Some(UncToken::from_attounc(2))
);
assert_eq!(tokens.checked_sub(more_tokens), None);
}
#[test]
fn checked_mul_tokens() {
let tokens = UncToken::from_attounc(u128::MAX / 10);
assert_eq!(
tokens.checked_mul(10),
Some(UncToken::from_attounc(u128::MAX / 10 * 10))
);
assert_eq!(tokens.checked_mul(11), None);
}
#[test]
fn checked_div_tokens() {
let tokens = UncToken::from_attounc(10);
assert_eq!(tokens.checked_div(2), Some(UncToken::from_attounc(5)));
assert_eq!(tokens.checked_div(11), Some(UncToken::from_attounc(0)));
assert_eq!(tokens.checked_div(0), None);
}
#[test]
fn saturating_add_tokens() {
let tokens = UncToken::from_attounc(100);
let added_tokens = UncToken::from_attounc(1);
let another_tokens = UncToken::from_attounc(u128::MAX);
assert_eq!(
tokens.saturating_add(added_tokens.clone()),
UncToken::from_attounc(101)
);
assert_eq!(
another_tokens.saturating_add(added_tokens),
UncToken::from_attounc(u128::MAX)
);
}
#[test]
fn saturating_sub_tokens() {
let tokens = UncToken::from_attounc(100);
let rhs_tokens = UncToken::from_attounc(1);
let another_tokens = UncToken::from_attounc(u128::MIN);
assert_eq!(
tokens.saturating_sub(rhs_tokens.clone()),
UncToken::from_attounc(99)
);
assert_eq!(
another_tokens.saturating_sub(rhs_tokens),
UncToken::from_attounc(u128::MIN)
);
}
#[test]
fn saturating_mul_tokens() {
let tokens = UncToken::from_attounc(2);
let rhs = 10;
let another_tokens = u128::MAX;
assert_eq!(tokens.saturating_mul(rhs), UncToken::from_attounc(20));
assert_eq!(
tokens.saturating_mul(another_tokens),
UncToken::from_attounc(u128::MAX)
);
}
#[test]
fn saturating_div_tokens() {
let tokens = UncToken::from_attounc(10);
let rhs = 2;
let another_tokens = 20;
assert_eq!(tokens.saturating_div(rhs), UncToken::from_attounc(5));
assert_eq!(
tokens.saturating_div(another_tokens),
UncToken::from_attounc(0)
);
}
}